洛谷P2017晕牛
传送门啦
这个题没有想象中复杂。
我们先有向边建立,因为我们无法改变有向边的方向。
建立完之后跑一边拓扑排序,我们按拓扑序小的指向大的就好了。
解释:
我们知道如果一个点在另一个点顺序的后面的话,如果我们添加这个点回去的边显然是不合理的,所以我们每读入一条无向边的时候我们判断这个点在拓扑排序里面位置的大小,如果 $ u > v $ 就代表 $ u $ 到$ v $ 可能有路线,但是 $ v $ 绝对不能返回 $ u $ ,那么此时如果有一条边要你选择,建立 $ u $ 到 $ v $ 不会使 $ v $ 回到 $ u $ ,判断条件也就非常明显了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 100005;
inline int read(){
char ch = getchar();
int f = 1, x = 0;
while(ch > '9' || ch < '0'){if(ch == '-')f = -1;ch = getchar();}
while(ch >= '0' && ch <= '9'){x = (x << 1) + (x << 3) + ch - '0';ch = getchar();}
return x * f;
}
int n,p1,p2,u,v;
struct Edge{
int from,to,next;
}edge[maxn << 1];
int head[maxn],tot;
int in[maxn],t[maxn],cnt;
void add(int u,int v){
edge[++tot].from = u;
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot;
}
void tuopu(){
queue<int> q;
for(int i=1;i<=n;i++)
if(!in[i]) q.push(i);
while(!q.empty()){
int cur = q.front();
t[cur] = ++cnt;
q.pop();
for(int i=head[cur];i;i=edge[i].next){
int v = edge[i].to;
in[v]--;
if(in[v] == 0)
q.push(v);
}
}
}
int main(){
n = read(); p1 = read(); p2 = read();
for(int i=1;i<=p1;i++){
u = read(); v = read();
add(u , v);
in[v]++;
}
tuopu();
for(int i=1;i<=p2;i++){
u = read(); v = read();
if(t[u] < t[v]) printf("%d %d\n",u,v);
else printf("%d %d\n",v,u);
}
return 0;
}
洛谷P2017晕牛的更多相关文章
- 洛谷 2953 [USACO09OPEN]牛的数字游戏Cow Digit Game
洛谷 2953 [USACO09OPEN]牛的数字游戏Cow Digit Game 题目描述 Bessie is playing a number game against Farmer John, ...
- 洛谷P2017 [USACO09DEC]晕牛Dizzy Cows [拓扑排序]
题目传送门 晕牛Dizzy Cows 题目背景 Hzwer 神犇最近又征服了一个国家,然后接下来却也遇见了一个难题. 题目描述 The cows have taken to racing each o ...
- 洛谷P3045 [USACO12FEB]牛券Cow Coupons
P3045 [USACO12FEB]牛券Cow Coupons 71通过 248提交 题目提供者洛谷OnlineJudge 标签USACO2012云端 难度提高+/省选- 时空限制1s / 128MB ...
- 洛谷 P3048 [USACO12FEB]牛的IDCow IDs
题目描述 Being a secret computer geek, Farmer John labels all of his cows with binary numbers. However, ...
- 洛谷P2853 [USACO06DEC]牛的野餐Cow Picnic
题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ 100) cows is grazing in one of N ...
- [洛谷P3014][USACO11FEB]牛线Cow Line (康托展开)(数论)
如果在阅读本文之前对于康托展开没有了解的同学请戳一下这里: 简陋的博客 百度百科 题目描述 N(1<=N<=20)头牛,编号为1...N,正在与FJ玩一个疯狂的游戏.奶牛会排成一行 ...
- 洛谷P3048 [USACO12FEB]牛的IDCow IDs
P3048 [USACO12FEB]牛的IDCow IDs 12通过 67提交 题目提供者lin_toto 标签USACO2012 难度普及/提高- 时空限制1s / 128MB 提交 讨论 题解 ...
- 洛谷 3029 [USACO11NOV]牛的阵容Cow Lineup
https://www.luogu.org/problem/show?pid=3029 题目描述 Farmer John has hired a professional photographer t ...
- 洛谷——P2952 [USACO09OPEN]牛线Cow Line
P2952 [USACO09OPEN]牛线Cow Line 题目描述 Farmer John's N cows (conveniently numbered 1..N) are forming a l ...
随机推荐
- 怎样将Android SDK源码 导入到Eclipse中?
在Eclipse中导入android sdk源码 http://blog.csdn.net/hahahacff/article/details/8590649
- 解题:USACO13NOV Empty Stalls
题面 当然可以用并查集做,不过你需要按秩合并+路径压缩(才可能过),因为数据范围十分不友好...... USACO的官方做法更为优秀.首先题目告诉我们牛们加入的前后顺序不影响结果(自己证明也很容易,显 ...
- ssh 通过跳板机连接到远程服务器
ssh 通过跳板机连接到远程服务器 import paramiko from sshtunnel import SSHTunnelForwarder import threading def read ...
- linux join命令
http://note.youdao.com/noteshare?id=151c4844cac74e9b08c5dc954a1a4967
- java基础-引用数据类型之二维数组(Array)
java基础-引用数据类型之二维数组(Array) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 之前我们学习过了Java的一维数组,所谓的二维数组就是元素是一堆一维数组的数组,换 ...
- call 大佬 三分姿势
为什么注释掉的三分方式不能过 @大佬 题目来源:http://hihocoder.com/problemset/problem/1142?sid=1003381 貌似不是eps的问题 #include ...
- poj 1182/codevs 1074 食物链
http://poj.org/problem?id=1182 http://codevs.cn/problem/1074/ 动物王国中有三类动物 A,B,C,这三类动物的食物链构成了有趣的环形.A吃B ...
- Machine Learning Trick of the Day (1): Replica Trick
Machine Learning Trick of the Day (1): Replica Trick 'Tricks' of all sorts are used throughout machi ...
- spring boot使用自定义配置的线程池执行Async异步任务
一.增加配置属性类 package com.chhliu.springboot.async.configuration; import org.springframework.boot.context ...
- soj1564. HOUSING
1564. HOUSING Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description For the Youth Olympic ...