POJ 1236 Network Of Schools (强连通分量缩点求出度为0的和入度为0的分量个数)
Network of Schools
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
the network (2 <= N <= 100). The schools are identified by the
first N positive integers. Each of the next N lines describes a list of
receivers. The line i+1 contains the identifiers of the receivers of
school i. Each list ends with a 0. An empty list contains a 0 alone in
the line.
Output
first line should contain one positive integer: the solution of subtask
A. The second line should contain the solution of subtask B.
Sample Input
5
2 4 3 0
4 5 0
0
0
1 0
Sample Output
1
2
给定一个有向图,求:
1) 至少要选几个顶点,才能做到从这些顶点出发,可以到达全部顶点
2) 至少要加多少条边,才能使得从任何一个顶点出发,都能到达全部顶点
顶点数<= 100
解题思路:
1. 求出所有强连通分量
2. 每个强连通分量缩成一点,则形成一个有向无环图DAG。
3. DAG上面有多少个入度为0的顶点,问题1的答案就是多少
在DAG上要加几条边,才能使得DAG变成强连通的,问题2的答案就是多少
加边的方法:
要为每个入度为0的点添加入边,为每个出度为0的点添加出边
假定有 n 个入度为0的点,m个出度为0的点,如何加边?
把所有入度为0的点编号 0,1,2,3,4 ....N -1
每次为一个编号为i的入度0点可达的出度0点,添加一条出边,连到编号为(i+1)%N 的那个出度0点,
这需要加n条边
若 m <= n,则
加了这n条边后,已经没有入度0点,则问题解决,一共加了n条边
若 m > n,则还有m-n个入度0点,则从这些点以外任取一点,和这些点都连上边,即可,这还需加m-n条边。
所以,max(m,n)就是第二个问题的解
此外:当只有一个强连通分支的时候,就是缩点后只有一个点,虽然入度出度为0的都有一个,但是实际上不需要增加清单的项了,所以答案是1,0;
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
typedef long long ll;
using namespace std;
const int N = ;
const int M = ;
int vis[N],dfn[N],low[N],head[N],stack1[N],num[N],out[N],in[N];
int cost[N];
int n,m,tot,son,maxn,tim,top,cut;
int ans;
struct EDG{int to,next;}edg[N*N];
struct node{ll x,y,r,c;}a[N];
bool cmp(node f,node g){return f.c<g.c;}
void add(int u,int v){
edg[tot].to=v;edg[tot].next=head[u];head[u]=tot++;
}
void init(){
met(head,-);
tot=tim=top=cut=;
met(vis,);
met(edg,);
met(out,);met(in,);
met(cost,inf);
met(stack1,);met(num,);met(dfn,);met(low,);
}
void Tarjan(int u) {
int v;
low[u] = dfn[u] = ++tim;
stack1[top++] = u;
vis[u] = ;
for(int e = head[u]; e != -; e = edg[e].next){
v = edg[e].to;
if(!dfn[v]){
Tarjan(v);
low[u] = min(low[u], low[v]);
}else if(vis[v]){
low[u] = min(low[u], dfn[v]);
}
}
if(low[u] == dfn[u]){
cut++;
do{
v = stack1[--top];
num[v] = cut;
vis[v] = ;
}while(u != v);
}
}
int main() {
while(~scanf("%d",&n)){
init();
int u,v,ret=;
for(int i=;i<=n;i++){
while(~scanf("%d",&u)&&u){
add(i,u);
}
}
for(int i=;i<=n;i++){
if(!dfn[i])Tarjan(i);
}
for(int i=; i<=n; i++) {
for(int j=head[i]; j!=-; j=edg[j].next) {
int v=edg[j].to;
if(num[i]!=num[v])out[num[i]]++,in[num[v]]++;
}
}
ans=;
if(cut==)printf("1\n0\n");
else {
for(int i=;i<=cut;i++){
if(!out[i])ans++;
if(!in[i])ret++;
}
printf("%d\n%d\n",ret,max(ret,ans));
}
}
return ;
}
POJ 1236 Network Of Schools (强连通分量缩点求出度为0的和入度为0的分量个数)的更多相关文章
- POJ 1236 Network of Schools (强连通分量缩点求度数)
题意: 求一个有向图中: (1)要选几个点才能把的点走遍 (2)要添加多少条边使得整个图强联通 分析: 对于问题1, 我们只要求出缩点后的图有多少个入度为0的scc就好, 因为有入度的scc可以从其他 ...
- 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 强连通入门题
一些学校连接到计算机网络.这些学校之间已经达成了协议: 每所学校都有一份分发软件的学校名单("接收学校"). 请注意,如果B在学校A的分发名单中,则A不一定出现在学校B的名单中您需 ...
- 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 (Tarjan)
题目链接: Poj 1236 Network of Schools 题目描述: 有n个学校,学校之间有一些单向的用来发射无线电的线路,当一个学校得到网络可以通过线路向其他学校传输网络,1:至少分配几个 ...
- 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 (强连通分量+缩点)
题目大概: 每个学校都可以把软件复制好,交给它名单上的学校. 问题A:把软件复制成几份,然后交给不同的学校,所有学校才能够都有软件. 问题B:添加几条边,能使得这个图变成强连通图. 思路: 找出所有的 ...
随机推荐
- BZOJ1009: [HNOI2008]GT考试 矩阵快速幂+kmp+dp
这个题你发现打暴力的话可以记忆化搜素加剪枝,那么意味着可以递推,我们搜的话就是1010^9我们就往下匹配遇到匹配成功就return,那么我们可以想一下什么决定了状态,我们考虑kmp的过程,对于我们目前 ...
- [HEOI2017]分手是祝愿 期望概率dp 差分
经分析可知:I.操作每个灯可看做一种异或状态 II.每个状态可看做是一些异或状态的异或和,而且每个异或状态只能由它本身释放或放入 III.每一种异或状态只有存在不存在两中可行状态,因此这些灯只有同时处 ...
- Android tips tool 发现的性能问题(转载翻译)
先翻译刚好在研究到的一段,其余的无限期待续. 1.ObsoleteLayoutParam不起作用的标签 Invalid layout param in a LinearLayout: layout_c ...
- angular js module 的理解
module其实就是一个容器,里面可以装controller,service,directive,filter等, 官网的解释是:Module :A container for the differe ...
- Vue2.0关于生命周期和钩子函数
Vue生命周期简介: Vue1.0+和Vue2.0在生命周期钩子上的区别还是很大的,如下: 代码验证: <!DOCTYPE html> <html> <head& ...
- Linux下部署weblogic应用
1.Linux下weblogic安装 2.Linux下设置weblogic监听服务器地址(默认为本机) 1).修改domain\config\config.xml文件 修改 <server> ...
- c语言指针学习【转】
前言 近期俄罗斯的陨石.四月的血月.五月北京的飞雪以及天朝各种血腥和混乱,给人一种不详的预感.佛祖说的末法时期,五浊恶世 ,十恶之世,人再无心法约束,道德沦丧,和现在正好吻合.尤其是在天朝,空气,水, ...
- PHP文件操作函数二
PHP部分文件访问函数总结: 1.filetype("文件路径") //可以输出相关文件类型,返回之为:dir/file... 2.stat("文件名") / ...
- MDK stm32 仿真
直接选择simulator,仿真时报错 *** error 65: access violation at 0x40021000 : no 'read' permission 修改系统配置,原配置如下 ...
- TCP/IP Http的区别
TPC/IP协议是传输层协议,主要解决数据如何在网络中传输,而HTTP是应用层协议,主要解决如何包装数据. 关于TCP/IP和HTTP协议的关系,网络有一段比较容易理解的介绍:“我们在传输数据时,可以 ...