牛客 - 拓扑排序 - [USACO 2009 Dec G]Dizzy Cows

文章讲述了如何通过拓扑排序算法解决农场中避免循环路径问题的编程挑战,涉及有向图和无向边的处理。
摘要由CSDN通过智能技术生成

牛客 - 拓扑排序 - [USACO 2009 Dec G]Dizzy Cows

链接:https://ac.nowcoder.com/acm/problem/24868
来源:牛客网

题目描述

The cows have taken to racing each other around the farm but they get very dizzy when running in circles, and everyone knows that dizzy cows don’t produce any milk. Farmer John wants to convert all of the two-way cow paths in the farm to one-way paths in order to eliminate any ‘cycles’ and prevent the cows from getting dizzy. A ‘cycle’ enables a cow to traverse one or more cow paths and arrive back at her starting point, thus completing a loop or circle.
The farm comprises N pastures (1 <= N <= 100,000) conveniently numbered 1…N. M1 (1 <= M1 <= 100,000) one-way cow paths and M2 two-way cow paths (1 <= M2 <= 100,000) connect the pastures. No path directly connects a pasture to itself, although multiple paths might connect two different pastures. A cow may or may not be able to travel between any two given pastures by following a sequence of cow paths.
Your job is to assign a direction to the two-way cow paths such that the entire farm (ultimately with only one-way paths) has no cycles. That is, there should be no sequence of one-way cow paths which leads back to its starting position. The existing one-way cow paths do not form a cycle and should be left as they are.
One-way cow paths run from pasture Ai (1 <= Ai <= N) to pasture Bi (1 <= Bi <= N). Two-way cow paths connect pastures Xi (1 <= Xi <= N) and Yi (1 <= Yi <= N).

Consider this example:
             1-->2
             |  /|
             | / |
             |/  |
             3<--4
The cow paths between pastures 1 and 3, 2 and 3, and 2 and 4 are two-way paths. One-way paths connect 1 to 2 and also 4 to 3. One valid way to convert the two-way paths into one-way paths in such a way that there are no cycles would be to direct them from 1 to 3, from 2 to 3, and from 3 to 4:

             1-->2
             |  /|
             | / |
             vL  v
             3<--4

输入描述:

* Line 1: Three space separated integers: N, M1, and M2
* Lines 2..1+M1: Line i+1 describes a one-way cow path using two space separated integers: Ai and Bi
* Lines 2+M1..1+M1+M2: Line i+M1+1 describes a two-way cow path using two space separated integers: Xi and Yi

输出描述:

* Lines 1..M2: Line i should contain two space-separated integers: either Xi and Yi or Yi and Xi, depending on the direction assigned to the i-th two-way path. The two-way paths must appear in the same order in the output as they do in the input. If there is no solution, output "-1" on a single line.

示例1

输入

4 2 3
1 2
4 3
1 3
4 2
3 2

输出

1 3
4 2
2 3

思路分析:

看到这个有向边无向边,和无环,很容易想到拓扑排序。

这题用bfs去写

本题确保答案有解,所有我们只需要开始把入度 = 0 的点加入到队列当中去遍历他的出边(删除出边 即 入度 --),并且标记一下。

最后在遍历一下无向边输出即可。

#include<bits/stdc++.h>

using namespace std;

const int N = 100010;

int h[N], e[N], ne[N], idx;
int n, m1, m2, cnt;
int in[N], q[N], t[N];

void add(int a, int b) //此图是稀疏图用邻接表存储
{
    e[idx] = b;
    ne[idx] = h[a];
    h[a] = idx ++;
}

void topSort()
{
    int hh = 0, tt = -1;
    for(int i=1; i<=n; i++)
        if(in[i] == 0)
            q[++ tt] = i;
    
    while(hh <= tt)
    {
        int t1 = q[hh ++];
        t[t1] = cnt ++;//标记一下
        
        for(int i=h[t1]; i!=-1; i=ne[i])
        {
            int j = e[i];
            in[j] --;
            if(in[j] == 0)
                q[++ tt] = j;
        }
    }
}

int main()
{
    memset(h, -1, sizeof h);
    
    cin >> n >> m1 >> m2;
    
    while(m1 --)
    {
        int a, b;
        cin >> a >> b;
        in[b] ++; //入度 ++
        add(a, b);
    }
    
    topSort();
    
    while(m2 --)
    {
        int a, b;
   	    cin >> a >> b;
   	    if(t[a] < t[b]) cout << a << " " << b << endl;
   	    else cout << b << " " << a << endl;
    }
        
    return 0;
}
  • 35
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值