POJ 2762 Going from u to v or from v to u?(Tarjan)
2012-08-17 15:52
253 查看
题意:对任给的两个点x,y,是否一定有路,从x到y,或者从y到x,,,刚开始看错了,以为是赤裸裸的Tarjan,结果是或,不是且,,比较简单
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> using namespace std; const int N = 1009; const int M = 6009; const int INF = 0x3f3f3f3f; int n,m; struct LT{ int nex,to; } L[M]; int F ,cnt; void add(int f,int t) { L[cnt].nex = F[f]; L[cnt].to = t; F[f] =cnt++; } void init() { scanf("%d%d",&n,&m); memset(F,0,sizeof(F)); cnt =1; int f,t; for(int i=0;i<m;i++) scanf("%d%d",&f,&t),add(f,t); } int dfn ,low ,post ,col ,color,ind; stack<int> S; void tarjan(int k) { dfn[k] = low[k] = ind++; post[k] = 1;S.push(k); for(int i=F[k];i;i=L[i].nex) { int to = L[i].to; if(!dfn[to]) { tarjan(to); low[k] = min(low[k],low[to]); }else if(post[to]&&dfn[to]<low[k]) { low[k] = dfn[to]; } } if(low[k]==dfn[k]) { int i;color++; for(i=S.top(),S.pop();i!=k;i=S.top(),S.pop()) { post[i] = 0,col[i] = color; } post[k] = 0;col[k] = color; } } struct node{ int nex,to; } tL[M]; int tF ,tcnt; int in ; void tadd(int f,int t) { tL[tcnt].nex = tF[f]; tL[tcnt].to = t; tF[f] = tcnt++; } void solve() { memset(dfn,0,sizeof(dfn)); color = 0; ind = 1; for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i); memset(tF,0,sizeof(tF)); memset(in,0,sizeof(in)); tcnt =1; for(int i=1;i<=n;i++) for(int j=F[i];j;j=L[j].nex) { int to = L[j].to; if(col[i]!=col[to]) { tadd(col[i],col[to]); } } memset(in,0,sizeof(in)); for(int i=1;i<tcnt;i++) in[tL[i].to]++; int ans =0,v=-1; for(int j=1;j<color;j++) {//for(int i=1;i<=color;i++) cout<<in[i]<<" ";cout<<endl; ans =0; for(int i=1;i<=color;i++) if(!in[i]) { ans++;in[i] = INF;v=i; } if(ans>1) { printf("No\n");return ; } for(int i=tF[v];i;i=tL[i].nex) { in[tL[i].to]--; } } printf("Yes\n"); } int main() { freopen("in.txt","r",stdin); int cas; scanf("%d",&cas); while(cas--) { init(); solve(); } return 0; }
相关文章推荐
- POJ2762 Going from u to v or from v to u? 强连通 Tarjan缩点+拓扑排序topsort
- POJ 2762 Going from u to v or from v to u? (图论-tarjan)
- Going from u to v or from v to u? - POJ 2762 Tarjan+拓扑排序
- POJ - 2762 Going from u to v or from v to u? (Tarjan 缩点)
- [ tarjan + dfs ] poj 2762 Going from u to v or from v to u?
- POJ 2762 Going from u to v or from v to u? (Tarjan) - from lanshui_Yang
- poj 2762 Going from u to v or from v to u? 【判断图是否为弱连通】 【tarjan求SCC + 缩点 + 拓扑排序】
- POJ-2762 Going from u to v or from v to u? (强连通分量[Tarjan]&&(拓扑排序||树形DP))
- POJ 2762 Going from u to v or from v to u? (Tarjan) - from lanshui_Yang
- [ tarjan + dfs ] poj 2762 Going from u to v or from v to u?
- 【POJ 2762】Going from u to v or from u ? (Tarjan + 拓扑排序)
- POJ 2762 Going from u to v or ... 弱连通图 tarjan
- POJ 2762 Going from u to v or from v to u?(Tarjan + 拓扑排序)
- POJ 2762 Going from u to v or ... 弱连通图 tarjan
- poj 2762 Going from u to v or v to u (tarjan+缩点+dfs搜索)
- [练习][poj2762]tarjan缩点 Going from u to v or from v to u?
- 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缩点+判断链
- 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?