POJ 2762 Going from u to v or from v to u?(强连通分量+拓扑排序)@



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

给定一个有向图,问是否任意两个点x,y,是否X可以到达Y,或Y可以到达X,一开始想成要满足环的条件,但题目要求的是或,意味着这个图只要有一个入度为零的强连通分量

就可以了,先缩点,再用拓扑排序


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
const int N = 250000+10;
int cnt, taj, top, time;
int vis[N], head[N], stack1[N], low[N], dfn[N], belong[N];
void init()
{
    memset(vis,0,sizeof(vis));
    memset(head,-1,sizeof(head));
    memset(low,-1,sizeof(low));
    memset(dfn,-1,sizeof(dfn));
    cnt=0, top=0, taj=0, time=0;
    return ;
}

struct node
{
    int from, to, next;
}p[250010*8];

void add(int u,int v)
{
   p[cnt].from=u, p[cnt].to=v, p[cnt].next=head[u], head[u]=cnt++;
   return ;
}

void dfs(int u)
{
    low[u]=time;
    dfn[u]=time++;
    vis[u]=1, stack1[top++]=u;
    for(int i=head[u];i!=-1;i=p[i].next)
    {
        int v=p[i].to;
        if(dfn[v]==-1)
        {
            dfs(v);
            low[u]=min(low[u], low[v]);
        }
        else if(vis[v])
             low[u]=min(low[u], dfn[v]);
    }
    if(dfn[u]==low[u])
    {
        taj++;
        while(1)
        {
            int now=stack1[--top];
            vis[now]=0;
            belong[now]=taj;
            if(now==u)
                break;
        }
    }
    return ;
}
int in[N], out[N];
vector<int>G[N];

int main()
{
    int t, n, m;
    scanf("%d", &t);
    while(t--)
    {
        init();
        scanf("%d %d", &n, &m);
        for(int i=1;i<=m;i++)
        {
            int x, y;
            scanf("%d %d", &x, &y);
            add(x, y);
        }
        for(int i=1;i<=n;i++)
        {
            if(dfn[i]==-1)
                dfs(i);
        }
        if(taj==1)
        {
            printf("Yes\n");
            continue;
        }
        memset(in,0,sizeof(in));
        memset(out,0,sizeof(out));
        priority_queue<int,vector<int>, greater<int> >q;
        while(!q.empty()) q.pop();
        for(int i=0;i<=taj;i++) G[i].clear();
        for(int i=0;i<cnt;i++)
        {
            int u=p[i].from, v=p[i].to;
            if(belong[u]!=belong[v])
            {
                out[belong[u]]++, in[belong[v]]++;
                G[belong[u]].push_back(belong[v]);
            }
        }
        for(int i=1;i<=taj;i++)
        {
            if(in[i]==0) q.push(i);
        }
        if(q.size()==0)
            printf("Yes\n");
        else if(q.size()>1)
            printf("No\n");
        else
        {
           int flag=0;
            while(!q.empty())
            {
                if(q.size()>1)
                {
                    flag=2;
                    break;
                }
                int u=q.top();q.pop();
                for(int i=0;i<G[u].size();i++)
                {
                    int v=G[u][i];
                    in[v]--;
                    if(in[v]==0) q.push(v);
                }
            }
            if(flag==0)
                printf("Yes\n");
            else
                printf("No\n");
        }
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值