poj 2762 Going from u to v or from v to u? 【 强连通 拓扑排序】
给出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? 【 强连通 拓扑排序】的更多相关文章
- POJ 2762Going from u to v or from v to u?(强联通 + 缩点 + 拓扑排序)
[题意]: 有N个房间,M条有向边,问能否毫无顾虑的随机选两个点x, y,使从①x到达y,或者,②从y到达x,一定至少有一条成立.注意是或者,不是且. [思路]: 先考虑,x->y或者y-> ...
- 拓扑排序 POJ 1094 Sorting It All Out
题意:给定N个字和M行他们之间的关系,要求输出他们的拓扑排序.此题采用边输入边检测的方式,如果发现环,就结束并输出当前行号:如果读取到当前行时,可以确定拓扑序列就输出,不管后面的输入(可能包含环路): ...
- 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. ...
- 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: ...
- POJ 2762 Going from u to v or from v to u?(强连通分量+拓扑排序)
职务地址:id=2762">POJ 2762 先缩小点.进而推断网络拓扑结构是否每个号码1(排序我是想不出来这点的. .. ).由于假如有一层为2的话,那么从此之后这两个岔路的点就不可 ...
- POJ 2762 Going from u to v or from v to u? (判断单连通)
http://poj.org/problem?id=2762 题意:给出有向图,判断任意两个点u和v,是否可以从u到v或者从v到u. 思路: 判断图是否是单连通的. 首先来一遍强连通缩点,重新建立新图 ...
- [ 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 ...
- 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 ...
- [强连通分量] 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 ...
随机推荐
- paramiko模块学习笔记
SSHClient 基于用户名密码连接 import paramiko # 创建SSH对象 ssh = paramiko.SSHClient() # 允许连接不在know_hosts文件中的主机 ss ...
- Java中方法重载
方法重载:指在同一个类中,允许存在一个以上的同名方法,只要它们的参数列表不同即可,与修饰符和返回值类型无关. 参数列表:个数不同,数据类型不同,顺序不同. 重载方法调用:JVM通过方法的参数列表,调用 ...
- MySQL数据库各个版本的区别
MySQL数据库各个版本的区别 MySQL数据库 MySQL是一种开放源代码的关系型数据库管理系统(RDBMS),MySQL数据库系统使用最常用的数据库管理语言--结构化查询语言(SQL)进行数据库管 ...
- UNIX时间转换ASP代码.txt
'参数:strTime:要转换的时 间:intTimeZone:该时间对应的时区 '返回值:strTime相对于1970年1月1日午夜0点经过的秒数 '示例:ToUnixTime("2008 ...
- [SDOI2016]生成魔咒(后缀自动机)
看一眼题.本质不同的字串数. 嘴角微微上扬. 每一次加一个数输出一个答案. 笑容渐渐消失. 等等,\(SAM\)好像也可以求本质不同的字串. 设当前字符串用\(x\)表示,每次插入完成后\(ans\) ...
- 洛谷P1427 小鱼的数字游戏
题目描述 小鱼最近被要求参加一个数字游戏,要求它把看到的一串数字(长度不一定,以0结束,最多不超过100个,数字不超过2^32-1),记住了然后反着念出来(表示结束的数字0就不要念出来了).这对小鱼的 ...
- C 语言预编译 #if #else
这个方法我一般用来调试的时候用,有时候串口打印信息太多,一条条注释就很麻烦,于是就用这种方法,定义个宏变量,判断宏变量的条件,来达到改变宏变量的值控制那些代码编译,那些不编译的目的,这样就不用一条条代 ...
- nmon分析文件各sheet含义
sheet名称sheet含义 SYS_SUMM系统汇总,蓝线为cpu占有率变化情况,粉线为磁盘IO的变化情况: AAA关于操作系统以及nmon本身的一些信息: BBBB系统外挂存储容量以及存储类型: ...
- 最短路&查分约束
[HDU] 1548 A strange lift 根蒂根基最短路(或bfs)★ 2544 最短路 根蒂根基最短路★ 3790 最短路径题目 根蒂根基最短路★ 2066 一小我的观光 根蒂根基最短路( ...
- nodejs-NPM基本使用
搜索模块 npm search express 更新模块 npm update express 卸载模块 npm uninstall express 安装模块 npm install express ...