poj 1236 强连通分量+缩点
来源:互联网 发布:swps软件怎么用 编辑:程序博客网 时间:2024/05/21 04:25
Network of Schools
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 12273 Accepted: 4887
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.
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
52 4 3 04 5 0001 0
Sample Output
12
Source
IOI 1996
52 4 3 04 5 0001 0#include <iostream>#include <cstring>#include <string>#include <cstdio>#include <algorithm>#include <vector>#include <set>#include <map>#include <stack>#include <queue>using namespace std;#define maxn 101#define maxm 20005int n;int to[maxm],next[maxm],first[maxn],tot;int rto[maxm], rnext[maxm],rfirst[maxn], rtot;int vs[maxn],cnt=0;bool used[maxn];int cmp[maxn];int ind[maxn];int outd[maxn];void init(){ memset(first, -1, sizeof(first)); memset(rfirst, -1, sizeof(rfirst)); tot=rtot=0; cnt=0;}void add(int u, int v){ to[tot]=v; next[tot]=first[u]; first[u]=tot++; rto[rtot]=u; rnext[rtot]=rfirst[v]; rfirst[v]=rtot++;}void dfs(int u){ int v; used[u]=1; for(int i=first[u]; i!=-1; i=next[i]){ v=to[i]; if(!used[v]) dfs(v); } vs[cnt++]=u;}void rdfs(int u, int k){ int v; cmp[u]=k; used[u]=1; for(int i=rfirst[u]; i!=-1; i=rnext[i]){ v=rto[i]; if(!used[v]) rdfs(v, k); }}int scc(int n){ cnt=0; memset(used,0, sizeof(used)); for(int i=1; i<=n; i++) if(!used[i]){ dfs(i); } memset(used, 0, sizeof(used)); cnt=0; for(int i=n-1; i>=0; i--) if(!used[vs[i]]) rdfs(vs[i], cnt++); return cnt;}int main(){ while(scanf("%d", &n)==1){ init(); int u,v; memset(ind, 0, sizeof(ind)); memset(outd, 0, sizeof(outd)); for(int i=0; i<n; i++){ u=i+1; while(scanf("%d", &v)==1 && v){ add(u,v); } } int num=scc(n); for(int i=1; i<=n; i++){ u=i; for(int j=first[u]; j!=-1; j=next[j]){ v=to[j]; if(cmp[u]!=cmp[v]) ind[cmp[v]]++,outd[cmp[u]]++; } } int ansa=0, ansb=0; for(int i=0; i<num; i++){ if(!ind[i]) ansa++; if(!outd[i]) ansb++; } ansb=max(ansa, ansb); if(num==1) ansb=0; printf("%d\n%d\n", ansa,ansb); } return 0;}
Sample Output
12
Source
IOI 1996
0 0
- poj 1236 强连通分量 缩点
- poj 1236 强连通分量+缩点
- poj 2186 强连通分量 缩点
- POJ 2186(强连通分量 缩点)
- POJ 1236 强连通分量+缩点+入度出度
- POJ 1236 Network of Schools强连通分量缩点
- POJ 1236 Network of Schools (强连通分量+缩点)
- POJ 1236 Network of Schools 强连通分量+缩点
- poj 1236 Network of Schools (强连通分量+缩点)
- poj 1236 Network of Schools(强连通分量+缩点)
- poj 1236 Network of Schools(强连通分量缩点)
- POJ-1236-Network of Schools【强连通分量】【缩点】
- POJ 1236(tarjan 强连通分量 缩点)
- POJ 1236 Network of Schools(强连通分量,缩点)
- 强连通分量缩点
- 强连通分量缩点
- 【连通图|强连通分量+缩点】POJ-1236 Network of Schools
- poj 1236【强连通分量】
- hdu 3908
- [Practical.Vim(2012.9)].Drew.Neil.Tip51 学习摘要
- git 在提交之前撤销add操作
- ios 数据的存储
- Java8 Lambda表达式入门
- poj 1236 强连通分量+缩点
- 黑马程序员——OC内存管理
- hdu 1232 并查集
- c语言文件读写形式区别
- Java Web 高性能开发,第 2 部分: 前端的高性能
- n&(n-1)的运用——二进制数中1的个数、判断它是否是2的方幂
- 《C++编程》第3章第20题
- 数据挖掘算法之关联规则挖掘(一)apriori算法
- Eclipse工具使用