题目传送门

晕牛Dizzy Cows

题目背景

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

  分析:

  有点考思维。

  如果不仔细思考的话其实一开始是看不出拓扑排序的做法的。对于这题,我们实际上只需要去掉多余的无向边就行了,因此我们可以这么做:

  输入有向边的时候记录入度,注意输入无向边的时候不要记录,然后做拓扑排序,如果遇到双向边,我们就标记一下它的反向边,最后只要输出未被标记的无向边就行了。

  Code:

//It is made by HolseLee on 25th Oct 2018
//Luogu.org P2017
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;const int N=1e5+7;
int n,m1,m2,head[N],cnte,dg[N];
struct Edge {int frm,to,type,nxt;
}e[N<<2];
queue<int>q;inline int read()
{char ch=getchar(); int x=0; bool flag=false;while( ch<'0' || ch>'9' ) {if( ch=='-' ) flag=true; ch=getchar();}while( ch>='0' && ch<='9' ) {x=x*10+ch-'0'; ch=getchar();}return flag ? -x : x ;
}inline void add(int x,int y,int z)
{e[++cnte].frm=x; e[cnte].to=y;e[cnte].type=z; e[cnte].nxt=head[x];head[x]=cnte;
}int main()
{n=read(), m1=read(), m2=read();int x,y;for(int i=1; i<=m1; ++i) {x=read(), y=read();add(x,y,0); dg[y]++;}if( !(cnte&1) ) cnte++;for(int i=1; i<=m2; ++i) {x=read(), y=read();add(x,y,1),add(y,x,1);}for(int i=1; i<=n; ++i) if( !dg[i] ) q.push(i);while( !q.empty() ) {x=q.front(), q.pop();for(int i=head[x]; i; i=e[i].nxt)if( e[i].type==0 ) {y=e[i].to;if( !(--dg[y]) ) q.push(y);} else if( e[i].type==1 ) {e[i^1].type=2;}}for(int i=1; i<=cnte; ++i)if( e[i].type==1 ) printf("%d %d\n",e[i].frm,e[i].to);return 0;
}

转载于:https://www.cnblogs.com/cytus/p/9850789.html

洛谷P2017 [USACO09DEC]晕牛Dizzy Cows [拓扑排序]相关推荐

  1. P2017 [USACO09DEC]晕牛Dizzy Cows

    图论日常不会系列... 题意:给定有向边和无向边,然后给每一条无向边定向,使得到的图无环. 我本来想缩一下点的,但是越想越晕. 然后就翻了题解,恍然大悟... 其实只需要给只有有向边的图跑一次topo ...

  2. 信息学奥赛一本通 1343:【例4-2】牛的旅行 | 洛谷 P1522 [USACO2.4] 牛的旅行 Cow Tours

    [题目链接] ybt 1343:[例4-2]牛的旅行 洛谷 P1522 [USACO2.4] 牛的旅行 Cow Tours [题目考点] 1. 图论 最短路径 Floyd算法 Floyd算法时间复杂度 ...

  3. 洛谷P2862 [USACO06JAN]把牛Corral the Cows

    P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...

  4. 洛谷——P2862 [USACO06JAN]把牛Corral the Cows

    P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...

  5. 洛谷 P2862 [USACO06JAN]把牛Corral the Cows

    P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...

  6. 洛谷 P2862 [USACO06JAN]把牛Corral the Cows 解题报告

    P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...

  7. 洛谷 P1821 [USACO07FEB]银牛派对Silver Cow Party

    题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the b ...

  8. 洛谷P1821 [USACO07FEB]银牛派对Silver Cow Party

    题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the b ...

  9. 洛谷 P1569 [USACO11FEB]属牛的抗议Generic Cow Prote…

    P1569 [USACO11FEB]属牛的抗议Generic Cow Prote- 题目描述 Farmer John's N (1 <= N <= 100,000) cows are li ...

最新文章

  1. 一段神奇的c代码错误分析
  2. python【数据结构与算法】二分归并模版
  3. 服务器文件后缀都加了re,已解决: Re: 修改了备份服务器客户端的别名之后所有的备份都出错了 - Dell Community...
  4. dbms数据库管理系统_DBMS中的数据库语言
  5. php批量采集电视剧,如何通过PHP多线程批量采集下载远程网站图片代码实例
  6. 知识技能归档--CA-PKI体系-20210324
  7. erlang的dict源码解析(2)
  8. WebDriverException: Cannot find firefox binary in PATH.的解决方法
  9. event事件的坐标 offsetWidth client scroll
  10. 用matlab求解二元二次方程组的方法
  11. eclipse SWT 中实现工程图标最小化到托盘,并只能右键托盘图标选择关闭
  12. JAVA打字游戏的实现
  13. python樱花树代码_Python画樱花树
  14. 上级对下级用通知合适吗_通知一般用于上级对下级
  15. CAD完美转ArcGIS的操作方法技巧
  16. 孤立词语音识别(1)——利用HMM-GMM模型实现数字识别(完整收发系统)
  17. Android软件中嵌入地图之三:Google地图
  18. 2008-1-14 《软件工程》课后习题解答...
  19. 按键精灵+大漠插件简单数字验证码识别实践笔记
  20. mac如何远程连接windows

热门文章

  1. ipvsadm命令用法
  2. vue项目中使用iview插件中this.$Modal.confirm的使用方法
  3. 破解HLS低延时的密匙: HLS+技术解密(一)
  4. 索引 - ElasticSearch基本使用
  5. 微信多次重定向问题解决
  6. 收藏 Dewplayer,一个非常简洁的flash音乐播放器
  7. maven使用systemPath方式加载本地jar
  8. laravel伪静态
  9. 双目测距--5 双目相机 联合 YOLOv8
  10. sql 四舍五入后补零