POJ 2762 Going from u to v or from v to u? Tarjan缩点+判断链

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

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


在一个图中让你判断是否任意两个点u和v,都有u能够到达v或者v能够到达u。在强联通分量里面的点肯定满足此条件,所以首先tarjan缩点,缩点之后,重新建图,要实现u到v或者v到u这一条件,那么建的新图必须满足是一条链,如果不是链,而在u点有分支,一边连接v1,一边连接v2的话,那么v1不可能到达v2,v2也不可能到达v1,因此就不能满足条件,所以不成立。
//4616K	547MS
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#define M 1007
using namespace std;
int dfn[M],low[M],head[M],vis[M],stack[M],belong[M];
int n,m,cnt,scnt,begin,num;
int g[M][M],in[M];
struct E
{
    int v,to;
}edg[M*M];
void init()
{
    memset(head,-1,sizeof(head));
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(belong ,0,sizeof(belong));
    memset(stack,0,sizeof(stack));
    memset(vis,0,sizeof(vis));
    memset(g,0,sizeof(g));
    memset(in,0,sizeof(in));
    cnt=scnt=num=begin=0;
}
void addedge(int u,int v)
{
    edg[num].v=v;edg[num].to=head[u];
    head[u]=num++;
}
void tarjan(int x)
{
    int v;
    dfn[x]=low[x]=++cnt;
    stack[++begin]=x;
    for(int i=head[x];i!=-1;i=edg[i].to)
    {
        v=edg[i].v;
        if(!dfn[v])
        {
            tarjan(v);
            low[x]=min(low[x],low[v]);
        }
        else if(!vis[v])
            low[x]=min(low[x],dfn[v]);
    }
    if(low[x]==dfn[x])
    {
        scnt++;
        do
        {
            v=stack[begin--];
            belong[v]=scnt;
            vis[v]=1;
        }while(v!=x);
    }
}
bool link()//判断能否形成一条链
{
    queue<int>q;
    for(int i=1;i<=scnt;i++)
        if(!in[i])q.push(i);
    if(q.size()>1)return false;
    while(!q.empty())
    {
        int now=q.front();
        q.pop();
        for(int i=1;i<=scnt;i++)
            if(g[now][i])
            {
                in[i]--;
                if(!in[i])q.push(i);
            }
        if(q.size()>1)return false;
    }
    return true;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        init();
        scanf("%d%d",&n,&m);
        int a,b;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&a,&b);
            addedge(a,b);
        }
        for(int i=1;i<=n;i++)
            if(!dfn[i])tarjan(i);
        if(scnt==1) {printf("Yes\n");continue;}
        for(int u=1;u<=n;u++)//缩点之后,重新建图
            for(int j=head[u];j!=-1;j=edg[j].to)
            {
                int v=edg[j].v;
                if(u!=v&&belong[u]!=belong[v])
                {
                    g[belong[u]][belong[v]]=1;
                    in[belong[v]]++;
                }
            }
        if(link())printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值