洛谷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 ...
随机推荐
- 数据库之MySQL的介绍与使用20180703
/*******************************************************************************************/ 一.mysq ...
- D. Vitya and Strange Lesson Codeforces Round #430 (Div. 2)
http://codeforces.com/contest/842/problem/D 树 二进制(路径,每个节点代表一位) #include <cstdio> #include < ...
- GNU C ------ __attribute__
attribute是GNU C特色之一,attribute可以设置函数属性(Function Attribute ).变量属性(Variable Attribute )和类型属性(Type Attri ...
- linux下安装shellinabox实现web登录服务器
GitHub地址(含有文件下载和详细安装流程):https://github.com/shellinabox/shellinabox 这里我们使用的是redhat安装方法如下: 1.配置安装依赖环境 ...
- windows安装filebeat服务报错
cmd进入filebeat目录下 用以下命令执行: PowerShell.exe -ExecutionPolicy UnRestricted -File .\install-service-fil ...
- 服务器能ping通ip,通不了域名解决方案
# 将网卡配置文件配置固定ip后,添加DNS解析,然后重启网卡即可: [root@a ~]# tail -2 /etc/sysconfig/network-scripts/ifcfg-ens160 D ...
- 【转】Elastic日报 第576期 (2019-04-05)
1.Elasticsearch性能测试实践http://t.cn/EiRzFiI2.监控Elasticsearch的插件推荐http://t.cn/EiRzFix3.支持机器数据的可扩展Elastic ...
- angularJS $resource
$resource是一个更方便地与RESTful服务器API进行交互,可以方便地定义一个REST资源,而不必手动所有的声明CRUD方法的Service. 使用 1.要使用$resource首先要在HT ...
- soj1762.排座椅
1762. 排座椅 Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description 上课的时候总有一些同学和前后左右的人交头接耳,这是令 ...
- 洛谷P3953 [NOIP2017]逛公园
K<=50,感觉可以DP 先建反图求出从n到各个点的最短路,然后在正图上DP 设f[当前点][比最短路多走的距离]=方案数 转移显然是 $f[v][res]=\sum f[u][res+tmp] ...