Going from u to v or from v to u?
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 14959 | Accepted: 3956 |
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.
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
题意:问是否对于任意两点,都存在(u,v)或者(v,u)的路线。
思路:首先Tarjan缩点,之后进行拓扑排序,若同时存在多个入度为0的点,就不能使其满足题意。
另外我RE了一下后,把数组都开大了10倍就过了,我看POJ上RE的次数也很多,可能是内部数据有问题吧。
AC代码如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
struct node
{
int v,next;
}edge[60100];
int T,t,n,m,Head[10010],DFN[10010],Low[10010],stack[10010],tot,scnt,cnt,top,Belong[10010];
bool instack[10010],flag;
int in[10010],out[10010],U[10010],V[10010];
queue<int> qu;
void init()
{
memset(Head,-1,sizeof(Head));
memset(DFN,0,sizeof(DFN));
tot=scnt=cnt=top=0;
}
void add(int u,int v)
{
edge[tot].v=v;
edge[tot].next=Head[u];
Head[u]=tot++;
}
void Tarjan(int u)
{
int i,j,k,v;
DFN[u]=Low[u]=++cnt;
stack[top++]=u;
instack[u]=1;
for(i=Head[u];i!=-1;i=edge[i].next)
{
v=edge[i].v;
if(!DFN[v])
{
Tarjan(v);
Low[u]=min(Low[u],Low[v]);
}
else if(instack[v])
Low[u]=min(Low[u],DFN[v]);
}
if(DFN[u]==Low[u])
{
scnt++;
do
{
v=stack[--top];
instack[v]=0;
Belong[v]=scnt;
}while(u!=v);
}
}
void tuopu()
{
int i,j,k,u,v;
while(!qu.empty())
{
u=qu.front();
qu.pop();
if(!qu.empty())
{
flag=false;
return ;
}
for(i=Head[u];i!=-1;i=edge[i].next)
{
v=edge[i].v;
in[v]--;
if(in[v]==0)
qu.push(v);
}
}
}
int main()
{
int i,j,k,u,v;
scanf("%d",&T);
for(t=1;t<=T;t++)
{
init();
scanf("%d%d",&n,&m);
for(i=1;i<=m;i++)
{
scanf("%d%d",&U[i],&V[i]);
add(U[i],V[i]);
}
for(i=1;i<=n;i++)
if(!DFN[i])
Tarjan(i);
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
memset(Head,-1,sizeof(Head));
tot=0;
for(i=1;i<=m;i++)
{
u=Belong[U[i]];
v=Belong[V[i]];
if(u!=v)
{
add(u,v);
in[v]++;
}
}
while(!qu.empty())
qu.pop();
memset(instack,0,sizeof(instack));
for(i=1;i<=scnt;i++)
if(in[i]==0)
qu.push(i);
flag=true;
tuopu();
if(flag)
printf("Yes\n");
else
printf("No\n");
}
}