POJ 2762

来源:互联网 发布:linux mint18.2输入法 编辑:程序博客网 时间:2024/06/06 01:27

Going from u to v or from v to u?
Time Limit: 2000MS Memory Limit: 65536KTotal 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

13 31 22 33 1

Sample Output

Yes

Source

POJ Monthly--2006.02.26,zgl & twb


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

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


#include<cstdio>#include<cstring>#include<algorithm>#include<queue>#define maxn 1009#define maxm 6001using 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");    }}