[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 ;
}
[POJ1236]Network of Schools(并查集+floyd,伪强连通分量)的更多相关文章
- P2746 [USACO5.3]校园网Network of Schools// POJ1236: Network of Schools
P2746 [USACO5.3]校园网Network of Schools// POJ1236: Network of Schools 题目描述 一些学校连入一个电脑网络.那些学校已订立了协议:每个学 ...
- codeforces 400D Dima and Bacteria 并查集+floyd
题目链接:http://codeforces.com/problemset/problem/400/D 题目大意: 给定n个集合,m步操作,k个种类的细菌, 第二行给出k个数表示连续的xi个数属于i集 ...
- UVALive 3027 Corporative Network 带权并查集
Corporative Network A very big corporation is developing its corporative networ ...
- codeforces 400 D Dima and Bacteria【并查集 Floyd】
题意:给出n个点,分别属于k个集合,判断每个集合里面的点的距离都为0,为0的话输出yes,并输出任意两个集合之间的最短路 这道题目有两个地方不会处理, 先是n个点,分别属于k个集合,该怎么记录下来这里 ...
- poj-1236.network of schools(强连通分量 + 图的入度出度)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 27121 Accepted: 10 ...
- upc组队赛14 Communication【并查集+floyd /Tarjan】
Communication 题目描述 The Ministry of Communication has an extremely wonderful message system, designed ...
- POJ1861 Network (Kruskal算法 +并查集)
Network Description Andrew is working as system administrator and is planning to establish a new net ...
- POJ1236 - Network of Schools tarjan
Network of Schools Time Limit: 1000MS Memory Limi ...
- POJ1236 Network of Schools (强连通)(缩点)
Network of Schools Time Limit: 1000MS ...
随机推荐
- Daject初探 - 从Table模型得到Record模型
前言: 如果你还不知道Daject是什么,如何使用,可以浏览 http://www.cnblogs.com/kason/p/3577359.html github地址:https://github.c ...
- 【学习总结】iOS 数据保存几种方式总结
在iOS开发过程中,不管是做什么应用,都会碰到数据保存的问题.将数据保存到本地,能够让程序的运行更加流畅,不会出现让人厌恶的菊花形状,使得用户体验更好.下面介绍一下数据保存的方式: NSKeyedAr ...
- oracle里如何将两个日期的时间差返回**时**分的格式
SELECT EXTRACT(DAY FROM (sysdate-to_date('2012-03-29 00:00:00','YYYY-MM-DD HH24:MI:ss')) DAY TO SECO ...
- 从状态转移看:载波侦听多路访问/冲突避免(CSMA/CA)
CSMA/CA是写入IEEE802.11的无线网络MAC层标准协议,相信看到这篇文章的读者都知道它是用来做什么的.但许多短文对这个协议的解释都有所缺乏,因此本文用状态转换图的形式详细说明协议的工作流程 ...
- ComboTree使用
1.参考资料 1.http://www.jeasyui.com/documentation/combotree.php 2.http://blog.csdn.net/woshichunchun/a ...
- [转载]GDI+中发生一般性错误
注:第一次写博客,把自己遇到的问题和收集的资料记录在博客上.在开发.NET应用中,使用 System.Drawing.Image.Save 方法而导致“GDI+ 中发生一般性错误”的发生,通常有以下三 ...
- BT5之Metasploit[MSF]连接postgresql数据库
1,先查看postgresql的端口,默认是自动开启的,端口7337 . root@bt:~# netstat -tnpl |grep postgres tcp 0 0 1 ...
- uva 1368
简单的贪心 ~ #include <cstdio> #include <cstdlib> #include <cmath> #include <map> ...
- uva 10910
简单dp /************************************************************************* > Author: xlc2845 ...
- Unity3d游戏中添加移动MM支付SDK问题处理
原地址:http://www.tuicool.com/articles/I73QFb 由于移动mm的SDK将部分资源文件放在jar包中,导致Unity无法识别,提示failed to find res ...