这道题要判断一张有向图是否是单连通图,即图中是否任意两点u和v都存在u到v或v到u的路径。

方法是,找出图中所有强连通分量,强连通分量上的点肯定也是满足单连通性的,然后对强连通分量进行缩点,缩点后就变成DAG。

现在问题就变成,如何判断DAG是否是单连通图——用拓扑排序——如果拓扑排序过程中出现1个以上入度为0的点那就不是单连通图,因为有2个入度0的点,那这两个点肯定都无法到达对方。

另外,注意题目没说给的图是连通的!。。

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<queue>
  4. #include<algorithm>
  5. using namespace std;
  6. #define MAXN 1111
  7. #define MAXM 6666
  8. struct Edge{
  9. int u,v,next;
  10. }edge[MAXM];
  11. int NE,head[MAXN];
  12. void addEdge(int u,int v){
  13. edge[NE].u=u; edge[NE].v=v; edge[NE].next=head[u];
  14. head[u]=NE++;
  15. }
  16.  
  17. int belong[MAXN],bn,stack[MAXN],top;
  18. bool instack[MAXN];
  19. int dn,dfn[MAXN],low[MAXN];
  20. void dfs(int u){
  21. dfn[u]=low[u]=++dn;
  22. stack[++top]=u; instack[u]=;
  23. for(int i=head[u]; i!=-; i=edge[i].next){
  24. int v=edge[i].v;
  25. if(dfn[v]==){
  26. dfs(v);
  27. low[u]=min(low[u],low[v]);
  28. }else if(instack[v]){
  29. low[u]=min(low[u],dfn[v]);
  30. }
  31. }
  32. if(dfn[u]==low[u]){
  33. int v; ++bn;
  34. do{
  35. v=stack[top--];
  36. belong[v]=bn;
  37. instack[v]=;
  38. }while(u!=v);
  39. }
  40. }
  41.  
  42. int deg[MAXN];
  43. bool toposort(){
  44. queue<int> que;
  45. for(int i=; i<=bn; ++i){
  46. if(deg[i]==) que.push(i);
  47. }
  48. if(que.size()>) return ;
  49. while(!que.empty()){
  50. int u=que.front(); que.pop();
  51. for(int i=head[u]; i!=-; i=edge[i].next){
  52. int v=edge[i].v;
  53. if(--deg[v]==) que.push(v);
  54. }
  55. if(que.size()>) return ;
  56. }
  57. return ;
  58. }
  59. int main(){
  60. int t,n,m,a,b;
  61. scanf("%d",&t);
  62. while(t--){
  63. NE=;
  64. memset(head,-,sizeof(head));
  65. scanf("%d%d",&n,&m);
  66. while(m--){
  67. scanf("%d%d",&a,&b);
  68. addEdge(a,b);
  69. }
  70.  
  71. top=bn=dn=;
  72. memset(instack,,sizeof(instack));
  73. memset(dfn,,sizeof(dfn));
  74. for(int i=; i<=n; ++i){
  75. if(dfn[i]==) dfs(i);
  76. }
  77.  
  78. int tmp=NE; NE=;
  79. memset(head,-,sizeof(head));
  80. memset(deg,,sizeof(deg));
  81. for(int i=; i<tmp; ++i){
  82. int u=belong[edge[i].u],v=belong[edge[i].v];
  83. if(u==v) continue;
  84. addEdge(u,v);
  85. ++deg[v];
  86. }
  87. if(toposort()) puts("Yes");
  88. else puts("No");
  89. }
  90. return ;
  91. }

POJ2762 Going from u to v or from v to u?(判定单连通图:强连通分量+缩点+拓扑排序)的更多相关文章

  1. 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. ...

  2. 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 ...

  3. POJ2762 Going from u to v or from v to u? 强连通分量缩点+拓扑排序

    题目链接:https://vjudge.net/contest/295959#problem/I 或者 http://poj.org/problem?id=2762 题意:输入多组样例,输入n个点和m ...

  4. 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:  ...

  5. Going from u to v or from v to u?_POJ2762强连通+并查集缩点+拓扑排序

         Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K       Description I ...

  6. POJ2762 单向连通图(缩点+拓扑排序

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19552 ...

  7. POJ 2762Going from u to v or from v to u?(强联通 + 缩点 + 拓扑排序)

    [题意]: 有N个房间,M条有向边,问能否毫无顾虑的随机选两个点x, y,使从①x到达y,或者,②从y到达x,一定至少有一条成立.注意是或者,不是且. [思路]: 先考虑,x->y或者y-> ...

  8. Java实现判断单联通(强连通缩点+拓扑排序)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 ...

  9. poj2762 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: 13040 ...

随机推荐

  1. 淘宝(阿里百川)手机客户端开发日记第六篇 Service详解(六)

    Service和Thread的关系 不少初学者都可能会有这样的疑惑,Service和Thread到底有什么关系呢?什么时候应该用Service,什么时候又应该用Thread? 答案是Service和T ...

  2. magic-encoding

    (文章都是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) 今天页面跳转都出问题了,各种方法都试过了, log里说语法错误,问了pp,他说是汉字的原因...果 ...

  3. 在link的url里新增参数

    (文章都是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) <%= link_to image_tag("/images/icons/aaa. ...

  4. CSS 确定选中变红色

    textarea:focus { border: 1px solid #f4645f; outline: none; } blockquote { border-left: 4px solid #f4 ...

  5. PHP很有用的一个函数ignore_user_abort ()

    PHP很有用的一个函数ignore_user_abort () 2013-01-16 14:21:31|  分类: PHP |  标签:php  函数  |举报|字号 订阅     ignore_us ...

  6. rocksdb 编译安装 日志

    Compilation RocksDB's library should be able to compile without any dependency installed, although w ...

  7. Scrapy and Selenium

    How to scrapy js? scrapy结合webkit抓取js生成的页面 http://www.cnblogs.com/Safe3/archive/2011/10/19/2217965.ht ...

  8. less,sass,stylus配置和应用教程及三者比较

    less,sass,stylus配置和应用教程及三者比较  Less 1. 定义: Less是CSS预处理语言,在css基础之上增加了诸如变量,混合(mix),继承,运算,函数等功能,LESS既可以运 ...

  9. k Sum | & ||

    k Sum Given n distinct positive integers, integer k (k <= n) and a number target. Find k numbers ...

  10. php的socket通信(二)

    案例一:代码详解 // 设置一些基本的变量$host = "192.168.1.99";$port = 1234;// 设置超时时间set_time_limit(0);// 创建一 ...