Network of Schools
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 16735 Accepted: 6610
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
Source
IOI 1996
一个强连通分量内 任意2个学校的资源都可以相互分享,因此可以看作一个点
求强连通分量后进行缩点 图就转化为有向无环图
显然 taskA:入度为0的点各给一次资源即可
taskB:不难发现 将入度=0和出度=0的点首尾相连即可,taskB=max(入度=0的点数,出度为0的点数)
注意 此题有坑
当缩点后只剩下一个点时 显然ans=1 0
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>
const int N=100+5;
const int M=N*N;
struct Edge{
int fr,to,next,nextRev;
}edge[M];
int head[N],headRev[N];
void addEdge(int k,int u,int v){
edge[k].fr=u;
edge[k].to=v;
edge[k].next=head[u];
edge[k].nextRev=headRev[v];
head[u]=headRev[v]=k;
}
int visited[N];
int num[N];
int scc[N];
int dfs1(int cur,int&sig){
visited[cur]=true;
for(int i=head[cur];i!=-1;i=edge[i].next){
if(visited[edge[i].to]==false){
dfs1(edge[i].to,sig);
}
}
num[sig++]=cur;
return 0;
}
int dfs2(int cur,int sig){
visited[cur]=true;
scc[cur]=sig;
for(int i=headRev[cur];i!=-1;i=edge[i].nextRev){
if(!visited[edge[i].fr]){
dfs2(edge[i].fr,sig);
}
}
return 0;
}
int kosaraju(int n){
int sig=0;
fill(visited,visited+n+1,false);
for(int i=1;i<=n;++i){
if(visited[i]==false){
dfs1(i,sig);
}
}
sig=1;
fill(visited,visited+n+1,false);
for(int i=n-1;i>=0;--i){
int k=num[i];
if(visited[k]==false){
dfs2(k,sig++);
}
}
return sig-1;
}
int outDeg[N];
int inDeg[N];
int main()
{
//freopen("/home/lu/文档/r.txt","r",stdin);
//freopen("/home/lu/文档/w.txt","w",stdout);
int n;
scanf("%d",&n);
int m=0;
fill(head,head+n+1,-1);
fill(headRev,headRev+n+1,-1);
for(int u=1,v;u<=n;++u){
while(scanf("%d",&v),v){
addEdge(m++,u,v);
}
}
int newN=kosaraju(n);
for(int i=0,newM=0;i<m;++i){
int u=scc[edge[i].fr],v=scc[edge[i].to];
if(u!=v){
++inDeg[v];
++outDeg[u];
}
}
if(newN==1){
printf("1\n0\n");
return 0;
}
int inDeg0=0,outDeg0=0;
for(int i=1;i<=newN;++i){
inDeg0+=inDeg[i]==0;
outDeg0+=outDeg[i]==0;
}
int ansA=inDeg0;
int ansB=max(outDeg0,inDeg0);
printf("%d\n%d\n",ansA,ansB);
return 0;
}