[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 ...
随机推荐
- Nginx开启gzip压缩功能
在Nginx安装完成之后,我们可以开启Gzip压缩功能,这里Nginx默认只能对text/html类型的文件进行压缩.下面的指令为开启Gzip的指令: gzip on; gzip_http_versi ...
- c++类中的静态成员
静态成员和非静态成员的区别: 类静态成员用static修饰,类的静态成员属于类本身,而不属于类的某个具体对象,静态成员被类的所有对象共享,因此某个对象对静态成员(数据成员)的修改对其对象是可见的.而类 ...
- JDBC连接数据库代码
//连接是需要导包 http://pan.baidu.com/s/1o6nyuOa /*配合数据库建立表 create database day14 character set utf8 collat ...
- C#基础原理拾遗——面试都爱问的委托和事件(纠正)
这篇博客是我昨天写的,文中的观点有些问题,后经过网友留言和个人学习发现错误,原文还是保留,更改补在后面,不怕贻笑大方,唯恐误人子弟.不知道还能不能放在首页,让被误导的同学再被反误导一次. 一.原文 几 ...
- iOS点击cell查看大图,点击大图还原小图-b
一.项目需求 用collectionView展示很多照片,点击某个照片,用全屏scrollView无限循环的方式查看图片.点击放大的图片,图片缩小到原先的尺寸. 如图gif1.gif所示,点击中间的图 ...
- Codeforces Round #222 (Div. 1) C. Captains Mode 对弈+dp
题目链接: http://codeforces.com/contest/378/problem/E 题意: dota选英雄,现在有n个英雄,m个回合,两支队伍: 每一回合两个选择: b 1,队伍一ba ...
- map中的erase成员函数用法
转载于 http://www.cnblogs.com/graphics/archive/2010/07/05/1771110.html http://hi.baidu.com/sdkinger/it ...
- jquery获取标签内容,编辑内容
一.获取页面元素 三种方式获取页面中元素的内容. input标签使用:.val()获取 标签下的html及文本内容:.html() 仅获取标签下的纯文本内容:.text() <head> ...
- hdu 1166 树状数组 线段树入门
点修改 区间求和 #include <cstdio> #include <cstdlib> #include <cmath> #include <map> ...
- SQL SERVER(MSSQLSERVER) 服务无法启用 特定服务错误:126
SQL SERVER(MSSQLSERVER) 服务无法启用 特定服务错误:126 对于这样一个错误google了一下 说是 要禁止掉via才行 回到SQL配置管理器中 禁止掉via 果然可以重新 ...