POJ 2762

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

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



题意很简单,判断一个图是否为单连通图(一开始看成了强连通,囧)。

解法是先对图强连通缩点,然后判断缩点后的树形图是不是一条链就可以了。对缩点后的图拓扑排序,用队列维护(只要发现入度为0的点就进队列),如果某时刻队列中元素多于1个,就一定不是链。


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define maxn 1009
#define maxm 6001
using namespace std;
int n,m,tot;
int first[maxn];
int u[maxm],v[maxm],next[maxm];
int low[maxn],dfn[maxn],cnt,in[maxn],time,stk[maxn],top,id[maxn];
vector<int>G[maxn];
int du[maxn];
void add(int x,int y)
{
    u[tot]=x,v[tot]=y;
    next[tot]=first[x];
    first[x]=tot++;
}
void dfs(int cur)
{
    low[cur]=dfn[cur]=++time;
    in[cur]=1;
    stk[++top]=cur;
    for(int e=first[cur];e!=-1;e=next[e])
    {
        if(!dfn[v[e]])
        {
            dfs(v[e]);
            low[cur]=min(low[cur],low[v[e]]);
        }
        else if(in[v[e]])
            low[cur]=min(low[cur],dfn[v[e]]);
    }
    if(dfn[cur]==low[cur])
    {
        cnt++;int x;
        do
        {
            x=stk[top--];
            in[x]=0;
            id[x]=cnt;
        }while(x!=cur);
    }
}
bool topsort()
{
    queue<int>q;
    for(int i=1;i<=cnt;i++)
        if(!du[i])
        q.push(i);
    while(!q.empty())
    {
        if(q.size()>1)
            return 0;
        int x=q.front();
        q.pop();
        for(int i=0;i<G[x].size();i++)
        {
            int v=G[x][i];
            du[v]--;
            if(!du[v])
                q.push(v);
        }
    }
    return 1;
}
int main()
{
    int tt;
    scanf("%d",&tt);
    while(tt--)
    {
        scanf("%d%d",&n,&m);
        memset(first,-1,sizeof(first));tot=0;
        for(int i=0;i<m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            add(x,y);
        }
        cnt=0;
        memset(dfn,0,sizeof(dfn));
        top=0;time=0;
        for(int i=1;i<=n;i++)
            if(!dfn[i])dfs(i);
        memset(du,0,sizeof(du));
        for(int i=1;i<=cnt;i++)G[i].clear();
        for(int i=0;i<tot;i++)
            if(id[u[i]]!=id[v[i]])
        {
            du[id[v[i]]]++;
            G[id[u[i]]].push_back(id[v[i]]);
        }
        if(topsort())
            printf("Yes\n");
        else
            printf("No\n");
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值