poj-2762-Going from u to v or from v to u?-tarjan算法求缩点+算是不是一字链
来源:互联网 发布:常用的网络拓扑结构是 编辑:程序博客网 时间:2024/06/06 02:48
tarjan求缩点,然后算缩点之后的图是不是一字链。
判断是不是一字链很简单,直接dfs求出一条最长边。
看最长边是不是等于缩点之后的数目即可。
#include <iostream>#include<algorithm>#include<stdio.h>#include<string.h>#include<stdlib.h>#include<queue>#include<stack>#include<math.h>using namespace std;#define maxm 10000#define maxn 1100#define eps 0.000001#define zero(x) ((fabs(x)<eps?0:x))#define INF 99999999struct gra{ struct list { int u,v,next; } edge[maxm]; int head[maxn]; int vis[maxn]; int num; int n; int id[maxn]; int od[maxn]; int shuyu[maxn]; int nums; int viss[maxn]; int dnf[maxn],low[maxn],times,instack[maxn]; stack<int>qq; void init() { memset(head,-1,sizeof(head)); memset(vis,0,sizeof(vis)); memset(shuyu,0,sizeof(shuyu)); memset(dnf,0,sizeof(dnf)); memset(low,0,sizeof(low)); memset(instack,0,sizeof(instack)); memset(viss,0,sizeof(viss)); while(!qq.empty())qq.pop(); num=0; nums=1; times=1; } void add(int u,int v) { edge[num].u=u; edge[num].v=v; edge[num].next=head[u]; head[u]=num++; } void tarjan(int x) { dnf[x]=low[x]=times++; instack[x]=1; qq.push(x); for(int i=head[x]; i!=-1; i=edge[i].next) { int y=edge[i].v; if(!dnf[y]) { tarjan(y); low[x]=min(low[x],low[y]); } else if(instack[y]) { low[x]=min(low[x],dnf[y]); } } if(low[x]==dnf[x]) { int y=-1; while(x!=y) { y=qq.top(); shuyu[y]=nums; qq.pop(); instack[y]=0; } nums++; } } void start() { for(int i=1; i<=n; i++) { if(!dnf[i])tarjan(i); } } int dfs(int x,int p) { int mm=p; for(int i=head[x]; i!=-1; i=edge[i].next) { mm=max(mm,dfs(edge[i].v,p+1)); } return mm; }} G,g;int main(){ int n,i,j,m,a,b,T; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); G.init(); g.init(); G.n=n; for(i=1; i<=m; i++) { scanf("%d%d",&a,&b); G.add(a,b); } G.start(); g.n=G.nums; int nin,nout; nin=nout=0; for(i=1; i<=G.n; i++) { for(j=G.head[i]; j!=-1; j=G.edge[j].next) { int u=G.edge[j].u; int v=G.edge[j].v; if(G.shuyu[u]==G.shuyu[v])continue; G.id[G.shuyu[v]]++; G.od[G.shuyu[u]]++; // cout<<G.shuyu[u]<<" - "<<G.shuyu[v]<<endl; g.add(G.shuyu[u],G.shuyu[v]); } } for(i=1; i<G.nums; i++) { if(G.id[i]==0)nin++; if(G.od[i]==0)nout++; } if(nin>1||nout>1) { cout<<"No"<<endl; continue; } for(i=1; i<G.nums; i++) { if(G.id[i]==0)break; } // cout<<g.dfs(i,1)<<endl; if(g.dfs(i,1)==G.nums-1)cout<<"Yes"<<endl; else cout<<"No"<<endl; } return 0;}
0 0
- 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? (Tarjan) - from lanshui_Yang
- 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?(tarjan+拓扑排序)
- [ tarjan + dfs ] poj 2762 Going from u to v or from v to u?
- 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+拓扑排序)
- 【POJ 2762】Going from u to v or from u ? (Tarjan + 拓扑排序)
- POJ 2762 Going from u to v or from v to u? 图的单连通性 tarjan or kosaraju
- 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?
- 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
- poj 2762 Going from u to v or from v to u?
- hdu 3328 Flipper
- ZOJ 3769 Diablo III
- 设计模式总结
- 进来看看吧
- C 语言指针与地址
- poj-2762-Going from u to v or from v to u?-tarjan算法求缩点+算是不是一字链
- 我和我的输入提示--格式化输入文件
- Ubuntu安装单机1.2.1hadoop与伪分布式模式进行WordCount实验
- hdu 1558 Segment set 线段相交+并查集
- Hadoop YARN配置参数剖析(4)—Fair Scheduler相关参数
- @InitBinder的使用
- 继承System.Web.UI.Page的页面基类
- 主设备号、次设备号
- 英语学习利器,后悔后知后觉了--------------------韦氏三宝