POJ 1236——Network of Schools——————【加边形成强连通图】
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u
Description
You are to write a program that computes the minimal number of schools that must receive a copy of the new software in order for the software to reach all schools in the network according to the agreement (Subtask A). As a further task, we want to ensure that by sending the copy of new software to an arbitrary school, this software will reach all schools in the network. To achieve this goal we may have to extend the lists of receivers by new members. Compute the minimal number of extensions that have to be made so that whatever school we send the new software to, it will reach all other schools (Subtask B). One extension means introducing one new member into the list of receivers of one school.
Input
Output
Sample Input
5
2 4 3 0
4 5 0
0
0
1 0
Sample Output
1
2 题目大意:给你n个学校,每个学校有一个接收表,表示城市i向表内城市都可以发送文件。现在要发送文件给所有学校,问题A:问你至少发给几个学校就可以通过学校间的发送,让所有学校都能收到文件。问题B:问你发个任意一个学校,要让所有学校都能收到文件,需要最少加多少条有向边。 解题思路:问题A,其实就是让求对于强连通分量缩点后,有多少个缩点的入度为0。问题B:其实就让你求至少加入多少条有向边,可以让原图形成强连通图。问题B,对于缩点,我们统计入度和出度。对于入度为0的,我们理论上应该给它加一条入边,对于出度为0的,我们应该给它加一条出边。但是要求最少加边数,那么我们就可以让入度为0的和出度为0的各取所需,即从出度为0的缩点向入度为0的缩点连一条边。对于最后可能会剩下的一个点,让他另外加一条边即可。那么我们的答案就很明显了,即max(入度为0的缩点个数,出度为0的缩点个数)。
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
#include<stack>
#include<iostream>
using namespace std;
const int maxn = 210;
vector<int>G[maxn];
int pre[maxn],lowlink[maxn],sccno[maxn],dfs_clock,scc_cnt;
stack<int>S;
int indeg[maxn], outdeg[maxn];
void dfs(int u){
pre[u] = lowlink[u] = ++dfs_clock;
S.push(u);
for(int i = 0; i < G[u].size(); i++){
int v = G[u][i];
if(!pre[v]){
dfs(v);
lowlink[u] = min(lowlink[u],lowlink[v]);
}else if(!sccno[v]){
lowlink[u] = min(lowlink[u],pre[v]);
}
}
if(lowlink[u] == pre[u]){
scc_cnt++;
for(;;){
int x = S.top(); S.pop();
sccno[x] = scc_cnt;
if(x == u) break;
}
}
}
void find_scc(int n){
dfs_clock = scc_cnt = 0;
memset(sccno,0,sizeof(sccno));
memset(pre,0,sizeof(pre));
for(int i = 1; i <= n; i++){
if(!pre[i]) dfs(i);
}
}
int main(){
int n;
scanf("%d",&n);
for(int i = 1; i <= n; i++){
int v ;
for(;;){
scanf("%d",&v);
if(v == 0){
break;
}
G[i].push_back(v);
}
}
find_scc(n);
if(scc_cnt == 1){
puts("1");
puts("0");
}else{
int sccout, sccin;
for(int i = 1; i <= n; ++i){
for(int j = 0; j < G[i].size(); j++){
int v = G[i][j];
sccout = sccno[i];
sccin = sccno[v];
if(sccout == sccin){
continue;
}
outdeg[sccout]++;
indeg[sccin]++;
}
}
int ans1 = 0, ans2 = 0;
for(int i = 1; i <= scc_cnt; i++){
if(indeg[i] == 0){
ans1++;
}
if(outdeg[i] == 0){
ans2++;
}
}
printf("%d\n%d\n",ans1,max(ans1,ans2));
}
return 0;
}
POJ 1236——Network of Schools——————【加边形成强连通图】的更多相关文章
- Poj 1236 Network of Schools (Tarjan)
题目链接: Poj 1236 Network of Schools 题目描述: 有n个学校,学校之间有一些单向的用来发射无线电的线路,当一个学校得到网络可以通过线路向其他学校传输网络,1:至少分配几个 ...
- POJ 1236 Network of Schools(强连通 Tarjan+缩点)
POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意: 给定一张有向图,问最少选择几个点能遍历全图,以及最少加入�几条边使得 ...
- POJ 1236 Network of Schools(强连通分量)
POJ 1236 Network of Schools 题目链接 题意:题意本质上就是,给定一个有向图,问两个问题 1.从哪几个顶点出发,能走全全部点 2.最少连几条边,使得图强连通 思路: #inc ...
- poj 1236 Network of Schools(连通图入度,出度为0)
http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- poj 1236 Network of Schools(又是强连通分量+缩点)
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【强连通求孤立强连通分支个数&&最少加多少条边使其成为强连通图】
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13800 Accepted: 55 ...
- 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 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12240 Accepted: 48 ...
随机推荐
- 洛谷P3709 大爷的字符串题(莫队)
题目背景 在那遥远的西南有一所学校 /*被和谐部分*/ 然后去参加该省省选虐场 然后某蒟蒻不会做,所以也出了一个字符串题: 题目描述 给你一个字符串a,每次询问一段区间的贡献 贡献定义: 每次从这个区 ...
- Codeforces Round #533 (Div. 2)题解
link orz olinr AK Codeforces Round #533 (Div. 2) 中文水平和英文水平都太渣..翻译不准确见谅 T1.给定n<=1000个整数,你需要钦定一个值t, ...
- CentOS7l联网
原文:https://blog.csdn.net/nothing2017/article/details/61420767 步骤: 1.以root管理员身份登录系统,输入 --->(ls / ...
- python安装出现的证书问题
1. pip install pyenv 安装时出现下图错误 Could not install packages due to an EnvironmentError: HTTPSConnectio ...
- 商品录入功能v1.0【持续优化中...】
# 录入商品 def goods_record(): print("欢迎使用铜锣辉的购物商城[商品管理][录入商品]".center(30, "*")) whi ...
- 读经典——《CLR via C#》(Jeffrey Richter著) 笔记_高级管理控制(配置)
一个应用程序的XML配置文件示例: <?xml version="1.0"?> <configuration> <runtime> <as ...
- jquery 拖动改变div大小
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Go语言基础之13--线程安全及互斥锁和读写锁
一.线程安全介绍 1.1 现实例子 A. 多个goroutine同时操作一个资源,这个资源又叫临界区 B. 现实生活中的十字路口,通过红路灯实现线程安全 C. 火车上的厕所(进去之后先加锁,在上厕所, ...
- MetricStatTimer
package org.apache.storm.metric.internal; import java.util.Timer; /** * Just holds a singleton metri ...
- vue(5)联动+tab页
来自:https://juejin.im/post/5a0c191f6fb9a04514639419 1.联动 新增 <input v-model="msg" /> & ...