给出n个点,m条边,问是否任意两点u,v,是否满足u能够到达v,或者v能够到达u

自己写的时候以为缩一下点,然后再判断一下能不能拓扑排序就可以了

但是--wa---

后来看了这篇题解

http://edward-mj.com/archives/27

按紫书上讲的,如果图中存在有向环,则不存在拓扑排序,反之则存在

所以上面这幅图是满足拓扑排序的

但是因为u,v的入度都为0,u,v之间不能到达

所以缩点完之后的图应该满足是一条长链才行

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
using namespace std; const int maxn = ;
int first[maxn];
int low[maxn],pre[maxn],sc[maxn];
int in[maxn],dout[maxn];
int n,m;
int ecnt,scnt,dfs_clock;
stack<int> S; vector<int> g[maxn];
int c[maxn];
int ans[maxn]; struct Edge{
int v,next;
}e[*maxn]; void init(){
ecnt = ;
memset(first,-,sizeof(first));
memset(in,,sizeof(in));
memset(dout,,sizeof(dout));
} void addedges(int u,int v){
e[ecnt].v = v;
e[ecnt].next = first[u];
first[u] = ecnt++;
} void dfs(int u){
low[u] = pre[u] = ++dfs_clock;
S.push(u);
for(int i = first[u];~i;i = e[i].next){
int v = e[i].v;
if(!pre[v]){
dfs(v);
low[u] = min(low[u],low[v]);
}
else if(!sc[v]) low[u] = min(low[u],pre[v]);
}
if(pre[u] == low[u]){
scnt++;
for(;;){
int x = S.top();S.pop();
sc[x] = scnt;
if(x == u) break;
}
}
} void find_scc(){
while(!S.empty()) S.pop();
memset(low,,sizeof(low));memset(pre,,sizeof(pre));
memset(sc,,sizeof(sc));
dfs_clock = scnt = ;
for(int i = ;i <= n;i++) if(!pre[i]) dfs(i);
} bool topsort(int N) {
queue<int> s;
for (int i = ; i <= N; i++) {
if (!in[i]) s.push(i);
if (s.size() == ) return false;
}
while (!s.empty()) {
int u = s.front();s.pop();
bool flag = false;
for (int i = ; i < g[u].size(); i++) {
int v = g[u][i];
in[v] -= ;
if (!in[v]) {
if (flag) return false;
s.push(v);
flag = true;
}
}
}
return true;
} int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d %d",&n,&m);
init();
for(int i = ;i <= m;i++){
int u,v;
scanf("%d %d",&u,&v);
addedges(u,v);
}
find_scc();
if(scnt == ) {
puts("Yes");
continue;
}
for(int i = ;i <= scnt;i++) g[i].clear();
for(int u = ;u <= n;u++){
for(int i = first[u];~i;i = e[i].next){
int v = e[i].v;
if(sc[u] != sc[v]) {
g[sc[u]].push_back(sc[v]);
in[sc[v]]++;
}
}
}
if(topsort(scnt)) puts("Yes");
else puts("No");
}
return ;
}

poj 2762 Going from u to v or from v to u? 【 强连通 拓扑排序】的更多相关文章

  1. POJ 2762Going from u to v or from v to u?(强联通 + 缩点 + 拓扑排序)

    [题意]: 有N个房间,M条有向边,问能否毫无顾虑的随机选两个点x, y,使从①x到达y,或者,②从y到达x,一定至少有一条成立.注意是或者,不是且. [思路]: 先考虑,x->y或者y-> ...

  2. 拓扑排序 POJ 1094 Sorting It All Out

    题意:给定N个字和M行他们之间的关系,要求输出他们的拓扑排序.此题采用边输入边检测的方式,如果发现环,就结束并输出当前行号:如果读取到当前行时,可以确定拓扑序列就输出,不管后面的输入(可能包含环路): ...

  3. POJ 2762 Going from u to v or from v to u? (强连通分量缩点+拓扑排序)

    题目链接:http://poj.org/problem?id=2762 题意是 有t组样例,n个点m条有向边,取任意两个点u和v,问u能不能到v 或者v能不能到u,要是可以就输出Yes,否则输出No. ...

  4. poj 2762 Going from u to v or from v to u?(强连通分量+缩点重构图+拓扑排序)

    http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit:  ...

  5. POJ 2762 Going from u to v or from v to u?(强连通分量+拓扑排序)

    职务地址:id=2762">POJ 2762 先缩小点.进而推断网络拓扑结构是否每个号码1(排序我是想不出来这点的. .. ).由于假如有一层为2的话,那么从此之后这两个岔路的点就不可 ...

  6. POJ 2762 Going from u to v or from v to u? (判断单连通)

    http://poj.org/problem?id=2762 题意:给出有向图,判断任意两个点u和v,是否可以从u到v或者从v到u. 思路: 判断图是否是单连通的. 首先来一遍强连通缩点,重新建立新图 ...

  7. [ tarjan + dfs ] poj 2762 Going from u to v or from v to u?

    题目链接: http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS   Memory L ...

  8. POJ 2762 Going from u to v or from v to u?(强联通,拓扑排序)

    id=2762">http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS ...

  9. [强连通分量] POJ 2762 Going from u to v or from v to u?

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17089 ...

随机推荐

  1. Kafka学习笔记(6)----Kafka使用Producer发送消息

    1. Kafka的Producer 不论将kafka作为什么样的用途,都少不了的向Broker发送数据或接受数据,Producer就是用于向Kafka发送数据.如下: 2. 添加依赖 pom.xml文 ...

  2. js商城倒计时

    js将秒转换为几天几小时几分钟 1 var oldsecond=second = 60,minute=0,hour=0,day=0; minute = parseInt(second/60); //算 ...

  3. 如何避免命令 rm -rf 的悲剧

    一.root高管用户为例,其他用户类同. https://www.cnblogs.com/eos666/articles/10389179.html [root@jenkins /]# vim /ro ...

  4. 关于MySQL日期操作函数 date_formate 的使用

    基本语法:DATE_FORMAT(date,format)说明:date 参数是合法的日期.format 规定日期/时间的输出格式.可以用的格式主要有格式 描述%a 缩写星期名%b 缩写月名%c 月, ...

  5. 洛谷P2038 无线网络发射器选址 水题 枚举

    刚开始边界写错了(将128写成127). 注意n <= 20,所以可以每读入一个点就将其周边更新,这样最多也只会有 40 * 40 * 20 种位置需要被枚举. Code: #include&l ...

  6. 更新时间戳.txt

    UPDATE bbs2 INNER JOIN time1 ON bbs2.AnnounceID = time1.AnnounceID SET bbs2.asptime = time1.asptime

  7. [luogu3627 APIO2009] 抢掠计划 (tarjan缩点+spfa最长路)

    传送门 Description Input 第一行包含两个整数 N.M.N 表示路口的个数,M 表示道路条数.接下来 M 行,每行两个整数,这两个整数都在 1 到 N 之间,第 i+1 行的两个整数表 ...

  8. groupadd(创建组)重要参数介绍

    -g :值定用户组GID值.除非接 -o 参数(如:groupadd -g 666 -o oldboy),否则ID值必须是唯一的数字(不能为负数). 如果不指定 -g 参数,则默认从500开始 

  9. linux 编译网卡驱动

    将smsc7500网卡驱动拷贝到/drive/net/usb文件夹下 拷贝ioctl_7500.h  smsc7500usbnet.c smsc7500version.h smsclan7500.h ...

  10. CCPC2018秦皇岛游记

    Day1 27号晚上8点多的火车. 然后..第二天(28号)6点40左右的样子到了天津(中转站) 然后一顿乱拍. 看到宝葫芦了没:) 然后.看到了狗不理包子铺...不过当时没开门,就溜了. 然后去秦皇 ...