標籤:acm 強連通分量
Network of Schools
| Time Limit: 1000MS |
|
Memory Limit: 10000K |
| Total Submissions: 11407 |
|
Accepted: 4539 |
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
52 4 3 04 5 0001 0
Sample Output
12
Source
IOI 1996
題意以及解題思路來自北大暑期培訓課件:
本題例子解釋:
代碼:
#include <iostream>#include <algorithm>#include <stdio.h>#include <string.h>#include <vector>using namespace std;const int maxn=102;vector<int>g[maxn],gre[maxn];//儲存正向圖和逆圖int ord[maxn];//正向搜尋,頂點的編號bool vis[maxn];int out[maxn];//轉化為DAG以後的每個縮點的出度int in[maxn];int belong[maxn];//當前頂點屬於哪個集合,相當於染色,當前頂點被染成了什麼顏色int ans[maxn];//每種顏色包括多少頂點,也就是強聯通分量的個數int color;//代表不同的顏色int no;//正向搜尋排序的編號int n;//頂點數void dfs1(int u)//從當前u頂點開始DFS{ vis[u]=1; for(int i=0;i<g[u].size();i++) { int v=g[u][i]; if(!vis[v]) dfs1(v); } ord[no++]=u;//為每個頂點編號}void dfs2(int u){ vis[u]=1; belong[u]=color;//當前頂點u被染成了color for(int i=0;i<gre[u].size();i++) { int v=gre[u][i]; if(!vis[v]) { dfs2(v); } }}void kosaraju(){ color=1,no=1; memset(in,0,sizeof(vis)); memset(out,0,sizeof(out)); memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++) if(!vis[i]) dfs1(i); memset(vis,0,sizeof(vis)); for(int i=no-1;i>=1;i--)//編好號以後,從排號最大的開始搜尋 { int v=ord[i]; if(!vis[v]) { dfs2(v); color++; } } //構造DAG for(int i=1;i<=n;i++) { for(int j=0;j<g[i].size();j++) { if(belong[i]==belong[g[i][j]]) continue; out[belong[i]]++; in[belong[g[i][j]]]++; } } int inzero=0,outzero=0; for(int i=1;i<color;i++) { if(!in[i]) inzero++; if(!out[i]) outzero++; } if(color==2) printf("1\n0\n"); else printf("%d\n%d\n",inzero,max(inzero,outzero));}int main(){ scanf("%d",&n); int to; for(int i=1;i<=n;i++) { while(scanf("%d",&to)&&to) { g[i].push_back(to); gre[to].push_back(i); } } kosaraju(); return 0;}
[ACM] poj 1236 Network of Schools (有向強連通分量)