Going from u to v or from v to u?
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 15494   Accepted: 4100

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

 
题目意思:
给一n个点、m条边的有向图,问是否随意选两个点u和v,是否能从u到达v或者从v到达u。
 
思路:
如果弱连通分量有多个,那么肯定是不可到达的,所以用并查集处理一下。
强连通分量内部随意两个点是互相到达的,不互相到达的点在强连通分量之间,所以先用tarjan强连通分量缩点,若入度为0的个数>=2||出度为0的个数>=2那么也是不可到达的。
各种条件判断一下即可。
 
代码:
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <vector>
  6. #include <queue>
  7. #include <cmath>
  8. #include <set>
  9. #include <stack>
  10. using namespace std;
  11.  
  12. #define N 1005
  13.  
  14. int max(int x,int y){return x>y?x:y;}
  15. int min(int x,int y){return x<y?x:y;}
  16. int abs(int x,int y){return x<?-x:x;}
  17.  
  18. int n, m;
  19. bool in[N];
  20. int suo[N];
  21. int cnt;
  22. vector<int>ve[N];
  23. int dfn[N], low[N], Time;
  24. stack<int>st;
  25. int father[N];
  26.  
  27. int findroot(int u){
  28. if(father[u]!=u) father[u]=findroot(father[u]);
  29. return father[u];
  30. }
  31.  
  32. void tarjan(int u){
  33. dfn[u]=low[u]=Time++;
  34. st.push(u);in[u]=true;
  35. int i, j, k;
  36. for(i=;i<ve[u].size();i++){
  37. int v=ve[u][i];
  38. if(dfn[v]==-){
  39. tarjan(v);
  40. low[u]=min(low[u],low[v]);
  41. }
  42. else if(in[v]) low[u]=min(low[u],dfn[v]);
  43. }
  44. if(dfn[u]==low[u]){
  45. while(){
  46. int x=st.top();
  47. suo[x]=cnt;
  48. in[x]=false;
  49. st.pop();
  50. if(x==u||st.empty()) break;
  51. }
  52. cnt++;
  53. }
  54. }
  55.  
  56. main()
  57. {
  58. int i, j, k, u, v;
  59. int t;
  60. cin>>t;
  61.  
  62. while(t--){
  63. scanf("%d %d",&n,&m);
  64. for(i=;i<=n;i++) ve[i].clear();
  65. for(i=;i<m;i++){
  66. scanf("%d %d",&u,&v);
  67. ve[u].push_back(v);
  68. }
  69. Time=cnt=;
  70. memset(dfn,-,sizeof(dfn));
  71. while(!st.empty()) st.pop();
  72. memset(in,false,sizeof(in));
  73. for(i=;i<=n;i++){
  74. if(dfn[i]==-){
  75. tarjan(i);
  76. }
  77. }
  78. int inn[N], out[N];
  79. memset(inn,,sizeof(inn));
  80. memset(out,,sizeof(out));
  81. for(i=;i<=n;i++) father[i]=i;
  82. for(i=;i<=n;i++){
  83. for(j=;j<ve[i].size();j++){
  84. int u=suo[i], v=suo[ve[i][j]];
  85. if(u!=v){
  86. father[findroot(v)]=findroot(u);
  87. inn[v]++;
  88. out[u]++;
  89. }
  90. }
  91. }
  92.  
  93. int num=;
  94. for(i=;i<cnt;i++){
  95. if(father[i]==i) num++;
  96. }
  97. if(num>) {
  98. printf("No\n");continue;
  99. }
  100.  
  101. int n1, n2;
  102. n1=n2=;
  103. for(i=;i<cnt;i++){
  104. if(!inn[i]) n1++;
  105. if(!out[i]) n2++;
  106. }
  107. if(n1>||n2>) printf("No\n");
  108. else printf("Yes\n");
  109. }
  110. }

POJ 2762 tarjan缩点+并查集+度数的更多相关文章

  1. poj 2762(tarjan缩点+判断是否是单链)

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

  2. poj 2762 tarjan缩点+拓扑序

    2013-09-08 10:00 var m, n :longint; t :longint; f, last :..] of longint; pre, other :..] of longint; ...

  3. poj 2236:Wireless Network(并查集,提高题)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 16065   Accepted: 677 ...

  4. 《程序员代码面试指南》第三章 二叉树问题 Tarjan算法与并查集解决二叉树节点间最近公共祖先的批量查询问题

    题目待续.... Tarjan算法与并查集解决二叉树节点间最近公共祖先的批量查询问题 java代码

  5. POJ 3694 (tarjan缩点+LCA+并查集)

    好久没写过这么长的代码了,题解东哥讲了那么多,并查集优化还是很厉害的,赶快做做前几天碰到的相似的题. #include <iostream> #include <algorithm& ...

  6. poj 1182:食物链(种类并查集,食物链问题)

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44168   Accepted: 12878 Description ...

  7. POJ 1456 Supermarket 区间问题并查集||贪心

    F - Supermarket Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  8. POJ 1182 食物链(种类并查集)

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63592   Accepted: 18670 Description ...

  9. POJ 1182 (经典食物链 /并查集扩展)

    (參考他人资料) 向量偏移--由"食物链"引发的总结 http://poj.org/problem?id=1182这道食物链题目是并查集的变型.非常久曾经做的一次是水过的,这次 ...

随机推荐

  1. Google账户_GooglePlay_关联

    PS:这过程叫一个折腾...穷逼的无奈啊... 1. 1.1. 网上搜到说,安装 google play & google框架服务,手机需要root,没去证实这个事情... 用了之前的 And ...

  2. POJ2676,HDU4069解决数独的两种实现:DFS、DLX

    搜索实现:解决数独有两种思考策略,一种是枚举当前格能填的数字的种数,这里有一优化策略就是先搜索能填入种数小的格子:另一种是考虑处理某一行(列.宫)时,对于某一个没用过的数字,若该行(列.宫)只有一个可 ...

  3. ean13码的生成,python读取csv中数据并处理返回并写入到另一个csv文件中

    # -*- coding: utf-8 -*- import math import re import csv import repr def ean_checksum(eancode): &quo ...

  4. php提示:Call to undefined function curl_init

    我要利用curl函数进行数据采集时发现提示Call to undefined function curl_init错误了,后来从官网了解到因为curl默认不是php开启的函数,我们需要手工打开哦,下面 ...

  5. TortoiseSvn

    TortoiseSVN 是svn版本控制系统的一个免费开源客户端,它是svn版本控制的 Windows 扩展.可以使你避免使用枯燥而且不方便的命令行.它完全嵌入 Windows Explorer,使用 ...

  6. 一些android系统参数的获取

    //获取网络类型 2G/3G/WIFI public String getNetworkType(){ String mNetWorkType = ""; Connectivity ...

  7. C++——友元、异常和其他

    一.友元 类并非只能拥有友元函数,也可以将类作为友元.在这种情况下,友元类的所有方法都可以访问原始类的私有成员和保护成员.另外,也可以做更严格的限制,只将特定的成员函数指定为另一个类的友元.哪些函数. ...

  8. js 重点 (转载)

  9. 【CDN】海外免费加速CDN:Incapsula,CloudFare

    最近服务器要搬迁到香港,因为后续有国外用户使用,基于此要使用大陆和海外都比较好的cdn才好 一开始国外同事推荐CloudFare,后来看看效果开始使用Incapsula CloudFare 官网:ht ...

  10. Python开发者须知 —— Bottle框架常见的几个坑

    Bottle是一个小巧实用的python框架,整个框架只有一个几十K的文件,但却包含了路径映射.模板.简单的数据库访问等web框架组件,而且语法简单,部署方便,很受python开发者的青睐.Pytho ...