洛谷P2017 [USACO09DEC]晕牛Dizzy Cows [拓扑排序]
晕牛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)
输入输出样例
4 2 3
1 2
4 3
1 3
4 2
3 2
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+;
int n,m1,m2,head[N],cnte,dg[N];
struct Edge {
int frm,to,type,nxt;
}e[N<<];
queue<int>q; inline int read()
{
char ch=getchar(); int x=; bool flag=false;
while( ch<'' || ch>'' ) {
if( ch=='-' ) flag=true; ch=getchar();
}
while( ch>='' && ch<='' ) {
x=x*+ch-''; 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=; i<=m1; ++i) {
x=read(), y=read();
add(x,y,); dg[y]++;
}
if( !(cnte&) ) cnte++;
for(int i=; i<=m2; ++i) {
x=read(), y=read();
add(x,y,),add(y,x,);
}
for(int i=; 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== ) {
y=e[i].to;
if( !(--dg[y]) ) q.push(y);
} else if( e[i].type== ) {
e[i^].type=;
}
}
for(int i=; i<=cnte; ++i)
if( e[i].type== ) printf("%d %d\n",e[i].frm,e[i].to);
return ;
}
洛谷P2017 [USACO09DEC]晕牛Dizzy Cows [拓扑排序]的更多相关文章
- 题解 p2017 [USACO09DEC]晕牛Dizzy Cows
前言:P大终于又更新了 正文 转送门 由于当时我这个ZZ不知怎么了,这份题解排版可能有些尴尬,建议大家读完题后,看我主程序前的代码的注释,然后看最下面的图片,然后看第一张图片,对不起,望多谅解 以样例 ...
- [USACO09DEC]晕牛Dizzy Cows (拓扑排序)
https://www.luogu.org/problem/P2017 题目背景 Hzwer 神犇最近又征服了一个国家,然后接下来却也遇见了一个难题. 题目描述 The cows have taken ...
- 洛谷 P3056 [USACO12NOV]笨牛Clumsy Cows
P3056 [USACO12NOV]笨牛Clumsy Cows 题目描述 Bessie the cow is trying to type a balanced string of parenthes ...
- 洛谷P3244 落忆枫音 [HNOI2015] 拓扑排序+dp
正解:拓扑排序+dp 解题报告: 传送门 我好暴躁昂,,,怎么感觉HNOI每年总有那么几道题题面巨长啊,,,语文不好真是太心痛辣QAQ 所以还是要简述一下题意,,,就是说,本来是有一个DAG,然后后来 ...
- [USACO09DEC] Dizzy Cows 拓扑序
[USACO09DEC] Dizzy Cows 拓扑序 先对有向边跑拓扑排序,记录下每个点拓扑序,为了使最后的图不存在环,加入的\(p2\)条无向边\(u,v\)必须满足\(u\)拓扑序小于\(v\) ...
- 洛谷P1522 [USACO2.4]牛的旅行 Cow Tours
洛谷P1522 [USACO2.4]牛的旅行 Cow Tours 题意: 给出一些牧区的坐标,以及一个用邻接矩阵表示的牧区之间图.如果两个牧区之间有路存在那么这条路的长度就是两个牧区之间的欧几里得距离 ...
- 洛谷——P1821 [USACO07FEB]银牛派对Silver Cow Party
P1821 [USACO07FEB]银牛派对Silver Cow Party 题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...
- POJ3621或洛谷2868 [USACO07DEC]观光奶牛Sightseeing Cows
一道\(0/1\)分数规划+负环 POJ原题链接 洛谷原题链接 显然是\(0/1\)分数规划问题. 二分答案,设二分值为\(mid\). 然后对二分进行判断,我们建立新图,没有点权,设当前有向边为\( ...
- 拓扑排序/DP【洛谷P2883】 [USACO07MAR]牛交通Cow Traffic
P2883 [USACO07MAR]牛交通Cow Traffic 随着牛的数量增加,农场的道路的拥挤现象十分严重,特别是在每天晚上的挤奶时间.为了解决这个问题,FJ决定研究这个问题,以能找到导致拥堵现 ...
随机推荐
- bzoj1178/luogu3626 会议中心 (倍增+STL::set)
贪心地,可以建出一棵树,每个区间对应一个点,它的父亲是它右边的.与它不相交的.右端点最小的区间. 为了方便,再加入一个[0,0]区间 于是就可以倍增来做出从某个区间开始,一直到某个右界,这之中最多能选 ...
- 洛谷P3928 Sequence2(dp,线段树)
题目链接: 洛谷 题目大意在描述底下有.此处不赘述. 明显是个类似于LIS的dp. 令 $dp[i][j]$ 表示: $j=1$ 时表示已经处理了 $i$ 个数,上一个选的数来自序列 $A[0]$ 的 ...
- 【bzoj3224】 Tyvj1728—普通平衡树
http://www.lydsy.com/JudgeOnline/problem.php?id=3224 (题目链接) 题意 1. 插入x数:2. 删除x数(若有多个相同的数,因只删除一个):3. 查 ...
- 【bzoj2001】 Hnoi2010—City 城市建设
http://www.lydsy.com/JudgeOnline/problem.php?id=2001 (题目链接) 题意 给出一张无向图,$m$组操作,每次修改一条边的权值,对于每次操作输出修改之 ...
- asp.net中SQL语句太长,怎么换行写?
http://bbs.csdn.net/topics/390639485?page=1 string strfac="insert into CarInfo values('"+T ...
- Linux上常用的基本命令
复制:copy [keysystem@localhost happydzy]$ cp file1 file2 [keysystem@localhost happydzy]$ ll total -rw- ...
- duilib踩坑记录
duilib官方 https://github.com/duilib/duilib duilib他人扩展 https://github.com/qdtroy/DuiLib_Ultimate 关于两者的 ...
- 在ubuntu server上搭建Hadoop
1. Java安装: Because everything work with java. $ sudo apt-get install openjdk-7-jdk 安装之后,可以查看java的版本信 ...
- Hadoop基础-MapReduce的Join操作
Hadoop基础-MapReduce的Join操作 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.连接操作Map端Join(适合处理小表+大表的情况) no001 no002 ...
- Centos 7和 Centos 6开放查看端口 防火墙关闭打开
Centos 7 firewall 命令: 查看已经开放的端口: firewall-cmd --list-ports 开启端口 firewall-cmd --zone=public --add-por ...