Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 17104   Accepted: 4594

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 their little sons go from one to the other. The son can either go from x to y, or from y to x. Wind promised that her tasks are all possible, but she actually doesn't know how to decide if a task is possible. To make her life easier, Jiajia decided to choose a cave in which every pair of rooms is a possible task. Given a cave, can you tell Jiajia whether Wind can randomly choose two rooms without worrying about anything?

Input

The first line contains a single integer T, the number of test cases. And followed T cases. 

The first line for each case contains two integers n, m(0 < n < 1001,m < 6000), the number of rooms and corridors in the cave. The next m lines each contains two integers u and v, indicating that there is a corridor connecting room u and room v directly. 

Output

The output should contain T lines. Write 'Yes' if the cave has the property stated above, or 'No' otherwise.

Sample Input

  1. 1
  2. 3 3
  3. 1 2
  4. 2 3
  5. 3 1

Sample Output

  1. Yes

Source

POJ Monthly--2006.02.26,zgl & twb
题目大意:n个山洞,对于每两个山洞s,e,都满足s可以到达e或者e可以到达s,则输出Yes,否则输出No。(s到e或者e到s满足一个就可以)
  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdio>
  4. #include<stack>
  5. #define maxm 6010
  6. #define maxn 1010
  7. using namespace std;
  8. int T,n,m;
  9. struct edge{
  10. int u,v,next;
  11. }e[maxm],ee[maxm];
  12. int head[maxn],js,headd[maxn],jss;
  13. bool exist[maxn];
  14. int visx,cur;// cur--缩出的点的数量
  15. int dfn[maxn],low[maxn],belong[maxn];
  16. int rudu[maxn],chudu[maxn];
  17. stack<int>st;
  18. void init(){
  19. memset(rudu,,sizeof(rudu));memset(chudu,,sizeof(chudu));
  20. memset(head,,sizeof(head));memset(headd,,sizeof(headd));
  21. jss=js=visx=cur=;
  22. memset(exist,false,sizeof(exist));
  23. while(!st.empty())st.pop();
  24. memset(dfn,-,sizeof(dfn));memset(low,-,sizeof(low));
  25. memset(belong,,sizeof(belong));
  26. }
  27. void add_edge1(int u,int v){
  28. e[++js].u=u;e[js].v=v;
  29. e[js].next=head[u];head[u]=js;
  30. }
  31. void tarjan(int u){
  32. dfn[u]=low[u]=++visx;
  33. exist[u]=true;
  34. st.push(u);
  35. for(int i=head[u];i;i=e[i].next){
  36. int v=e[i].v;
  37. if(dfn[v]==-){
  38. tarjan(v);
  39. if(low[v]<low[u]) low[u]=low[v];
  40. }
  41. else if(exist[v]&&low[u]>dfn[v]) low[u]=dfn[v];
  42. }
  43. int j;
  44. if(low[u]==dfn[u]){
  45. ++cur;
  46. do{
  47. j=st.top();st.pop();exist[j]=false;
  48. belong[j]=cur;
  49. }while(j!=u);
  50. }
  51. }
  52. void add_edge2(int u,int v){
  53. ee[++jss].u=u;ee[jss].v=v;
  54. ee[jss].next=headd[u];headd[u]=jss;
  55. }
  56. bool topo()
  57. {
  58. int tp=,maxt=;
  59. for(int i=;i<=cur;i++)
  60. {
  61. if(rudu[i]==){
  62. tp++;
  63. }
  64. if(chudu[i]>maxt)maxt=chudu[i];
  65. }
  66. if(tp>)return ;// 入读等于0的缩点只能有一个 否则..
  67. if(maxt>)return ;
  68. return ;
  69. }
  70. int main()
  71. {
  72. scanf("%d",&T);
  73. while(T--){
  74. scanf("%d%d",&n,&m);
  75. init();
  76. int u,v;
  77. for(int i=;i<m;i++){
  78. scanf("%d%d",&u,&v);add_edge1(u,v);
  79. }
  80. for(int i=;i<=n;i++){// 求强连通分量
  81. if(dfn[i]==-) tarjan(i);
  82. }
  83. for(int i=;i<=js;i++){
  84. int u=e[i].u,v=e[i].v;
  85. if(belong[u]!=belong[v]){
  86. add_edge2(belong[u],belong[v]);
  87. rudu[belong[v]]++;chudu[belong[u]]++;
  88. }
  89. }
  90. if(topo()==) printf("Yes\n");
  91. else printf("No\n");
  92. }
  93. return ;
  94. }

思路:首先建一个有向图,进行Tarjan算法,进行缩点,缩完点之后,再建一张有向图(这张图只能成一条链),对其进行检验,入度为0的店只能有一个或没有。。

POJ 2762 Going from u to v or from v to u? Tarjan算法 学习例题的更多相关文章

  1. KMP算法 学习例题 POJ 3461Oulipo

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37971   Accepted: 15286 Description The ...

  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?(强连通分量+缩点重构图+拓扑排序)

    http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit:  ...

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

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

  5. POJ 2762 Going from u to v or from v to u? (判断单连通)

    http://poj.org/problem?id=2762 题意:给出有向图,判断任意两个点u和v,是否可以从u到v或者从v到u. 思路: 判断图是否是单连通的. 首先来一遍强连通缩点,重新建立新图 ...

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

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

    id=2762">http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS ...

  8. [强连通分量] 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 ...

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

随机推荐

  1. 迅为IMX6Q开发板在道路交通信号控制系统解决方案中的应用

    智能交通综合管控平台是为交通指挥系统服务的统一信息平台,以信息技术为主导,以计算机通信网络和智能化指挥控制管理为基础,建成集高新技术应用为一体的智能化指挥调度集成平台,实现信息交换与共享.快速反应决策 ...

  2. bzoj 2658

    首先考虑容斥 我们计算出所有没有点在其中的矩形,然后用所有矩形减去这些矩形即可 然后考虑如何计算没有点在其中的矩形 采用扫描线的思想,从上向下一行一行扫,假设我们扫到的行编号是$a$,然后考虑如果左右 ...

  3. #include <> 和 #inlude ""的区别

    #include < >引用的是编译器的类库路径里面的头文件#include  " "引用的是你程序目录的相对路径中的头文件,在程序目录的相对路径中找不到该头文件时会继 ...

  4. 【dp】淘宝的推荐系统

    可能最近做二分和DFS做傻了? 小明刚刚入职淘宝,老大给他交代了一个简单的任务,实现一个简易的商品推荐系统. 这个商品推荐系统的需求如下: 一共有 n 件商品可以被推荐,他们的编号分别为 1 到 n. ...

  5. [LUOGU] 3959 宝藏

    https://www.luogu.org/problemnew/show/P3959 注意到n非常小,考虑状压/搜索. 发现状压需要枚举起点,跑n次,一个问题是转移不可以以数字大小为阶段了,考虑用d ...

  6. 介绍几款移动的WebAPP框架

    如果是 Angular 那就选 Ionic (一对好 CP)如果是 Vue 那就选 Vux (基于 WeUI)如果是 jQuery 那就选 Framework7 (iOS 和 Android 双皮肤) ...

  7. MariaDB数据库(五)

    1. MariaDB主从架构 1.1 概述 主从架构用来预防数据丢失.主从多用于网站架构,因为主从的同步机制是异步的,数据的同步有一定延迟,也就是说有可能会造成数据的丢失,但是性能比较好,因此网站大多 ...

  8. inotify+rsync sersync+rsync实时同步服务

    中小型网站搭建-数据实时的复制-inotify/sersync inotify是一种强大的,细粒度的.异步的文件系统事件监控机制(软件),linux内核从2.6.13起,加入inotify支持,通过i ...

  9. 【http】http协议的队首阻塞

    1 队首阻塞 就是需要排队,队首的事情没有处理完的时候,后面的人都要等着. 2 http1.0的队首阻塞 对于同一个tcp连接,所有的http1.0请求放入队列中,只有前一个请求的响应收到了,然后才能 ...

  10. SVN 如何提交 SO 库文件

    今天提交代码时候发现,svn add 还是 svn st 均查看不到想要提交的 so 文件. 后来才知道原来是配置文件出了问题,把so文件的提交给屏蔽掉了. 修改步骤如下: 1.Ubuntu 系统,点 ...