传送门啦

这个题没有想象中复杂。

我们先有向边建立,因为我们无法改变有向边的方向。

建立完之后跑一边拓扑排序,我们按拓扑序小的指向大的就好了。

解释:

我们知道如果一个点在另一个点顺序的后面的话,如果我们添加这个点回去的边显然是不合理的,所以我们每读入一条无向边的时候我们判断这个点在拓扑排序里面位置的大小,如果 $ 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晕牛的更多相关文章

  1. 洛谷 2953 [USACO09OPEN]牛的数字游戏Cow Digit Game

    洛谷 2953 [USACO09OPEN]牛的数字游戏Cow Digit Game 题目描述 Bessie is playing a number game against Farmer John, ...

  2. 洛谷P2017 [USACO09DEC]晕牛Dizzy Cows [拓扑排序]

    题目传送门 晕牛Dizzy Cows 题目背景 Hzwer 神犇最近又征服了一个国家,然后接下来却也遇见了一个难题. 题目描述 The cows have taken to racing each o ...

  3. 洛谷P3045 [USACO12FEB]牛券Cow Coupons

    P3045 [USACO12FEB]牛券Cow Coupons 71通过 248提交 题目提供者洛谷OnlineJudge 标签USACO2012云端 难度提高+/省选- 时空限制1s / 128MB ...

  4. 洛谷 P3048 [USACO12FEB]牛的IDCow IDs

    题目描述 Being a secret computer geek, Farmer John labels all of his cows with binary numbers. However, ...

  5. 洛谷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 ...

  6. [洛谷P3014][USACO11FEB]牛线Cow Line (康托展开)(数论)

    如果在阅读本文之前对于康托展开没有了解的同学请戳一下这里:  简陋的博客    百度百科 题目描述 N(1<=N<=20)头牛,编号为1...N,正在与FJ玩一个疯狂的游戏.奶牛会排成一行 ...

  7. 洛谷P3048 [USACO12FEB]牛的IDCow IDs

    P3048 [USACO12FEB]牛的IDCow IDs 12通过 67提交 题目提供者lin_toto 标签USACO2012 难度普及/提高- 时空限制1s / 128MB 提交  讨论  题解 ...

  8. 洛谷 3029 [USACO11NOV]牛的阵容Cow Lineup

    https://www.luogu.org/problem/show?pid=3029 题目描述 Farmer John has hired a professional photographer t ...

  9. 洛谷——P2952 [USACO09OPEN]牛线Cow Line

    P2952 [USACO09OPEN]牛线Cow Line 题目描述 Farmer John's N cows (conveniently numbered 1..N) are forming a l ...

随机推荐

  1. java 批量文件后缀重命名

    import java.io.File; public class BatchFileSuffixRename { public static void main(String[] args) { / ...

  2. 解题:NOI 1999 生日蛋糕

    题面 裸的搜索题,就说剪枝(注:nw->noww->当前,res->rest->剩余): 1.想达到$Nπ$的体积,那么半径一开始最多也就$sqrt(n)$了,再大就超了... ...

  3. 菜鸟合作伙伴日志接入规范之C#实现

    由于行业的业务功能需要,需要与菜鸟对接登录日志,将我们系统的用户登录信息提交给菜鸟,菜鸟分析通过分析日志,检查存在的一些安全问题.根据菜鸟提供的标准,实现了C#的接入,下面分享给大家,让需要的朋友少走 ...

  4. mac 必备工具

    iTerm 可以在一个窗口中垂直.水平分割窗口,而不用切换来切换去 一些基本功能如下: 1.分窗口操作:shift+command+d(横向)command+d(竖向) 2.查找和粘贴:command ...

  5. 用递归的方法求一个数组的前n项和

    用递归的方法求一个数组的前n项和 public class Demo1 { /* * 用递归的方法求一个数组的前n项和 */ public static void main(String[] args ...

  6. [Leetcode] Backtracking回溯法解题思路

    碎碎念: 最近终于开始刷middle的题了,对于我这个小渣渣确实有点难度,经常一两个小时写出一道题来.在开始写的几道题中,发现大神在discuss中用到回溯法(Backtracking)的概率明显增大 ...

  7. canvas 入门

    <canvas>是HTML5新增的,是可以使用脚本(JavaScript)在其中绘制图像的HTML元素. canvas是由HTML代码配合高度和宽度属性而定义出的可绘制区域,JavaScr ...

  8. VBS 重启 TP-Link 路由器

    分享一个自己用的小工具,重启TP-Link路由器的,好像还是大学时候写的,献丑了. 其他路由器可能有些不同,但是思路都是差不多的. user = "admin" '路由器帐号 pa ...

  9. soj1564. HOUSING

    1564. HOUSING Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description For the Youth Olympic ...

  10. [csp-201509-3]模板生成系统

    #include<bits/stdc++.h> using namespace std; ; string a[N],b[N],c[N]; int main() { //freopen(& ...