Going from u to v or from v to u? 【判定弱连通】=【tarjan求scc+ 缩点+topo】
来源:互联网 发布:淘宝上传生产许可证 编辑:程序博客网 时间:2024/05/16 05:30
In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has n rooms, and one-way corridors connecting some rooms. Each time, Wind choose two rooms x and y, and ask one of their little sons go from one to the other. The son can either go from x to y, or from y to x. Wind promised that her tasks are all possible, but she actually doesn’t know how to decide if a task is possible. To make her life easier, Jiajia decided to choose a cave in which every pair of rooms is a possible task. Given a cave, can you tell Jiajia whether Wind can randomly choose two rooms without worrying about anything?
Input
The first line contains a single integer T, the number of test cases. And followed T cases.
The first line for each case contains two integers n, m(0 < n < 1001,m < 6000), the number of rooms and corridors in the cave. The next m lines each contains two integers u and v, indicating that there is a corridor connecting room u and room v directly.
Output
The output should contain T lines. Write ‘Yes’ if the cave has the property stated above, or ‘No’ otherwise.
Sample Input
1
3 3
1 2
2 3
3 1
Sample Output
Yes
题意 给一个有向图,问是否对于任意的两个点,都可以相互到达。
是不是和强连通很像,其实我少说了一个很重要的条件,就是这个任意的两个点:没有规定那个是起始点,那个是终点,都可以当起点或者终点。所以和强连通还是很不一样。 其实这样的图叫做弱连通图;
对于一个DAG(有向图求scc+缩点后的新图(有向无环图))图来说,其实这样题意的图(弱连通图),就是只要有一个起点,它可以走过所有的点,并到达终点。 这样的图符合题意(弱连通图);
链接
代码
没加输入挂,334ms 加了输入挂64ms 。。 再一次感叹输入挂的强大。其实仔细想一下这道题的输入量 ,m*t*2==2*6000*t ;也就至少到了1e5级了。一般到1e5级的输入量,用输入挂,效果就很显著。
#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<stack>#include<queue>#include<iostream>#include<vector>using namespace std;const int MAXN= 1001+10;inline int read(){ int x=0,f=1;char ch=getchar(); while(ch>'9'||ch<'0'){ if(ch=='-') f=-1; ch=getchar();} while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();} return x*f;} // 输入挂/*------------------------------------*/struct Edge { int from,to,next;}edge[6000+10];int head[MAXN],top;int n,m;void init(){ memset(head,-1,sizeof(head)); top=0;}void addedge(int a,int b){ Edge e={a,b,head[a]}; edge[top]=e;head[a]=top++;}void getmap(){ int a,b; while(m--){ a=read();b=read(); addedge(a,b); }}int low[MAXN],dfn[MAXN];int scc_cnt,sccno[MAXN];stack<int>S;int Instack[MAXN];vector<int>G[MAXN];int dfs_clock;void tarjan(int now,int par){ low[now]=dfn[now]=++dfs_clock; S.push(now);Instack[now]=1; for(int i=head[now];i!=-1;i=edge[i].next){ Edge e=edge[i]; if(!dfn[e.to]){ tarjan(e.to,now); low[now]=min(low[now],low[e.to]); }else if(Instack[e.to]) low[now]=min(low[now],dfn[e.to]); } if(low[now]==dfn[now]) { scc_cnt++; for(;;){ int nexts=S.top();S.pop();Instack[nexts]=0; sccno[nexts]=scc_cnt; if(nexts==now) break; } }}void find_cut(int le,int ri){ memset(low,0,sizeof(low)); memset(dfn,0,sizeof(dfn)); memset(Instack,0,sizeof(Instack)); memset(sccno,0,sizeof(sccno)); dfs_clock=scc_cnt=0; for(int i=le;i<=ri;i++){ if(!dfn[i]) tarjan(i,-1); }}int in[MAXN];void suodian(){ for(int i=1;i<=scc_cnt;i++) { in[i]=0;G[i].clear(); } for(int i=0;i<top;i++){ Edge e=edge[i]; int now=sccno[e.from]; int nexts=sccno[e.to]; if(now!=nexts){ G[now].push_back(nexts); in[nexts]++; } }}queue<int>Q;bool topo(){ while(!Q.empty()) Q.pop(); int num=0; for(int i=1;i<=scc_cnt;i++){ if(!in[i]) { Q.push(i); num++; if(num>1) return false; } } int k=0; while(!Q.empty()){ int now=Q.front();Q.pop();num=0;k++; for(int i=0;i<G[now].size();i++){ int v=G[now][i]; if(--in[v]==0){ Q.push(v); num++; if(num>1) return false; } } } return k==scc_cnt;}void solve(){ find_cut(1,n); suodian(); if(scc_cnt==1) puts("Yes"); else puts(topo()?"Yes":"No");}int main(){ int t;t=read();while(t--){ n=read();m=read(); init(); getmap(); solve(); } return 0;}
- Going from u to v or from v to u? 【判定弱连通】=【tarjan求scc+ 缩点+topo】
- poj 2762 Going from u to v or from v to u? 【判断图是否为弱连通】 【tarjan求SCC + 缩点 + 拓扑排序】
- POJ2762 Going from u to v or from v to u? 强连通 Tarjan缩点+拓扑排序topsort
- 【POJ2762】Going from u to v or from v to u?(tarjan+缩点+拓扑排序)
- [练习][poj2762]tarjan缩点 Going from u to v or from v to u?
- POJ2762 Going from u to v or from v to u? 强连通+缩点
- POJ 2762--Going from u to v or from v to u?【scc缩点新建图 && 判断是否是弱连通图】
- PKU 2762 Going from u to v or from v to u? - 单连通图判定
- poj 2762 Going from u to v or from v to u? 单向连通图判定
- poj 2762 Going from u to v or from v to u?(SCC缩点+拓扑排序)
- POJ 2762 Going from u to v or from v to u? Tarjan缩点+判断链
- POJ--2762--Going from u to v or from v to u?【tarjan缩点+拓扑排序】
- POJ2762-Going from u to v or from v to u?(Tarjan缩点,DAG判直链)
- Poj 2762 Going from u to v or from v to u? (判断图弱连通)
- POJ 2762 Going from u to v or from v to u(弱连通分量)
- POJ 2762 Going from u to v or from v to u? 弱连通分量 -
- POJ2762 Going from u to v or from v to u?(强连通分量缩点+拓扑排序)
- POJ 2762 Going from u to v or from v to u?(强连通分量+缩点)
- 44 WebGL通过点击获取到点击面的下标
- Linux菜鸟笔记——磁盘管理与文件系统管理 之 磁盘的分割、格式化、检验
- Can you find it?(二分)
- MySQL数据库小技巧(持续更新)
- 【shell脚本】eval命令详解及命令代换
- Going from u to v or from v to u? 【判定弱连通】=【tarjan求scc+ 缩点+topo】
- Android Studio 使用第三方库的方法
- 215.m1-当服务器给的同类型图片大小不一致的时候的适配
- 登陆框居中问题
- Android Studio上传项目至GitHub的方法
- spring3 升级4 spring security4.2.x配置
- 解决 python 中文输出成乱码的心得
- 佐切的第三天学习分享
- 对象的打印和比较对象以及匿名对象和对象的生命周期