POJ 2762 Going from u to v or from v to u? Tarjan算法 学习例题

时间:2023-03-10 05:51:21
POJ  2762  Going from u to v or from v to u? Tarjan算法 学习例题
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
3 3
1 2
2 3
3 1

Sample Output

Yes

Source

POJ Monthly--2006.02.26,zgl & twb
题目大意:n个山洞,对于每两个山洞s,e,都满足s可以到达e或者e可以到达s,则输出Yes,否则输出No。(s到e或者e到s满足一个就可以)
 #include<iostream>
#include<cstring>
#include<cstdio>
#include<stack>
#define maxm 6010
#define maxn 1010
using namespace std;
int T,n,m;
struct edge{
int u,v,next;
}e[maxm],ee[maxm];
int head[maxn],js,headd[maxn],jss;
bool exist[maxn];
int visx,cur;// cur--缩出的点的数量
int dfn[maxn],low[maxn],belong[maxn];
int rudu[maxn],chudu[maxn];
stack<int>st;
void init(){
memset(rudu,,sizeof(rudu));memset(chudu,,sizeof(chudu));
memset(head,,sizeof(head));memset(headd,,sizeof(headd));
jss=js=visx=cur=;
memset(exist,false,sizeof(exist));
while(!st.empty())st.pop();
memset(dfn,-,sizeof(dfn));memset(low,-,sizeof(low));
memset(belong,,sizeof(belong));
}
void add_edge1(int u,int v){
e[++js].u=u;e[js].v=v;
e[js].next=head[u];head[u]=js;
}
void tarjan(int u){
dfn[u]=low[u]=++visx;
exist[u]=true;
st.push(u);
for(int i=head[u];i;i=e[i].next){
int v=e[i].v;
if(dfn[v]==-){
tarjan(v);
if(low[v]<low[u]) low[u]=low[v];
}
else if(exist[v]&&low[u]>dfn[v]) low[u]=dfn[v];
}
int j;
if(low[u]==dfn[u]){
++cur;
do{
j=st.top();st.pop();exist[j]=false;
belong[j]=cur;
}while(j!=u);
}
}
void add_edge2(int u,int v){
ee[++jss].u=u;ee[jss].v=v;
ee[jss].next=headd[u];headd[u]=jss;
}
bool topo()
{
int tp=,maxt=;
for(int i=;i<=cur;i++)
{
if(rudu[i]==){
tp++;
}
if(chudu[i]>maxt)maxt=chudu[i];
}
if(tp>)return ;// 入读等于0的缩点只能有一个 否则..
if(maxt>)return ;
return ;
}
int main()
{
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
init();
int u,v;
for(int i=;i<m;i++){
scanf("%d%d",&u,&v);add_edge1(u,v);
}
for(int i=;i<=n;i++){// 求强连通分量
if(dfn[i]==-) tarjan(i);
}
for(int i=;i<=js;i++){
int u=e[i].u,v=e[i].v;
if(belong[u]!=belong[v]){
add_edge2(belong[u],belong[v]);
rudu[belong[v]]++;chudu[belong[u]]++;
}
}
if(topo()==) printf("Yes\n");
else printf("No\n");
}
return ;
}

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