POJ 1236 Network of Schools
2016-04-21 18:59
316 查看
http://poj.org/problem?id=1236 Network of Schools
DescriptionA 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.
InputThe 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.OutputYour 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 Input5
2 4 3 0
4 5 0
0
0
1 0
Sample Output1
2
Sourcetarjan 求scc图。第一问是求scc图中入度为0的顶点数目。第二问是求scc图中入读为0 的点和出度为0 的点的数目的最大值。
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 11919 | Accepted: 4750 |
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.
InputThe 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.OutputYour 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 Input5
2 4 3 0
4 5 0
0
0
1 0
Sample Output1
2
Sourcetarjan 求scc图。第一问是求scc图中入度为0的顶点数目。第二问是求scc图中入读为0 的点和出度为0 的点的数目的最大值。
#include<iostream> #include<cstring> #include<algorithm> #include<cstdlib> #include<vector> #include<cmath> #include<stdlib.h> #include<iomanip> #include<list> #include<deque> #include<map> #include <stdio.h> #include <queue> #include <stack> #define maxn 100+5 #define ull unsigned long long #define ll long long #define reP(i,n) for(i=1;i<=n;i++) #define rep(i,n) for(i=0;i<n;i++) #define cle(a) memset(a,0,sizeof(a)) #define mod 90001 #define PI 3.141592657 #define INF 1<<30 const ull inf = 1LL << 61; const double eps=1e-5; using namespace std; bool cmp(int a,int b) { return a>b; } vector<int>G[maxn]; int pre[maxn],lowlink[maxn],sccno[maxn],dfs_clock,scc_cnt; stack<int>S; int n; int outdegree[maxn],indegree[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; cle(sccno); cle(pre); for(int i=1;i<=n;i++) if(!pre[i])dfs(i); } void solve() { find_scc(n); int in=0,out=0; if(scc_cnt==1) { printf("%d\n%d\n",1,0); } else { cle(outdegree),cle(indegree); for(int i=1;i<=n;i++) for(int j=0;j<G[i].size();j++) { int k=G[i][j]; if(sccno[i]!=sccno[k]) { outdegree[sccno[i]]++; indegree[sccno[k]]++; } } for(int i=1;i<=scc_cnt;i++) { if(indegree[i]==0)in++; if(outdegree[i]==0)out++; } printf("%d\n%d\n",in,max(in,out)); } } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif //freopen("out.txt","w",stdout); while(cin>>n) { int a,b; for(int i=1;i<=n;i++) { while(1) { scanf("%d",&a); if(a==0)break; G[i].push_back(a); } } solve(); } return 0; }
相关文章推荐
- Tarjan算法
- HDU 5174 Ferries Wheel
- 加载顺序,一个类的
- HDU 1556 Color the ball
- POJ 1394 Minimum Inversion Number
- POJ 2299 Ultra-QuickSort
- HDU 2492 Ping pong
- 冒泡排序 快速排序
- 20160421javaweb之上传下载小案例---网盘
- POJ 2155 Matrix(二维线段树)
- POJ 3321 Apple Tree
- HDU 3974 Assign the task
- HDU 1698 Just a Hook
- POJ 3368 Frequent values (RMQ)
- POJ 3264 Balanced Lineup(RMQ)
- Can you find it?(二分)
- POJ 3468 A Simple Problem with Integers(区间更新)
- POJ 2828 Buy Tickets
- POJ 2528 Mayor\'s posters
- HDU 5166 Missing number