【题意】:

有N个房间,M条有向边,问能否毫无顾虑的随机选两个点x, y,使从①x到达y,或者,②从y到达x,一定至少有一条成立。注意是或者,不是且。

【思路】:

先考虑,x->y或者y->x是什么意思,如果是且的话就简单了,就直接判断整个图是不是强联通图即可,但是这道题是或,那么可以随手画出一个DAG

比如1->3, 2->3 这样很明显是不行的,1,2没有联通,那么如果是这样1->2->3 就可以了,或者是1->2->3->1,这样也是可以的。

很显然,整个图中某一时刻入度同时为0的点的数量num≤1即可以找出合理方案,反之当某一时刻num>1时则不能。

考虑到图不可能是3个点这么简单,可以先求出强联通分量,因为分量中的每个点都可以相互到达,然后将每个联通分量缩点,这样就不用分别考虑。然后

对于每个缩点的入度判断可以使用topo排序判断。到此完毕。

  1. #include <iostream>
  2. #include <cstring>
  3. #include <queue>
  4. #include <cstdio>
  5. using namespace std;
  6.  
  7. const int maxn = + ;
  8. const int maxm = + ;
  9. int n, m, t;
  10. struct edge{
  11. int to, next;
  12. } ed[maxm<<];
  13. int head[maxn<<], tot, cnt;
  14. int dfn[maxn], low[maxn], num, stak[maxn], c[maxn];
  15. int indu[maxn<<];
  16. bool instack[maxn], vis[maxn];
  17. inline void init(){
  18. memset( head ,-, sizeof(head) );
  19. memset( dfn, , sizeof(dfn) );
  20. memset( low, , sizeof(low) );
  21. memset( indu, , sizeof(indu) );
  22. memset( vis, , sizeof(vis) );
  23. tot = ;
  24. stak[] = cnt = num = ;
  25. }
  26.  
  27. inline void add( int u, int v ){
  28. ed[++tot].to = v;
  29. ed[tot].next = head[u];
  30. head[u] = tot;
  31. }
  32.  
  33. inline int min( int a, int b ){
  34. return a<b ? a:b;
  35. }
  36.  
  37. inline void tarjan( int x ){ //求强联通
  38. dfn[x] = low[x] = ++num;
  39. instack[x] = ;
  40. stak[++stak[]] = x;
  41. for( int i=head[x]; i!=-; i=ed[i].next ){
  42. int y = ed[i].to;
  43. if( !dfn[y] ){
  44. tarjan(y);
  45. low[x] = min(low[x], low[y]);
  46. }else if(instack[y]) low[x] = min(low[x], dfn[y]);
  47. }
  48. if( low[x]==dfn[x] ){
  49. ++cnt;
  50. do{
  51. int p = stak[stak[]];
  52. c[p] = cnt+n;
  53. instack[p] = ;
  54. } while(stak[stak[]--]!=x );
  55. }
  56. }
  57.  
  58. inline void scc( int x ){ //缩点
  59. if( vis[x] ) return;
  60. vis[x] = ;
  61. for( int i=head[x]; i!=-; i=ed[i].next ){
  62. int y = ed[i].to;
  63. if( c[x]!=c[y] ){
  64. add( c[x], c[y] );
  65. indu[c[y]] ++;
  66. }
  67. scc(y);
  68. }
  69. }
  70.  
  71. inline bool topo(){ //topo判断某一时刻有无多个点的入度同时为0
  72. queue<int> q;
  73. for( int i=; i<=cnt; i++ )
  74. if( !indu[i+n] ) q.push(i+n);
  75. if( q.size()> ) return ;
  76. while( !q.empty() ){
  77. int x = q.front(); q.pop();
  78. for( int i=head[x]; i!=-; i=ed[i].next ){
  79. int y =ed[i].to;
  80. indu[y]--;
  81. if(!indu[y]) q.push(y);
  82. if( q.size()> ) return ;
  83. }
  84. }
  85. return ;
  86. }
  87.  
  88. int main(){
  89. // freopen("in.txt", "r", stdin);
  90. scanf("%d", &t);
  91. while( t-- ){
  92. init();
  93. scanf("%d%d", &n, &m);
  94. for( int i=; i<m; i++ ){
  95. int u, v;
  96. scanf("%d%d", &u, &v);
  97. add( u, v );
  98. }
  99. for( int i=; i<=n; i++ )
  100. if( !dfn[i] ) tarjan(i);
  101. for( int i=; i<=n; i++ ) scc(i);
  102. if( topo() ) puts("Yes");
  103. else puts("No");
  104. }
  105.  
  106. return ;
  107. }

POJ 2762Going from u to v or from v to u?(强联通 + 缩点 + 拓扑排序)的更多相关文章

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

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

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

  4. POJ2762 Going from u to v or from v to u?(判定单连通图:强连通分量+缩点+拓扑排序)

    这道题要判断一张有向图是否是单连通图,即图中是否任意两点u和v都存在u到v或v到u的路径. 方法是,找出图中所有强连通分量,强连通分量上的点肯定也是满足单连通性的,然后对强连通分量进行缩点,缩点后就变 ...

  5. poj 2762 强连通缩点+拓扑排序

    这题搞了好久,先是拓扑排序这里没想到,一开始自己傻乎乎的跑去找每层出度为1的点,然后才想到能用拓扑排序来弄. 拓扑排序的时候也弄了挺久的,拓扑排序用的也不多. 题意:给一个图求是否从对于任意两个点能从 ...

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

  7. POJ 2186 Popular Cows(强联通+缩点)

    Description Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= ...

  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. POJ 2186 Popular Cows (强联通)

    id=2186">http://poj.org/problem? id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 655 ...

随机推荐

  1. 安装hadoop集群-cm 5.14.4

    环境 要求: centos 7 java 1.8 cm 5.14.4 mysql 5.7 4核16G 工具要求: mysql-connector-java-5.1.39.jar 1.绑定 hosts ...

  2. Orm 配置说明

    一.在线技术文档: http://files.cnblogs.com/files/humble/d.pdf   二.使用的大致流程   1.首先下载代码生成器,可以一键生成项目Model层;(其中含有 ...

  3. Linux查看端口使用情况

    1.netstat -tunlp,查看已使用的端口 2.netstat -tunlp | grep 8080,查询指定端口使用情况 3.netstat命令无法使用需要安装net-tools yum i ...

  4. C# 杀掉系统中的进程

    杀掉系统进程之前首先要知道进程名称(说了句废话),这里要注意在任务管理器中的进程名称不一定是真实的名称.打个比方,我们开启一个"记事本",任务管理器中进程名称为"记事本& ...

  5. C# 读取配置指定Config文件--亲测通过

    直接上代码: public class ConfigUtils { public static String GetKey(String configPath,String key) { Config ...

  6. [转帖]Kubernetes的部署策略

    Kubernetes的部署策略,你常用哪种? https://www.sohu.com/a/318731931_100159565?spm=smpc.author.fd-d.78.1574127778 ...

  7. poi根据excel模板导出Excel

    /****单元格值对象**/public class Cells { /*** * 行 */ private int row; /** * 列 */ private int column; /** * ...

  8. Apache信息头

    package org.apache.http; public final class HttpHeaders { public static final String ACCEPT = " ...

  9. 给Go程序加入编译版本时间等信息

    先看效果 $./myapp -v GitCommitLog=d97d098e5bb4ad38a2a7968f273a256e10a0108f mod bininfo comment GitStatus ...

  10. 解决v-html无法理解vue模版的问题-动态获取模版,动态插入app并使用当下app状态数据需求

    很多情况下,我们需要使用动态的html作为某个dom元素的inner html,如果这个内容是标准的html的话,则v-html能够圆满满足需求,而如果内容包含了vue组件,则使用v-html就不能达 ...