poj 2762 Going from u to v or from v to u?

u到v或者v到u

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

简单来说,这是一个有向图的弱连通问题…

注意,是有向图!

学习了图的强连通后,不难发现,一个样有向图的强连通是有向环,对于环上的每一个点(room),他们都可以互相到达。

如果我们找到图中的强连通分量,将他们缩点,再连边,会形成一个“压缩到极致的”无环有向图(不想无向图,缩点连边后是一棵树。实际上是因为有向图有横叉边)

再仔细想想,如果此时的新图中,有两个或以上入度为0的点,那么这两个点肯定不能互相到达。

如果只有一个入度为0的点,以这个点为root,DFS遍历整个图,如果对于某个结点,他有两个或以上的儿子,那么这两棵或以上的子树上的点,就不能互相到达。说了这么多其实就是能形成一条单链。。。

那么,就来实现一下。

代码:

#include<cstdio>
#include<stack>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 1000
#define MAXM 6000
int t,n,m;
struct node{
    int v;
    node *next;
}edge[MAXM+10],*adj[MAXN+10],*ecnt=&edge[0];
int dfn[MAXN+10],low[MAXN+10],dcnt,block_cnt,bccno[MAXN+10],in[MAXN+10],out[MAXN+10];
stack<int> point;
bool instack[MAXN+10],mat[MAXN+10][MAXN+10],vis[MAXN+10];
void reset()
{
    memset(edge,0,sizeof(edge)); memset(adj,0,sizeof(adj)); ecnt=&edge[0];
    memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); memset(bccno,0,sizeof(bccno));
    memset(in,0,sizeof(in)); memset(out,0,sizeof(out));
    memset(instack,0,sizeof(instack)); memset(mat,0,sizeof(mat)); memset(vis,0,sizeof(vis));
    dcnt=block_cnt=0;
}
void addedge(int u,int v)
{
    node *p=++ecnt;
    p->v=v;
    p->next=adj[u];
    adj[u]=p;
}
void read()
{
    int x,y;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        scanf("%d%d",&x,&y);
        addedge(x,y);
        mat[x][y]=true;
    }
}
void dfs_find_SCC(int u,int fa)
{
    point.push(u);
    instack[u]=true;
    dfn[u]=low[u]=++dcnt;
    for(node *p=adj[u];p;p=p->next){
        int v=p->v;
        if(!dfn[v]){
            dfs_find_SCC(v,u);
            low[u]=min(low[u],low[v]);
        }
        else if(instack[v])
            low[u]=min(low[u],dfn[v]);
    }
    if(dfn[u]==low[u]){
        ++block_cnt;
        int uu;
        do{
            uu=point.top(); point.pop();
            bccno[uu]=block_cnt;
            instack[uu]=false;
        }while(uu!=u);
    }
}
void build_new_tree() 
{
    memset(edge,0,sizeof(edge));
    memset(adj,0,sizeof(adj));
    ecnt=&edge[0];
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++){
            if(j==i) continue;
            if(mat[i][j]&&bccno[i]!=bccno[j]){
                addedge(bccno[i],bccno[j]);
                out[bccno[i]]++,in[bccno[j]]++;
            }
        }
}
bool dfs_Judge(int u)
{
    vis[u]=true;
    int cnt=0;
    for(node *p=adj[u];p;p=p->next){
        int v=p->v;
        if(!vis[v]){
            cnt++;
            bool f=dfs_Judge(v);
            if(!f) return false;
        }
    }
    if(cnt>=2) return false;
    return true;
}
bool solve()
{
    //最多只能有一个入度为0的点,如果有多个入度为0的点,则这两个连通分量肯定是不连通的
    int incnt=0,rt=-1;
    for(int i=1;i<=block_cnt;i++)
        if(!in[i]) incnt++,rt=i;
    if(incnt>1) return false;
    if(dfs_Judge(rt)) return true;
    else return false;
}

int main()
{
    scanf("%d",&t);
    for(int i=1;i<=t;i++){
        reset();
        read();
        for(int i=1;i<=n;i++)
            if(!dfn[i])
                dfs_find_SCC(i,-1); //strongly connected componenet:强连通
        build_new_tree();  //缩点,建树
        bool f=solve();
        if(f) printf("Yes\n");
        else printf("No\n");
    }
}

不大满意的地方:reset写得比较丑。。。。

转载于:https://www.cnblogs.com/katarinayuan/p/6572876.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值