Network of Schools
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 15211   Accepted: 6040

Description

A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a list of schools to which it distributes software (the “receiving schools”). Note that if B is in the distribution list of school A, then A does not necessarily appear in the list of school B 
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 first line contains an integer N: the number of schools in 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

Your program should write two lines to the standard output. The 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

题意:

N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输,问题1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件。2,至少需要添加几条传输线路(边),使任意向一个学校发放软件后,经过若干次传送,网络内所有的学校最终都能得到软件。

给定一个有向图,求:

1) 至少要选几个顶点,才能做到从这些顶点出发,可以到达全部顶点

2) 至少要加多少条边,才能使得从任何一个顶点出发,都能到达全部顶点

思路:

问题1答案就是入度为0的点的个数。

在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)就是第二个问题的解。

/*
* Author: sweat123
* Created Time: 2016/6/25 15:49:19
* File Name: main.cpp
*/
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<time.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1<<30
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define pi acos(-1.0)
using namespace std;
const int MAXN = ;
struct node{
int from;
int to;
int next;
}edge[MAXN*];
int pre[MAXN],vis[MAXN],dfn[MAXN],low[MAXN],n,m,ind;
int f[MAXN],siz[MAXN],num,dep,out[MAXN],in[MAXN];
stack<int>s;
void add(int x,int y){
edge[ind].from = x;
edge[ind].to = y;
edge[ind].next = pre[x];
pre[x] = ind ++;
}
void dfs(int rt){
dfn[rt] = low[rt] = ++dep;
vis[rt] = ;
s.push(rt);
for(int i = pre[rt]; i != -; i = edge[i].next){
int t = edge[i].to;
if(!dfn[t]){
dfs(t);
low[rt] = min(low[rt],low[t]);
} else if(vis[t]){
low[rt] = min(low[rt],dfn[t]);
}
}
if(low[rt] == dfn[rt]){
++num;
while(!s.empty()){
int tp = s.top();
s.pop();
vis[tp] = ;
f[tp] = num;
siz[num] ++;
if(tp == rt)break;
}
}
}
void setcc(){
num = ;
dep = ;
memset(in,,sizeof(in));
memset(out,,sizeof(out));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
for(int i = ; i <= n; i++){
if(!dfn[i]){
dfs(i);
}
}
int ret = ind;
for(int i = ; i < ret; i++){
int x = f[edge[i].from];
int y = f[edge[i].to];
if(x == y)continue;
out[x] ++;
in[y] ++;
}
int ans1,ans2;
ans1 = ans2 = ;
for(int i = ; i <= num; i++){
if(in[i] == ){
ans1 ++;
}
if(out[i] == ){
ans2 ++;
}
}
ans2 = max(ans2,ans1);
if(num == )ans2 = ;
printf("%d\n%d\n",ans1,ans2);
}
int main(){
while(~scanf("%d",&n)){
ind = ;
while(!s.empty())s.pop();
memset(pre,-,sizeof(pre));
memset(vis,,sizeof(pre));
memset(f,-,sizeof(f));
for(int i = ; i <= n; i++){
while(){
int x;
scanf("%d",&x);
if(x == )break;
add(i,x);
}
}
setcc();
}
return ;
}

poj1236 强连通缩点的更多相关文章

  1. poj1236强连通缩点

    题意:给出每个学校的list 代表该学校能链接的其他学校,问1:至少给几个学校资源使所有学校都得到:2:至少加多少个边能让所有学校相互连通: 思路:1:找出缩点后入度为零的点个数  2:找出缩点后入度 ...

  2. poj2553 强连通缩点

    The Bottom of a Graph Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 10114   Accepted: ...

  3. hdu 4635 Strongly connected 强连通缩点

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4635 题意:给你一个n个点m条边的图,问在图不是强连通图的情况下,最多可以向图中添多少条边,若图为原来 ...

  4. poj1236 强连通

    题意:有 n 个学校每个学校可以将自己的软件共享给其他一些学校,首先,询问至少将软件派发给多少学校能够使软件传播到所有学校,其次,询问添加多少学校共享关系可以使所有学校的软件能够相互传达. 首先,第一 ...

  5. BZOJ 1051: [HAOI2006]受欢迎的牛 强连通缩点

    题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=1051 题解: 强连通缩点得到DAG图,将图转置一下,对入度为零的点跑dfs看看能不能访问 ...

  6. hdu 2767 Proving Equivalences 强连通缩点

    给出n个命题,m个推导,问最少添加多少条推导,能够使全部命题都能等价(两两都能互推) 既给出有向图,最少加多少边,使得原图变成强连通. 首先强连通缩点,对于新图,每一个点都至少要有一条出去的边和一条进 ...

  7. UVA - 11324 The Largest Clique 强连通缩点+记忆化dp

    题目要求一个最大的弱联通图. 首先对于原图进行强连通缩点,得到新图,这个新图呈链状,类似树结构. 对新图进行记忆化dp,求一条权值最长的链,每一个点的权值就是当前强连通分量点的个数. /* Tarja ...

  8. poj-1904(强连通缩点)

    题意:有n个王子,每个王子都有k个喜欢的女生,王子挑选喜欢的女生匹配,然后再给你n个王子最开始就定好的匹配,每个王子输出能够结合且不影响其他王子的女生匹配 解题思路:强连通缩点,每个王子与其喜欢的女生 ...

  9. NOIP2017提高组Day1T3 逛公园 洛谷P3953 Tarjan 强连通缩点 SPFA 动态规划 最短路 拓扑序

    原文链接https://www.cnblogs.com/zhouzhendong/p/9258043.html 题目传送门 - 洛谷P3953 题目传送门 - Vijos P2030 题意 给定一个有 ...

随机推荐

  1. Collider Collision 区别

    Collision 中带有碰撞的信息,例如:速度和撞击到的点 示例 void OnCollisionEnter2D(Collision2D coll) { foreach(ContactPoint c ...

  2. [No000065]python 获取当前时间

    要取的当前时间的话,要取得当前时间的时间戳,时间戳好像是1970年到现在时间相隔的时间.用下面的方式来取得当前时间的时间戳: import time print(time.time()) 输出的结果是 ...

  3. java 25 - 5 网络编程之多线程实现聊天室

    平时聊天都是在同一个窗口的,所以,这个窗口同时实现发送数据和接收数据,这时就需要多线程实现. 建立一个类: 把聊天的发送端和接收端放在同一个类,启动一个窗口 public class CharRoom ...

  4. 技术专题—Python黑客【优质内容聚合贴】

    作者:坏蛋链接:https://zhuanlan.zhihu.com/p/24645819来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 一.前言 本着知识分享,聚合优 ...

  5. android之android.intent.category.DEFAULT的用途和使用

    1.要弄清楚这个问题,首先需要弄明白什么是implicit(隐藏) intent什么是explicit(明确) intent. Explicit Intent明确的指定了要启动的Acitivity , ...

  6. 见鬼了,swiper

    1.今天不知怎么swiper的onInit函数不起作用,怎么弄都不行: 把以前能行的案例的包都导进去还是不行,但是onSlideChangeEnd可以触发,晕死了.... 不,它触发了一次onInit ...

  7. Github 简明教程

    http://www.runoob.com/w3cnote/git-guide.html http://rogerdudler.github.io/git-guide/index.zh.html

  8. try catch finally的执行顺序(有return的情况下)

    结论:1.不管有木有出现异常,finally块中代码都会执行:2.当try和catch中有return时,finally仍然会执行:3.finally是在return后面的表达式运算后执行的(此时并没 ...

  9. 2795: [Poi2012]A Horrible Poem

    2795: [Poi2012]A Horrible Poem Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 484  Solved: 235[Subm ...

  10. 用微信小程序做H5游戏尝试

    微信小程序发布后,公司虽然没有拿到第一批内测资格,但作为微信亲密合作伙伴,一定要第一时间去尝试啦.现在微信小程序刚发布还在测试阶段,可以说是1.0版本,所以框架和结构内容都还不多,相关的文档跟微信AP ...