POJ 2762 tarjan缩点+拓扑

Going from u to v or from v to u?

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 14566   Accepted: 3846

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
3 3
1 2
2 3
3 1

Sample Output

Yes

题目意思:给出一个结点数目为n,边数为m的有向图,若随意选出两个结点x、y,若能从x到y或者(注意,不是而且)y到x,输出Yes,否则输出No。

思路:在强连通里肯定成立,若不成立,肯定在两个强连通分量里面,那么狠容易想到缩点分块了。画了几个图后发现若分块后的图为单链那么肯定成立,若不是单链即有分叉的话那么不成立,自然而然想到拓扑了,每次删除一个入度为0的点,就看一下入度为0的点为多少个,若>1则不成立。

代码:
  1 #include <cstdio>
  2 #include <cstring>
  3 #include <algorithm>
  4 #include <iostream>
  5 #include <vector>
  6 #include <queue>
  7 #include <stack>
  8 using namespace std;
  9
 10 #define N 10005
 11
 12 int low[N], dfn[N], dfn_clock;
 13 int instack[N], a[N], in[N];
 14 int n, m;
 15 stack<int>st;
 16 vector<int>ve[N],out[N];
 17 int ans, num;
 18
 19 void init(){
 20     int i;
 21     for(i=0;i<=n;i++){
 22         ve[i].clear();instack[i]=1;
 23         out[i].clear();
 24     }
 25     memset(dfn,0,sizeof(dfn));
 26     memset(in,0,sizeof(in));
 27 }
 28
 29 void tarjan(int u){
 30     int v, i, j;
 31     dfn[u]=low[u]=dfn_clock++;
 32     st.push(u);
 33     for(i=0;i<ve[u].size();i++){
 34         v=ve[u][i];
 35         if(!dfn[v]){
 36             tarjan(v);
 37             low[u]=min(low[u],low[v]);
 38         }
 39         else if(instack[v]){
 40             low[u]=min(low[u],dfn[v]);
 41         }
 42     }
 43     if(low[u]==dfn[u]){
 44         ans++;
 45         while(1){
 46             v=st.top();st.pop();
 47             instack[v]=0;
 48             a[v]=ans;
 49             if(v==u){
 50                 break;
 51             }
 52         }
 53     }
 54 }
 55
 56 main()
 57 {
 58     int i, j, k;
 59     int x, y;
 60     int t;
 61     cin>>t;
 62
 63     while(t--){
 64         scanf("%d %d",&n,&m);
 65         init();
 66         while(m--){
 67             scanf("%d %d",&x,&y);
 68             ve[x].push_back(y);
 69         }
 70         dfn_clock=1;ans=0;
 71         for(i=1;i<=n;i++){
 72             if(!dfn[i])
 73             tarjan(i);
 74         }
 75         for(i=1;i<=n;i++){
 76             for(j=0;j<ve[i].size();j++){
 77                 x=ve[i][j];
 78                 if(a[x]!=a[i]){
 79                     in[a[x]]++;
 80                     out[a[i]].push_back(a[x]);
 81                 }
 82             }
 83         }
 84         int aa, f;
 85     //    printf("%d\n",ans);
 86 //        for(i=1;i<=n;i++){
 87     //        printf("%d ",a[i]);
 88     //    }
 89     //    cout<<endl;
 90         for(i=1;i<=ans;i++){
 91             aa=0;f=0;
 92             for(j=1;j<=ans;j++){
 93                 if(!in[j]) aa++;
 94                 if(!in[j]&&!f){
 95                     for(k=0;k<out[j].size();k++){
 96                         in[out[j][k]]--;
 97                     }
 98                     in[j]=-1;f=1;
 99                 }
100
101             }
102
103             if(aa>1) break;
104
105         }
106         if(aa<=1) printf("Yes\n");
107         else printf("No\n");
108     }
109 }
时间: 2024-10-20 11:32:58

POJ 2762 tarjan缩点+拓扑的相关文章

POJ 2762 tarjan缩点+并查集+度数

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

POJ 2672 Tarjan + 缩点 + 拓扑思想

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

UVA 11324.The Largest Clique tarjan缩点+拓扑dp

题目链接:https://vjudge.net/problem/UVA-11324 题意:求一个有向图中结点数最大的结点集,使得该结点集中任意两个结点u和v满足:要目u可以到达v,要么v可以到达u(相互可达也可以). 思路:同一个强联通分量中满足结点集中任意两个结点u和v满足:要目u可以到达v,要么v可以到达u(相互可达也可以).把强联通分量收缩点后得到scc图,让每个scc结点的权值等于他的结点数,则求scc图上权最大的路径.拓扑dp,也可以直接bfs,但是要建立一个新的起点,连接所有入度为0

[模板]tarjan缩点+拓扑排序

题目:给定一个n个点m条边有向图,每个点有一个权值,求一条路径,使路径经过的点权值之和最大.你只需要求出这个权值和. 允许多次经过一条边或者一个点,但是,重复经过的点,权值只计算一次. 题目简述:先tarjan缩点,再从入度为零处进行一次拓扑排序,求最长路即可,话说拓扑排序求最长路真方便... 注意: 要明确拓扑的写法,要用栈写最优. 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define man 100010 4 inline i

[ZJOI2007]最大半连通子图 (Tarjan缩点,拓扑排序,DP)

题目链接 Solution 大概是个裸题. 可以考虑到,如果原图是一个有向无环图,那么其最大半联通子图就是最长的一条路. 于是直接 \(Tarjan\) 缩完点之后跑拓扑序 DP就好了. 同时由于是拓扑序DP,要去掉所有的重边. Code #include<bits/stdc++.h> #define ll long long using namespace std; const int maxn=100008; struct sj{int to,next;}a[maxn*10]; ll mo

bzoj5017 [Snoi2017]炸弹 (线段树优化建图+)tarjan 缩点+拓扑排序

题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=5017 题解 这个题目方法挺多的. 线段树优化建图 线段树优化建图的做法应该挺显然的,一个炸弹能够引爆的炸弹的显然应该是一个区间里面的,直接对这个区间进行线段树优化建图. 这样可以得到一个带环图,缩点以后这个炸弹能够炸到的炸弹就是从这个点能够走到的点. 但是这个不太好做,不过可以发现最终的炸弹也是一个区间,所以可以通过缩点后的 DAG 来求出左右端点. 时间复杂度 \(O(n\log n)\)

HDU 6165 FFF at Valentine(Tarjan缩点+拓扑排序)

FFF at Valentine Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 575    Accepted Submission(s): 281 Problem Description At Valentine's eve, Shylock and Lucar were enjoying their time as any oth

BZOJ 1512 [POI2006]Pro-Professor Szu Tarjan缩点+拓扑DP

题意: n个别墅以及一个主建筑楼,从每个别墅都有很多种不同方式走到主建筑楼,其中不同的定义是(每条边可以走多次,如果走边的顺序有一条不同即称两方式不同). 询问最多的不同方式是多少,以及有多少个别墅有这么多方式,按照顺序输出别墅编号. 如果最多不同方式超过了36500那么都视作zawsze 解析: 容易想到把边反向,问题转化成求从主建筑楼走向各个点的方案数. 对于一个强连通分量,显然我们可以看做是一个点,所以首先把图缩点. 缩点之后 我们设f[i]表示走到第i个点的方案数. 显然f[i]=∑f[

POJ 1236 tarjan缩点+度数

Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11441   Accepted: 4554 Description A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a li