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 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题目大意:n个节点 m条单向边。构成一个图,问你是否可以保证任选2个点u、v,这两个点之间是连通的? 保证则输出:Yes 否则:No强连通分量算法将图化简缩点后,进行拓扑排序。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <vector>
#include <stack>
#include <algorithm>
#define N 1002

using namespace std;

struct node
{
    int e, val;
}item;
vector<node>g[N];
stack<int>st;

int dfn[N], low[N];
int id[N]; //标记i节点属于哪个连通分量
int mat2[N][N];
int n2;
int counter, n, scnt;

void Tarjan(int e)
{
    int t, i;
    int mm=low[e]=dfn[e]=counter++;
    st.push(e);//入栈
    for(i=0; i<g[e].size(); i++){
        t=g[e][i].e;
        if(dfn[t]==-1)
            Tarjan(t);
        if(low[t]<mm)
            mm=low[t];
    }
    if(mm<low[e]){//有回边
        low[e]=mm; return;
    }
    do{
        id[t=st.top()]=scnt; low[t]=n;
        st.pop();
    }while(t!=e);
    scnt++;
}

void Search(int n)
{
    int i;
    memset(dfn, -1, sizeof(dfn));
    counter=0; scnt=0;
    for(i=0; i<n; i++)
        if(dfn[i]==-1)
            Tarjan(i);
}

void base_vertex()
{
    int i, j, t;
    Search(n); //调用求强连通分量
    n2=scnt;
    memset(mat2,0,sizeof(mat2));
    for(i=0; i<n; i++){
        for(j=0; j<g[i].size(); j++){
            t=g[i][j].e; mat2[id[i]][id[t]]=1;
        }
    }
}

int topo_sort(int n, int mat[][N], int *topo)
{//拓扑排序的n是强连通分量的个数
    int d[N], i, j, k;
    for(i=0; i<n; i++){
        d[i]=0;
        for(j=0; j<n; j++)
            d[i]=d[i]+mat[j][i];//入度数
    }
    for(k=0; k<n; k++){
        for(i=0; d[i]&&i<n; i++);
        if(i==n) return 0;
        d[i]=-1;
        for(j=0; j<n; j++)
            d[j]-=mat[i][j];
        topo[k]=i;
    }
    return 1;
}
int main()
{
    int tg; scanf("%d", &tg);
    int m, u, v;
    int i, j;
    int topo[N];
    while(tg--)
    {
        scanf("%d %d", &n, &m);
        for(i=0; i<=n; i++)
            g[i].clear();
        for(i=0; i<m; i++){
            scanf("%d %d", &u, &v);
            item.e=v-1; item.val=1;
            g[u-1].push_back(item);
        }//建立单向图
        base_vertex();
        topo_sort(n2, mat2, topo);
        int flag=1;
        for(i=0; i<n2-1; i++){
            if(!mat2[topo[i]][topo[i+1]] ){
                flag=0; break;
            }
        }
        if(flag) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}

/*
    下面的数据会访问冲突崩溃掉 但poj可以Accepted 不懂~~~
1
8 11
1 2
2 3
2 5
2 6
3 5
4 3
5 2
5 4
6 7
6 8
7 6

*/
				
时间: 2024-11-09 00:23:44

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

POJ 3114 - Countries in War(强连通分量+缩点+拓扑排序+DAG最短路)

Countries in War Time Limit:1000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u Appoint description: Description In the year 2050, after different attempts of the UN to maintain peace in the world, the third world war broke out. The impor

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: 14789   Accepted: 3915 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?(强连通缩点+拓扑排序)

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

POJ 1236 Network of Schools (强连通分量缩点求度数)

题意: 求一个有向图中: (1)要选几个点才能把的点走遍 (2)要添加多少条边使得整个图强联通 分析: 对于问题1, 我们只要求出缩点后的图有多少个入度为0的scc就好, 因为有入度的scc可以从其他地方到达. 对于问题2, 每个入度为0的scc, 都可以补一条边可以变成强连通图, 每个出度为0的scc, 也可以补一条边使其变成强连通图. 所以答案就是max(入度为0scc个数,出度为0scc个数). #include<cstdio> #include<iostream> #inc

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,

POJ 2762 tarjan缩点+并查集+度数

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

POJ - 1236 Network of Schools(有向图的强连通分量)

d.各学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输, 问题1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件. 问题2:至少需要添加几条传输线路(边),使任意向一个学校发放软件后,经过若干次传送,网络内所有的学校最终都能得到软件.s.首先找强连通分量,然后看强连通分量的入度为0点的总数,出度为0点的总数. 第一问为入度为0的强连通分量的个数. 第二问为入度为0的个数和出度为0的个数中大的.(将这个图的所有子树找出来,然后将一棵子树的叶子结

poj 1236 scc强连通分量

分析部分摘自:http://www.cnblogs.com/kuangbin/archive/2011/08/07/2130277.html 强连通分量缩点求入度为0的个数和出度为0的分量个数 题目大意:N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输,问题1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件.2,至少需要添加几条传输线路(边),使任意向一个学校发放软件后,经过若干次传送,网络内所有的学校最终都能得到软件