POJ 1236 Network of Schools - 缩点
POJ 1236 :http://poj.org/problem?id=1236
参考:https://www.cnblogs.com/TnT2333333/p/6875680.html
题意:
有好多学校,每个学校可以给其他特定的学校发送文件。第一个问题是最少要给几个学校发文件,可以使得全部的学校收到文件。第二个问题是最少要加几条线路,使得随意挑一个学校发文件,也能使得全部的学校收到文件。
思路:
第一个问题,可以用tarjan给图中先缩点,因为强连通的环相互可达。所以只要数出缩完点后图中入度为0的点的个数。第二个问题,可以这么考虑,缩完点后的图中有c1个入度为0的点,有c2个出度为0的点。把入度为0的点和出度为0的点尽量匹配,剩下的就向连通图中连一条边即可,所以第二个问题的答案就是max(c1,c2)。
/*
* @Author: chenkexing
* @Date: 2018-09-05 11:05:14
* @Last Modified by: chenkexing
* @Last Modified time: 2018-09-07 20:25:39
*/
#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert>
using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000") //c++
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull; typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e9+;
const double esp = 1e-;
const double PI=acos(-1.0); template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} /*-----------------------showtime----------------------*/
const int maxn = ;
vector<int>mp[maxn];
int dfn[maxn],low[maxn],vis[maxn],col[maxn];
int in[maxn],out[maxn];
int tot,cnt;
stack<int>S;
void tarjan(int x){
low[x] = dfn[x] = ++tot;
S.push(x);vis[x] = ;
for(int i=; i<mp[x].size(); i++){
int v = mp[x][i];
if(!dfn[v]){
tarjan(v);
low[x] = min(low[x],low[v]);
}
else if(vis[v]){
low[x] = min(low[x], dfn[v]);
}
}
if(low[x] == dfn[x]){
cnt++;
while(true){
int now = S.top();
S.pop();
col[now] = cnt;
vis[now] = ;
if(now == x)break;
}
}
}
int main(){
int n;
while(~scanf("%d", &n)){
for(int i=; i<=n; i++){
mp[i].clear();
dfn[i] = low[i] = vis[i] = col[i] = ;
in[i] = out[i] = ;
tot = cnt = ;
}
while(!S.empty())S.pop(); for(int i=; i<=n; i++){
int x;
while(scanf("%d", &x) && x){
mp[i].pb(x);
}
} for(int i=; i<=n; i++){
if(dfn[i] == ){
tarjan(i);
}
} for(int i=; i<=n; i++){
for(int j=; j<mp[i].size(); j++){
int u = i,v = mp[i][j];
if(col[u] != col[v]){
out[col[u]]++;
in[col[v]]++;
}
}
}
// debug(cnt); int ans1 = ,ans2 = ;
for(int i=; i<=cnt; i++){
if(in[i] == )ans1++;
if(out[i] == )ans2++;
}
if(cnt==)
printf("1\n0\n");
else printf("%d\n%d\n", ans1,max(ans1,ans2));
}
return ;
}
POJ 1236
POJ 1236 Network of Schools - 缩点的更多相关文章
- POJ 1236 Network of Schools(强连通 Tarjan+缩点)
POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意: 给定一张有向图,问最少选择几个点能遍历全图,以及最少加入�几条边使得 ...
- Poj 1236 Network of Schools (Tarjan)
题目链接: Poj 1236 Network of Schools 题目描述: 有n个学校,学校之间有一些单向的用来发射无线电的线路,当一个学校得到网络可以通过线路向其他学校传输网络,1:至少分配几个 ...
- POJ 1236 Network of Schools(强连通分量)
POJ 1236 Network of Schools 题目链接 题意:题意本质上就是,给定一个有向图,问两个问题 1.从哪几个顶点出发,能走全全部点 2.最少连几条边,使得图强连通 思路: #inc ...
- poj 1236 Network of Schools(又是强连通分量+缩点)
http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- poj 1236 Network of Schools(连通图入度,出度为0)
http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- [tarjan] poj 1236 Network of Schools
主题链接: http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K To ...
- POJ 1236 Network of Schools(Tarjan缩点)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16806 Accepted: 66 ...
- poj 1236 Network of Schools(tarjan+缩点)
Network of Schools Description A number of schools are connected to a computer network. Agreements h ...
- POJ 1236 Network Of Schools (强连通分量缩点求出度为0的和入度为0的分量个数)
Network of Schools A number of schools are connected to a computer network. Agreements have been dev ...
随机推荐
- Java编程思想之十七 容器深入研究
17.1 完整的容器分类方法 17.2 填充容器 import java.util.*; class StringAddress { private String s; public StringAd ...
- 解释一下一门语言该有的东东(Javascript)
注释 Js中有两种注释 // 单行注释 /**/ 多行注释 变量 变量就像学校学习的 未知数 如 3 + x = 8 x: 类似变量,在改造一下 x + y = z 当 x=3, y=5, z=8, ...
- 使用ForkJoinPool来多线程的拆分任务,执行任务,合并结果。
ForkJoinPool 是jdk1.7 由Doug Lea 写的实现 递归调用任务拆分,合并,的线程池. 代码示例: package www.itbac.com; import com.alib ...
- JS 自执行函数
由于自己js基础知识薄弱,很多js的知识还没有掌握,所以接下来会经常写一些关于js基础知识的博客,也算给自己提个醒吧. js自执行函数,听到这个名字,首先会联想到函数.接下来,我来定义一个函数: fu ...
- DesignPattern系列__06迪米特原则
迪米特原则定义 迪米特原则,也叫最少知道原则,即一个类应该对自己依赖的类知道的越少越好,而你被依赖的类多么复杂,对我都没有关系.也就是说,对于别依赖的类来说,不管业务逻辑多么复杂,都应该尽量封装在类的 ...
- codeforces679A_Bear and Prime 100 交互题
传送门 第一道交互题 题意: 电脑事先想好了一个数[,] 你会每次问电脑一个数是否是它想的那个数的因数 电脑会告诉你yes或no 至多询问20次 最后要输出它想的数是质数还是合数 思路: 枚举< ...
- Spring入门(七):Spring Profile使用讲解
1. 使用场景 在日常的开发工作中,我们经常需要将程序部署到不同的环境,比如Dev开发环境,QA测试环境,Prod生产环境,这些环境下的一些配置肯定是不一样的,比如数据库配置,Redis配置,Rabb ...
- Python3基本数据类型之列表
1.初识列表 列表(List)是Python3中的"容器型"数据类型. 列表通过中括号把一堆数据括起来的方式形成,列表的长度不限. 列表里面的元素可以是不同的数据类型,但是一般是相 ...
- 源码解读 Spring Boot Profiles
前言 上文<一文掌握 Spring Boot Profiles> 是对 Spring Boot Profiles 的介绍和使用,因此本文将从源码角度探究 Spring Boot Profi ...
- Kubernetes 服务发现
目录 什么是服务发现? 环境变量 DNS 服务 Linux 中 DNS 查询原理 Kubernetes 中 DNS 查询原理 调试 DNS 服务 存根域及上游 DNS 什么是服务发现? 服务发现就是一 ...