(判断是否为弱联通分量) poj 2762

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

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的点出发,进行DFS找出链子的长度是否为点的个数,ok
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
vector<int> e[1010],mp[1010];
stack<int> s;
int n,m,Dfs[1010],use[1010],isstack[1010],low[1010],in[1010],mark[1010];
bool vis[1010][1010];
int top,newflag,ans;
void init()
{
    memset(Dfs,0,sizeof(Dfs));
    memset(use,0,sizeof(use));
    memset(isstack,0,sizeof(isstack));
    memset(low,0,sizeof(low));
    memset(vis,0,sizeof(vis));
    memset(in,0,sizeof(in));
    memset(mark,0,sizeof(mark));
    top=newflag=0;
    ans=0;
    for(int i=1;i<=n;i++)
        e[i].clear(),mp[i].clear();
    while(!s.empty())
        s.pop();
}
void dfs(int u)
{
    mark[u]=1;
    ans++;
    for(int i=0;i<mp[u].size();i++)
    {
        int v=mp[u][i];
        if(!mark[v])
        {
            dfs(v);
            return ;
        }
    }
}
void tarjan(int u)
{
    Dfs[u]=low[u]=++top;
    s.push(u);
    isstack[u]=1;
    for(int i=0;i<e[u].size();i++)
    {
        int v=e[u][i];
        if(!Dfs[v])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(isstack[v])
            low[u]=min(low[u],Dfs[v]);
    }
    if(low[u]==Dfs[u])
    {
        newflag++;
        int x;
        do
        {
            x=s.top();
            s.pop();
            isstack[u]=0;
            use[x]=newflag;
        }while(x!=u);
    }
}
int main()
{
    int tt;
    scanf("%d",&tt);
    while(tt--)
    {
        scanf("%d%d",&n,&m);
        init();
        for(int i=1;i<=m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            e[x].push_back(y);
        }
        for(int i=1;i<=n;i++)
        {
            if(!Dfs[i])
                tarjan(i);
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<e[i].size();j++)
            {
                if(use[i]!=use[e[i][j]]&&!vis[use[i]][use[e[i][j]]])
                {
                    mp[use[i]].push_back(use[e[i][j]]);
                    vis[use[i]][use[e[i][j]]]=1;
                    in[use[e[i][j]]]++;
                }
            }
        }
        for(int i=1;i<=newflag;i++)
        {
            if(in[i]==0)
            {
                dfs(i);
                break;
            }
        }
        if(ans==newflag)
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}

  

posted @ 2015-05-25 10:15  waterfull  阅读(237)  评论(0编辑  收藏  举报