Network of Schools(POJ-1236)
Problem 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 個結點,以及每個結點可到達的點,求有幾個強連通分量,然后問至少在可到達的點中添加幾個點,使得圖變為強連通圖
思路:先求出圖的所有強連通分量,然后進行縮點構建新圖,第一問是新圖中入度為 0 點的個數,第二問是新圖中入度0點數與出度0點數最大的一個
Source Program
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<string> #include<cstdlib> #include<queue> #include<set> #include<map> #include<stack> #include<ctime> #include<vector> #define INF 0x3f3f3f3f #define PI acos(-1.0) #define N 20001 #define MOD 16007 #define E 1e-6 #define LL long long using namespace std; int n,m; vector<int> G[N]; stack<int> S; int dfn[N],low[N]; bool in[N],out[N]; bool vis[N];//標記數組 int sccno[N];//記錄結點i屬于哪個強連通分量 int block_cnt;//時間戳 int sig;//記錄強連通分量個數 void Tarjan(int x){vis[x]=true;dfn[x]=low[x]=++block_cnt;//每找到一個新點,紀錄當前節點的時間戳S.push(x);//當前結點入棧for(int i=0;i<G[x].size();i++){//遍歷整個棧int y=G[x][i];//當前結點的下一結點if(vis[y]==false){//若未被訪問過Tarjan(y);low[x]=min(low[x],low[y]);}else if(!sccno[y])//若已被訪問過,且不屬于任何一個連通分量low[x]=min(low[x],dfn[y]);}if(dfn[x]==low[x]){//滿足強連通分量要求sig++;//記錄強連通分量個數while(true){//記錄元素屬于第幾個強連通分量int temp=S.top();S.pop();sccno[temp]=sig;if(temp==x)break;}} } int main() {while(scanf("%d",&n)!=EOF){for(int i=0;i<n;i++)G[i].clear();for(int i=0;i<n;i++){int y;while(scanf("%d",&y)!=EOF&&y){y--;G[i].push_back(y);}}sig=0;block_cnt=0;memset(vis,0,sizeof(vis));memset(dfn,0,sizeof(dfn));memset(low,0,sizeof(low));memset(sccno,0,sizeof(sccno));for(int i=0;i<n;i++)if(vis[i]==false)Tarjan(i);memset(in,false,sizeof(in));memset(out,false,sizeof(out));for(int i=1;i<=sig;i++)in[i]=out[i]=true;for(int x=0;x<n;x++)for(int i=0;i<G[x].size();i++){int y=G[x][i];if(sccno[x]!=sccno[y])in[sccno[y]]=out[sccno[x]]=false;}int a=0,b=0;for(int i=1;i<=sig;i++){if(in[i])a++;if(out[i])b++;}if(sig==1)printf("1\n0\n");elseprintf("%d\n%d\n",a,max(a,b));} }?
總結
以上是生活随笔為你收集整理的Network of Schools(POJ-1236)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数论 —— 快速幂
- 下一篇: 迷宫(信息学奥赛一本通-T1215)