POJ2762 Going from u to v or from v to u?(强连通缩点+拓扑排序)

Going from u to v or from v to u?

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 15196   Accepted: 4013

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

POJ Monthly--2006.02.26,zgl & twb

一开始题意不明,以为是只有任意两个点可以相互到达才输出Yes,就只用了一个强连通去判断,结果WA。

题意:有向图,如果有一个点可以到达其他n-1个点则输出Yes,否则No。

解题:先用强连通缩点后,形成 一个有向无环图,然后再用 拓扑排序,如果同时有两点的入度0 ,则不满足条件。

#include<stdio.h>
#include<vector>
#include<string.h>
using namespace std;
const int N = 1005;

int dfn[N],low[N],Stack[N],indx[N],vist[N],top,deep,k,tt;
vector<int>mapt[N];
void dfs(int u)
{
    deep++;
    vist[u]=tt;
    Stack[++top]=u;
    dfn[u]=low[u]=deep;
    int len=mapt[u].size();
    for(int i=0;i<len;i++)
    {
        int v=mapt[u][i];
        if(vist[v]==0)
        {
            dfs(v);
            if(low[u]>low[v])
                low[u]=low[v];
        }
        else if(vist[v]==tt&&low[u]>dfn[v])//有向图一定要用vist[v]==tt
            low[u]=dfn[v];
    }
    if(dfn[u]==low[u])
    {
        k++;
        while(u!=Stack[top])
            indx[Stack[top--]]=k;
        indx[Stack[top--]]=k;
    }
}
int in[N],mapt1[N][N];
int tope()
{
    int a[N],m=0;
    for(int i=1;i<=k;i++)
     if(in[i]==0)
        a[m++]=i;
    while(m--)
    {
        if(m)
            return 0;
        int s=a[m];
        for(int i=1;i<=k;i++)
        if(mapt1[s][i])
        {
            in[i]-=mapt1[s][i];
            if(in[i]==0)
                a[m++];
        }
    }
    return 1;
}
int main()
{
    int t,n,m,a,b;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            vist[i]=in[i]=0;  mapt[i].clear();
        }
        memset(mapt1,0,sizeof(mapt1));
        top=k=deep=tt=0;

        while(m--)
        {
            scanf("%d%d",&a,&b);
            mapt[a].push_back(b);
        }

        for(int i=1;i<=n;i++)
            if(vist[i]==0)
            {
                tt++;
                dfs(i);
            }
        for(int i=1;i<=n;i++)
        {
            int u=indx[i];
            for(int j=0;j<mapt[i].size();j++)
            {
                int v=indx[mapt[i][j]];
                if(u==v)
                    continue;
                mapt1[u][v]++; in[v]++;
            }
        }

        int flag=tope();
        if(flag==1)
            printf("Yes\n");
        else printf("No\n");
    }
}
时间: 2024-08-08 13:10:15

POJ2762 Going from u to v or from v to u?(强连通缩点+拓扑排序)的相关文章

poj 2762 Going from u to v or from v to u?【强连通分量缩点+拓扑排序】

Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15812   Accepted: 4194 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

POJ2762 Going from u to v or from v to u? 强连通分量缩点+拓扑排序

题目链接:https://vjudge.net/contest/295959#problem/I 或者 http://poj.org/problem?id=2762 题意:输入多组样例,输入n个点和m条有向边,问该图中任意两点x, y之间是否满足x可以到y或者y可以到x. 一开始WA的原因是因为没注意到是或者, 如果是并且的话,就是一道简单的强连通分量的题,直接判断整个图是否为一个强连通分量 对于该题, 先用强连通分量进行缩点,简化图.图就变成了DAG,用拓扑排序判断图中点的入度, 图中入度为0

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

Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17689   Accepted: 4745 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,

POJ2762 Going from u to v or from v to u? 强连通+缩点

题目链接: poj2762 题意: 给出一幅单向图.问这张图是否满足   随意两点ab 都能 从a到达b 或  从b到达a 题解思路: 推断一幅图是否满足弱连通 首先想到的是将图中的 强连通分量(能互相到达的顶点集)  进行缩点 然后再依据原有边 又一次建图 假设缩点后的图是一条单链(回路,通路都能够)   则一定满足弱连通 推断是否是一条单链 能够依据建图过程中得到 入度 出度 数组进行推断 某点的入度 或 出度假设大于1则一定不是单链 另外单链仅仅能有一条  不能有多个点入度=0 代码: #

poj2762 Going from u to v or from v to u? --- 缩点+拓扑

给一个有向图,问是否该图上任意两点间可达. 首先容易想到缩点成有向无环图,其次就是如何处理任意两点间可达. 我在纸上画了一些情况: 4 3 1 2 2 3 2 4 4 4 1 2 1 3 2 4 3 4 3 3 1 2 2 3 1 3 7 8 1 2 1 3 3 4 2 4 4 5 4 6 5 7 6 7 5 6 1 2 1 3 2 3 3 4 3 5 4 5 NNYNY 根据这里一直在纠结如何通过入度出度直接判断.但是一直觉得不严谨.. 然后发现只要拓扑序列唯一即可.这样图中就没有 没有关系的

[poj2762] Going from u to v or from v to u?(Kosaraju缩点+拓排)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14778   Accepted: 3911 Description In order to make their sons brave, Jiajia and Wind take them

【缩点+拓扑判链】POJ2762 Going from u to v or from v to 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 o

poj 2762 Going from u to v or from v to u? trajan+拓扑

Going from u to v or from v to 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 thei

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

职务地址:id=2762">POJ 2762 先缩小点.进而推断网络拓扑结构是否每个号码1(排序我是想不出来这点的. .. ).由于假如有一层为2的话,那么从此之后这两个岔路的点就不可能从一点到还有一点的. 代码例如以下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include