Going from u to v or from v to u? - POJ 2762 Tarjan+拓扑排序

76 篇文章 0 订阅

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. 

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");
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值