Network of Schools+强联通分量+POJ
2014-08-04 11:25
225 查看
Network of Schools
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
Sample Output
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 10910 | Accepted: 4346 |
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
解决方案:wa了几遍,一直没想明白第二个问,后来看了discuss,才知道一个规律,要添加的最少边数为max(in,out)即出度为0的个数和入度为0的个数两者的最大值,连的时候,要保证每个出度为0的店或每个入度为0的点都连上,这样就能保证整图成一个强联通图。
code:#include<iostream> #include<cstdio> #include<vector> #include<stack> #include<cstring> #define MMAX 103 using namespace std; int low[MMAX],dfn[MMAX],instack[MMAX],fa[MMAX],in[MMAX],out[MMAX],ii[MMAX],oo[MMAX]; int cnt; vector<int>Map[MMAX]; stack<int>S; int N,index; void init() { index=0; cnt=0; for(int i=0; i<=N; i++) { Map[i].clear(); dfn[i]=0; low[i]=0; in[i]=0; out[i]=0; fa[i]=i; oo[i]=0; ii[i]=0; } memset(instack,0,sizeof(instack)); while(!S.empty())S.pop(); } void tarjan(int u) { low[u]=dfn[u]=index++; instack[u]=1; S.push(u); int len=Map[u].size(); for(int i=0; i<len; i++) { int v=Map[u][i]; if(!instack[v]) { tarjan(v); low[u]=min(low[v],low[u]); } else if(instack[v]==1) { low[u]=min(low[v],low[u]); } } if(low[u]==dfn[u]) { while(!S.empty()) { int temp=S.top(); S.pop(); fa[temp]=u; instack[temp]=2; if(temp==u) break; } cnt++; } } int main() { while(~scanf("%d",&N)) { init(); for(int i=1; i<=N; i++) { while(1) { int a; scanf("%d",&a); if(a==0) break; Map[i].push_back(a); } } for(int i=1; i<=N; i++) { if(!instack[i]) { tarjan(i); } } for(int i=1; i<=N; i++) { int len=Map[i].size(); for(int j=0; j<len; j++) { int v=Map[i][j]; if(fa[v]!=fa[i]) { in[fa[v]]++,out[fa[i]]++; } } } int _in=0,_out=0; for(int i=1; i<=N; i++) { if(!in[fa[i]]) { ii[fa[i]]=1; } if(!out[fa[i]]) { oo[fa[i]]=1; } } for(int i=1;i<=N;i++){ _in+=ii[i]; _out+=oo[i]; } if(cnt==1) cout<<_in<<"\n"<<'0'<<endl; else cout<<_in<<"\n"<<max(_in,_out)<<endl; } return 0; }
相关文章推荐
- poj2942(双联通分量,交叉染色判二分图)
- POJ 2186 Popular Cows (强联通分量)
- POJ 3114 强联通分量+最短路
- POJ 2553 The Bottom of a Graph 缩点之后求出度为0的强联通分量的元素
- POJ 3180 强联通分量
- 【POJ 3352】 Road Construction(边联通分量入门)
- poj 3694 Network (强联通分量缩点+割桥+lca查询)
- POJ 1904 强联通分量
- poj-1236-Network of Schools-强联通分量
- POJ 2942 Tarjan双联通分量+二分图 解题报告
- POJ 3352 Road Construction 双联通分量 难度:1
- POJ 2942:Knights of the Round Table tarjan点双联通分量 二分图染色找奇环
- The Bottom of a Graph POJ - 2553 强联通分量中出度为零的点集
- POJ 2186 强联通分量
- POJ3352 Road Construction【边双联通分量】【Tarjan】
- POJ 2942 Knights of the Round Table 补图+tarjan求点双联通分量+二分图染色+debug
- poj 3180 求有多少个强联通分量
- POJ 3114 Countries in War(强联通分量+Tarjan)
- poj 1904 King's Quest 二分图中强联通分量的运用
- poj 2186 Popular Cows (强联通分量+缩点)