POJ 2762 Going from u to v or from v to u? (判断弱连通)

http://poj.org/problem?id=2762

题意:
给出有向图,判断任意两个点u和v,是否可以从u到v或者从v到u。

思路:

判断图是否是弱连通的。

首先来一遍强连通缩点,重新建立新图,接下来我们在新图中找入度为0的点,入度为0的点只能有1个,如果有多个那么这些个点肯定是不能相互到达的。

如果只有一个入度为0的点,走一遍dfs判断新图是否是单链,如果有分支,那么分支上的点肯定是不能相互到达的。

  1 #include<iostream>
  2 #include<algorithm>
  3 #include<cstring>
  4 #include<cstdio>
  5 #include<vector>
  6 #include<stack>
  7 #include<queue>
  8 #include<cmath>
  9 #include<map>
 10 using namespace std;
 11 typedef long long LL;
 12
 13 const int maxn=6000+5;
 14
 15 int n,m;
 16
 17 int x[maxn],y[maxn];
 18 int in[maxn];
 19 int flag;
 20
 21 vector<int> G[maxn];
 22 vector<int> new_G[maxn];
 23
 24 stack<int> S;
 25 int pre[maxn],lowlink[maxn],sccno[maxn],dfs_clock,scc_cnt;
 26
 27 void dfs(int u)
 28 {
 29     pre[u] = lowlink[u] = ++dfs_clock;
 30     S.push(u);
 31     for (int i = 0; i < G[u].size(); i++)
 32     {
 33         int v = G[u][i];
 34         if (!pre[v])
 35         {
 36             dfs(v);
 37             lowlink[u] = min(lowlink[u], lowlink[v]);
 38         }
 39         else if(!sccno[v])
 40         lowlink[u] = min(lowlink[u], pre[v]);
 41     }
 42     if (pre[u] == lowlink[u])
 43     {
 44         scc_cnt++;
 45         for(;;)
 46         {
 47             int x = S.top(); S.pop();
 48             sccno[x] = scc_cnt;
 49             if (x == u) break;
 50         }
 51     }
 52 }
 53
 54 void find_scc(int n)
 55 {
 56     dfs_clock = scc_cnt = 0;
 57     memset(pre, 0, sizeof(pre));
 58     memset(sccno, 0, sizeof(sccno));
 59     for (int i = 1; i <= n; i++)
 60         if (!pre[i]) dfs(i);
 61 }
 62
 63
 64 void dfs2(int u)
 65 {
 66     if(flag==0)  return;
 67     if(new_G[u].size()>1)  {flag=0;return;}
 68     if(new_G[u].size()!=0) dfs2(new_G[u][0]);
 69 }
 70
 71
 72 void rebulid()
 73 {
 74     for(int i=1;i<=scc_cnt;i++)  {new_G[i].clear();in[i]=0;}
 75     for(int i=0;i<m;i++)
 76     {
 77         if(sccno[x[i]]!=sccno[y[i]])
 78         {
 79             new_G[sccno[x[i]]].push_back(sccno[y[i]]);
 80             in[sccno[y[i]]]=1;
 81         }
 82     }
 83     int num=0;
 84     int pos;
 85     for(int i=1;i<=scc_cnt;i++)
 86     {
 87         if(in[i]==0)  {num++;pos=i;}
 88         if(num>=2)  {flag=0;return;}
 89     }
 90     if(flag)  dfs2(pos);
 91 }
 92
 93 int main()
 94 {
 95    //freopen("D:\\input.txt","r",stdin);
 96    int T;
 97    scanf("%d",&T);
 98    while(T--)
 99    {
100        flag=1;
101        scanf("%d%d",&n,&m);
102        for(int i=1;i<=n;i++)  G[i].clear();
103        for(int i=0;i<m;i++)
104        {
105            scanf("%d%d",&x[i],&y[i]);
106            G[x[i]].push_back(y[i]);
107        }
108        find_scc(n);
109        rebulid();
110        if(flag)  puts("Yes");
111        else puts("No");
112    }
113    return 0;
114 }
时间: 2024-10-27 14:06:32

POJ 2762 Going from u to v or from v to u? (判断弱连通)的相关文章

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

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

POJ 2762 Going from u to v or from v to u? (有向图求单连通性)

POJ 2762 Going from u to v or from v to u? 链接:http://poj.org/problem?id=2762 题意:为了让他们的儿子变得更勇敢些,Jiajia 和Wind 将他们带到一个大洞穴中.洞穴中有n 个房间,有一些单向的通道连接某些房间.每次,Wind 选择两个房间x 和y,要求他们的一个儿子从一个房间走到另一个房间,这个儿子可以从x 走到y,也可以从y 走到x.Wind 保证她布置的任务是可以完成的,但她确实不知道如何判断一个任务是否可以完成

[ 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 Limit: 65536K Total Submissions: 14546   Accepted: 3837 Description In order to make their sons brave, Jiajia and Wind take them to a big cave. The cav

poj 2762 Going from u to v or from v to u? (判断是否是弱联通图)

题意:给定一个有向图有m条单向边,判断是否任意两点都可达(a能到b或者b能到a或者互相可达),即求 弱联通分量. 算法: 先缩点求强连通分量.然后重新建图,判断新图是否是一条单链,即不能分叉,如果分叉了就会存在不可达的情况. 怎么判断是否是单链呢? 就是每次入度为0的点都只有一个,即每次队列里只有一个点. (    o(╯□╰)o.....好像已经是第二次用pair记录原图的点对,然后存pair的vector忘记清空导致wa来wa去! ) #include<cstdio> #include&l

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: 65536K Total Submissions: 14573   Accepted: 3849 Description In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has

POJ 2762 Going from u to v or from v to u? (图论-tarjan)

Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14469   Accepted: 3801 Description 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

[强连通分量] 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   Accepted: 4590 Description 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

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: 15812   Accepted: 4194 Description 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

poj 2762 Going from u to v or from v to u? trajan+拓扑

Going from u to v or from v to u? Description 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 thei