Semiconnected--强连通缩点

1451: Semiconnected

时间限制: 1 Sec   内存限制: 32 MB
提交: 79   解决: 20

题目描述

For a directed graph G = (V, E), if for all pairs of nodes u,v, u can always reach v or v can always reach u , then we call this a Semiconnected graph. Now you are given a directed graph, you should tell us whether it is a Semiconnected graph. If it is, output “yes”, or else“no”.

输入

There are several test cases, for each test case, there are two integers in the first line, n and m n(n<=10000) ,m(m<=50000) , means that there are n nodes and m edges in this graph. The nodes are numbered from 1 to n. Then m lines each with two integers u and v means there is an edge from u to v.

输出

For each test case, output “yes” if it is a Semiconnected graph, or else “no” instead.

样例输入

4 3
1 2
2 3
3 4
4 3
1 2
2 3
4 3

样例输出

yes
no

题意:给你一个有向图,若从图中任意两个点u,v。都有从u到v的路径,或者从v到u的路径,则该图为半连通的,是则输出yes,不是则输出no

思路:若该图符合以上情况,

        1:入度为0的点多于一个,不符合。

        2:对该图进行缩点后,若为半连通图,则当前的图为一条直线,统计新图中入度,出度为0的点是不是都只有一个即可。

代码如下:

#include "stdio.h"  //强连通缩点后,判断新图中入度为0,出度为0的点是否都只有一个
#include "string.h"
#include "vector"
#include "stack"
using namespace std;

#define N 10005
#define M 50005

struct node
{
    int x,y;
    int next;
}edge[M];
int idx,head[N];
void Init(){ idx=0; memset(head,-1,sizeof(head)); }
void Add(int x,int y)
{
    edge[idx].x=x; edge[idx].y=y;
    edge[idx].next=head[x]; head[x]=idx++;
}

int n;
int dfs_clock;
bool mark[N];
int scc_cnt;
int belong[N];
stack q;
int num[N];
int low[N],dfn[N];
int MIN(int a,int b) { return a1) { printf("no\n"); continue; }
        Solve(id);
    }
    return 0;
}








你可能感兴趣的:(图--连通图,算法)