首页 > 其他 > 详细

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

时间:2014-02-27 01:47:31      阅读:483      评论:0      收藏:0      [点我收藏+]
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;
}


POJ 2762 Going from u to v or from v to u? Tarjan缩点+判断链,布布扣,bubuko.com

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

原文:http://blog.csdn.net/crescent__moon/article/details/19931893

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!