[USACO09DEC]晕牛Dizzy Cows (拓扑排序)

https://www.luogu.org/problem/P2017

题目背景

Hzwer 神犇最近又征服了一个国家,然后接下来却也遇见了一个难题。

题目描述

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 A_i (1 <= A_i <= N) to pasture B_i (1 <= B_i <= N). Two-way cow paths connect pastures X_i (1 <= X_i <= N) and Y_i (1 <= Y_i <= 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 

奶牛们发现,在农场里面赛跑是很有趣的一件事.可是她们一旦在农场里面不断地转圈,就 会变得头晕目眩.众所周知,眩晕的奶牛是无法产奶的.于是,农夫约翰想要把他农场里面的双向道路全部改为单向道路,使得他的农场里面一个圈都没有,以避免他的奶牛们被搞得晕头转向.如果奶牛可以经过若干条道路回到起点,那么这些道路就称为一个圈.

农场有N(1 < N < 100000)片草地,编号为1到N.这些草地由M1条单向 道路和M2条双向道路连接起来.任何一条道路都不会把一片草地和这篇草地本 身连接起来.但是,任意两片草地之间都可能有多条道路连接.不保证任意两片草地之间都有路 径相连.

你的任务是给所有的双向道路设定一个方向,使得整个农场(只剩下单向道路)最后一个圈都 没有.也就是说,不存在一个单向道路序列的终点和起点重合.数据保证一开始就有的单向道路中,一个圈都没有.而且一开始就有的单向道路不能改变.

单向道路的起点是草地Ai,终点是草地Bi.双向道路连接草 地Xi,Yi

输入格式

从 dizzy.in 中输入数据

第一行 3 个整数 n,p1,p2,分别表示点数,有向边的数量,无向边的数量。

第二行起输入 p1 行,每行 2 个整数,a,b,表示 a 到 b 有一条有向边。

接下来输入 p2 行,每行 2 个整数 a,b,表示 a 和 b 中间有一条无向边。

输出格式

输出到 dizzy.out 中

对于每条无向边,我们要求按输入顺序输出你定向的结果,也就是如果你输出 a‘b,那

表示你将 a 和 b 中间的无向边定向为 a → b。

注意,也许存在很多可行的解。你只要输出其中任意一个就好。

(注:因为没有spj,我们保证按照常规方法做出的答案一定可以AC)

输入输出样例

输入 #1

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

输出 #1

1 3
4 2
2 3

题意:

给你一张有向无向混合图,要求你为无向边定向,使得图上没有环。

题解:

这是一道关于拓扑排序的题。

如果一个有向图没有环,那么一定可以进行拓扑排序,因为拓扑排序的前提要求是进行排序的图必须是DAG。

我们在看这道题的要求,就会发现,当你把所有双向边改成单向边以后,这个图就一定是一个DAG,否则一定无解。

那么我们只先读进来有向边,进行拓扑排序,就会得到一个序列

我们知道如果一个点在另一个点顺序的后面的话,如果我们添加这个点回去的边显然是不合理的,可能产生环,所以我们每读入一条无向边的时候我们判断这个点在拓扑排序里面位置的大小。

所以简述就是:

先拓扑排序按有向边排序,记录序号,然后无向边的顺序按排序后序号的大小定向

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <vector>
 8 #include <queue>
 9 #include <set>
10 #include <map>
11 #include <math.h>
12 const int INF=0x3f3f3f3f;
13 typedef long long LL;
14 const int mod=1e9+7;
15 const double PI=acos(-1);
16 const int maxn=100010;
17 using namespace std;
18 //ios::sync_with_stdio(false);
19 //    cin.tie(NULL);
20 
21 struct Edge
22 {
23     int u;
24     int v;
25     int next;
26 }edge[maxn];
27 
28 int n,e1,e2,u,v;
29 int head[maxn];
30 int in[maxn];//入度 
31 int od[maxn];//拓扑排序的次序,用来判断 
32 int cnt_edge;
33 int cnt;//拓扑排序的计数器 
34 queue<int> qe;
35 
36 void add_edge(int u,int v)
37 {
38     edge[++cnt_edge].next=head[u];
39     edge[cnt_edge].u=u;
40     edge[cnt_edge].v=v;
41     head[u]=cnt_edge;
42 }
43 
44 int  main()
45 {
46     //freopen("sample.txt","r",stdin);
47     scanf("%d %d %d",&n,&e1,&e2);
48     for(int i=0;i<e1;i++)
49     {
50         scanf("%d %d",&u,&v);
51         add_edge(u,v);
52         in[v]++;
53     }
54     for(int i=1;i<=n;i++)//将入度为0的入队 
55     {
56         if(in[i]==0)
57         {
58             qe.push(i);
59             od[i]=++cnt;
60         }
61     }
62     while(!qe.empty())//拓扑排序 
63     {
64         u=qe.front();
65         qe.pop();
66         for(int i=head[u];i;i=edge[i].next)
67         {
68             in[edge[i].v]--;
69             if(!in[edge[i].v])
70             {
71                 qe.push(edge[i].v);
72                 od[edge[i].v]=++cnt;
73             }
74         }
75     }
76     for(int i=1;i<=e2;i++)//判断无向边的方向 
77     {
78         scanf("%d %d",&u,&v);
79         if(od[u]<od[v])
80             printf("%d %d\n",u,v);
81         else
82             printf("%d %d\n",v,u);
83     }
84     return 0;
85 }

猜你喜欢

转载自www.cnblogs.com/jiamian/p/11374032.html