poj2762 判断单连通


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

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

首先利用tarjan算法求得所有的强连通分量,因为强连通分量之间的点两两可达,因此将其缩点,缩完点后的图必然没有环,因此单连通的充要条件是含有一条链,因此拓扑排序,每次产生的0度点必然只有一个。

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#define Maxn 1010
using namespace std;

struct edge{
    int to,next;
}p[Maxn*Maxn];
int head[Maxn];
int tot;
void addedge(int a,int b){
    p[tot].to=b;
    p[tot].next=head[a];
    head[a]=tot++;
}
int dfn[Maxn]; //时间戳
int low[Maxn]; //u和u的子孙能返回的最低深度
int belong[Maxn]; //属于哪个连通分量
int in[Maxn]; //是否在栈中
int st[Maxn]; //栈
int tmpdfn; //时间戳,从1开始
int top;
int scc;
void tarjan(int u){
    dfn[u]=low[u]=++tmpdfn;
    st[++top]=u;
    in[u]=1;
    for(int i=head[u];i!=-1;i=p[i].next){
        int v=p[i].to;
        if(!dfn[v]){
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(in[v])
            low[u]=min(low[u],dfn[v]);
    }
    if(dfn[u]==low[u]){
        scc++;
        do{
            in[st[top]]=0;
            belong[st[top]]=scc;
        }while(st[top--]!=u);
    }
}
int scc_cnt(int n){
    memset(dfn,0,sizeof dfn);
    memset(in,0,sizeof in);
    scc=0;
    for(int i=1;i<=n;i++){
        tmpdfn=top=0;
        if(!dfn[i]) tarjan(i);
    }
    return scc;
}
int indeg[Maxn];
int adj[Maxn][Maxn];
bool topo_sort(int n){
    int tp=0,cnt=0;
    for(int i=1;i<=n;i++)
        if(!indeg[i]){
            indeg[i]=tp;tp=i;
            if(++cnt>1) return false;
        }
    while(tp){
        int t=tp;
        tp=indeg[tp];
        cnt=0;
        for(int i=1;i<=n;i++)
            if(adj[t][i]){
                if(--indeg[i]==0){
                    indeg[i]=tp,tp=i;
                    if(++cnt>1) return false;
                }
            }
    }
    return true;
}
int main()
{
    int t,n,m,a,b;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        memset(head,-1,sizeof head);
        tot=0;
        for(int i=0;i<m;i++){
            scanf("%d%d",&a,&b);
            addedge(a,b);
        }
        scc_cnt(n);
        memset(adj,0,sizeof adj);
        memset(indeg,0,sizeof indeg);
        for(int i=1;i<=n;i++)
            for(int j=head[i];j!=-1;j=p[j].next)
                adj[belong[i]][belong[p[j].to]]=1;
        for(int i=1;i<=scc;i++) adj[i][i]=0;
        for(int i=1;i<=scc;i++)
            for(int j=1;j<=scc;j++)
                if(adj[i][j]) indeg[j]++;
        if(topo_sort(scc)) puts("Yes");
        else puts("No");
    }
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值