POJ_1236
2017-04-10 14:39
148 查看
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
Source
IOI 1996
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 17659 | Accepted: 6972 |
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
Source
IOI 1996
#include<iostream> #include<algorithm> #include<vector> #include<cstring> #include<stack> using namespace std; const int maxn=105; const int maxe=105*105; vector<int>E[maxn]; stack<int>S; int dfn[maxn],low[maxn],vis[maxn],belong[maxn],in[maxn],out[maxn]; int n,cnt=1,times=0; void tarjan(int x){ int y,j; times++;t++; dfn[x]=low[x]=times; S.push(x);vis[x]=1; for(int i;i<E[x].size();i++){ int v=E[x][i]; if(!dfn[v]){ tarjan(v); low[x]=min(low[x],low[v]); } else if(vis[v]){ low[x]=min(low[x],dfn[v]); } } if(low[x]==dfn[x]){ while(1){ int now=S.top(); S.pop(); vis[now]=0; belong[now]=cnt; if(now==x)break; } cnt++; } } int main(){ cin>>n; memset(in,0,sizeof(in)); memset(out,0,sizeof(out)); for(int i=1;i<=n;i++){ int j; cin>>j; while(j){ E[i].push_back(j); cin>>j; } } for(int i=1;i<=n;i++){ if(dfn[i]==0){ tarjan(i); } } for(int i=1;i<=n;i++){ for(int j=0;j<E[i].size();j++){ if(belong[i]!=belong[E[i][j]]){ out[belong[i]]++; in[belong[E[i][j]]]++; } } } int t1=0;int t2=0; for(int i=1;i<cnt;i++){ if(in[i]==0)t1++; if(out[i]==0)t2++; } if(cnt==2)cout<<1<<endl<<0<<endl; else cout<<t1<<endl<<max(t1,t2)<<endl; return 0; }
相关文章推荐
- POJ 1236 Networks of School Tarjan 基础
- poj 1236 Network of Schools(强连通分量构造强连通图)
- POJ 1236 Network of Schools 强连通图
- POJ1236——Network of Schools
- POJ 1236 强联通
- POJ 1236 强连通分量 Tarjan
- POJ 1236(tarjan+缩点)
- 【POJ】1236 - Network of Schools 强连通分量
- POJ 1236 Network of Schools 强联通分量求出度为0的点和入度为0的点的数目
- POJ 1236 Network of Schools【强联通】
- poj 1236 Network (SCC缩点)
- 【强连通分量模板题 && 加几条边变强连通】POJ - 1236 Network of Schools
- POJ 1236 Network of Schools 强连通图
- poj 1236 Network of Schools 1 (tarjan 缩点)
- POJ 1236 Network of Schools(Tarjan + 缩点 + 计算出度and入度)
- poj 1236
- [poj] 1236 networks of schools
- 【POJ】1236 Network of Schools 强连通
- poj 1236-Network of Schools:Tarjan算法实现
- poj1236 Network of Schools