题意:
给出数个点的有向图,求是否任意两点间互通,也就是强连通图判断
思路:
一开始是想,从第一个点dfs搜索所有点,再所有点搜索第一个点,感觉会超时,没写出来……
然后看了下dalao的题解,用的二维并查集,虽然捣鼓出来了,但感觉不太对的样子
额,之后才知道强连通图判断,可以套用强连通图的模板
代码1(二维并查集):
#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
const int INF = 0x3fffffff;
const int MAXN = 1e4+5;
int n;
int fa[2][MAXN];
//f[1][x]=y x can be found by y; y to x;
//f[0][x]=y x can found y; x to y;
void init(){
for(int i=1; i<=n; i++)
fa[0][i] = fa[1][i] = i;
}
int my_find(int x, int le){
while(x != fa[le][x])
x = fa[le][x];
return x;
}
void combine(int x, int y){
int xx, yy;
if(x != n){
xx = my_find(x,0);
yy = my_find(y,0);
if(xx != yy) fa[0][x] = y;
}
if(y != n){
xx = my_find(x,1);
yy = my_find(y,1);
if(xx != yy) fa[1][y] = x;
}
}
int main(){
int m;
int a, b;
while(scanf("%d%d", &n, &m) && (n||m)){
init();
while(m--){
scanf("%d%d", &a, &b);
combine(a, b);
}
b = 0;
for(int i=1; i<=n; i++){
a = my_find(i,0);
if(a!=n) {b=1; break;}
a = my_find(i,1);
if(a!=n) {b=1; break;}
}
if(b) printf("No\n");
else printf("Yes\n");
}
return 0;
}
反思:
知识量太少,还要多多提高姿势水平啊!