[POJ1236]Network of Schools(并查集+floyd,伪强连通分量)

时间:2023-03-10 04:14:19
[POJ1236]Network of Schools(并查集+floyd,伪强连通分量)

题目链接:http://poj.org/problem?id=1236

  这题本来是个强连通分量板子题的,然而弱很久不写tarjan所以生疏了一下,又看这数据范围觉得缩点这个事情可以用点到点之间的距离来判断不知道群巨兹磁不兹磁……下面弱就给大家搞一发如何用floyd和并查集来缩点……大致的思路就是先floyd跑出所有距离,然后O(n^2)找两两都可达的点,把它们的关系用并查集来维护。接下来O(n)找并查集里的代表元素。这个时候应当特判一下连通块为1的时候。再O(n^2)找出所有单向边,然后更新所有代表元素的出入度,就完事了…完事了…数据太水所以弱的floyd跑过了,以后不能这么投机了QAQ

 /*
━━━━━┒ギリギリ♂ eye!
┓┏┓┏┓┃キリキリ♂ mind!
┛┗┛┗┛┃\○/
┓┏┓┏┓┃ /
┛┗┛┗┛┃ノ)
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┃┃┃┃┃┃
┻┻┻┻┻┻
*/
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath> using namespace std; #define fr first
#define sc second
#define cl clear
#define W(a) while(a--)
#define pb(a) push_back(a)
#define Rint(a) scanf("%d", &a)
#define Rll(a) scanf("%I64d", &a)
#define Rs(a) scanf("%s", a)
#define FRead() freopen("in", "r", stdin)
#define FWrite() freopen("out", "w", stdout)
#define Rep(i, len) for(int i = 0; i < (len); i++)
#define For(i, a, len) for(int i = (a); i < (len); i++)
#define Cls(a) memset((a), 0, sizeof(a))
#define Clr(a, x) memset((a), (x), sizeof(a))
#define Full(a) memset((a), 0x7f7f, sizeof(a)) const int inf = 0x7f7f7f;
const int maxn = ;
int n, m;
int in[maxn], out[maxn];
int dp[maxn][maxn];
int pos[maxn], cnt;
int pre[maxn];
int belong[maxn]; int find(int x) {
return x == pre[x] ? x : pre[x] = find(pre[x]);
} void unite(int x, int y) {
x = find(x);
y = find(y);
if(x != y) pre[x] = y;
} int main() {
// FRead();
int v;
while(~Rint(n)) {
Cls(in); Cls(out);
For(i, , n+) {
pre[i] = i;
For(j, , n+) dp[i][j] = inf;
dp[i][i] = ;
}
For(u, , n+) {
while(Rint(v)) {
if(v == ) break;
dp[u][v] = ;
}
}
For(k, , n+)
For(i, , n+)
For(j, , n+)
if(dp[i][j] > dp[i][k] + dp[k][j])
dp[i][j] = dp[i][k] + dp[k][j];
For(i, , n+) {
For(j, i+, n+) {
if(dp[i][j] != inf && dp[j][i] != inf) {
unite(i, j);
}
}
}
For(i, , n+) {
int fa = find(i);
if(i == fa) pos[cnt++] = i;
belong[i] = fa;
}
if(cnt == ) {
printf("1\n0\n");
continue;
}
For(i, , n+) {
For(j, , n+) {
if(i == j) continue;
if(belong[i] != belong[j] && dp[j][i] == inf && dp[i][j] != inf) {
in[belong[i]]++;
out[belong[j]]++;
}
}
}
int ans1 = , ans2 = ;
Rep(i, cnt) {
if(!out[pos[i]]) ans1++;
if(!in[pos[i]]) ans2++;
}
printf("%d\n%d\n", ans1, max(ans1, ans2));
}
return ;
}