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 ...
随机推荐
- JS-数组的定义
- python 爬取豆瓣电影评论,并进行词云展示及出现的问题解决办法
本文旨在提供爬取豆瓣电影<我不是药神>评论和词云展示的代码样例 1.分析URL 2.爬取前10页评论 3.进行词云展示 1.分析URL 我不是药神 短评 第一页url https://mo ...
- win10去除快捷方式小箭头
切忌删除注册表项: HKEY_CLASSES_ROOT -> lnkfile -> IsShortcut 这个方法以前是可以的,但是在2018年之后更新的系统就会出现任务栏图标打不开的情况 ...
- Mobile game forensics
My friend Carrie'd like to know "Garena 传说对决" violates any mobile risks such as insecure d ...
- 一文搞懂 Prometheus 的直方图
原文链接:一文搞懂 Prometheus 的直方图 Prometheus 中提供了四种指标类型(参考:Prometheus 的指标类型),其中直方图(Histogram)和摘要(Summary)是最复 ...
- c#实现深拷贝的几种方法
为什么要用到深拷贝呢?比如我们建了某个类Person,并且实例化出一个对象,然后,突然需要把这个对象复制一遍,并且复制出来的对象要跟之前的一模一样,来看下我们一般会怎么做,看代码 public cla ...
- Spring Boot Security Oauth2之客户端模式及密码模式实现
Spring Boot Security Oauth2之客户端模式及密码模式实现 示例主要内容 1.多认证模式(密码模式.客户端模式) 2.token存到redis支持 3.资源保护 4.密码模式用户 ...
- 《机器学习基石》---VC维
1 VC维的定义 VC维其实就是第一个break point的之前的样本容量.标准定义是:对一个假设空间,如果存在N个样本能够被假设空间中的h按所有可能的2的N次方种形式分开,则称该假设空间能够把N个 ...
- 自定义Dialog---实现优美对话框
PS:自定义dialog,一些系统的dialog已经不能满足开发人员的需求了,所以,我们需要自定义一个属于并且适合自己项目的对话框,无论是颜色还是功能需求上都是和自己的项目紧密相关的,一些系统的对话框 ...
- Django Mysql数据库-F查询和Q查询
一.F查询和Q查询 F查询: 在上面所有的例子中,我们构造的过滤器都只是将字段值与某个常量做比较.如果我们要对两个字段的值做比较,那该怎么做呢? Django 提供 F() 来做这样的比较.F() 的 ...