1236 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
5 2 4 3 0 4 5 0 0 0 1 0 Sample Output
1 2 Source IOI 1996 題意:學校裡有n臺電腦,每臺電腦都可以通過網路傳達檔案給指定的電腦,但是指定的電腦不一定能傳回來,也就是有向圖,第一個問是最少要在多少電腦上安裝檔案,才能使得所有電腦都能有檔案,第二個問是最少要增加幾條邊,才能使把檔案安裝在任意一臺電腦上,所有電腦都能有檔案。
分析及思路:
首先每臺電腦是一個節點,可以看出,一個環也就是強連通分量內只要有一臺電腦能接收到檔案,則所有這個環內的電腦都能接收到檔案,因此可以把一個環看成一個點,這道題就變成了求強連通分量並且縮點的問題。縮點之後求所有點的入度和出度,顯然,入度為0的點就是第一個問的答案,因為這個環沒辦法從別的地方接受檔案,所以這個環必須自己安裝一個檔案,其它有入度的環都可以從別的地方接收檔案。第二個問的答案則是統計入度為0的環的數量t1和出度為0的環的數量t2,答案便是t1和t2的最大值,當然需要特判一下,如果只有一個環,則答案為0。原因也不難理解。讀者可以自行畫圖思考便可得出答案。 AC程式碼:
#include<iostream>
#include<cstring>
#include<stack>
#define rep(i,x,n) for(int i=x;i<n;i++)
#define per(i,x,n) for(int i=n-1;i>=x;i--)
using namespace std;
//head
const int maxn=106;
struct Edge{int to,next;}edge[100006];
int n,a,cur=0,tot=0,head[maxn],vis[maxn],low[maxn],dfn[maxn],color[maxn],out[maxn],in[maxn];
void addedge(int x){edge[cur].to=a;edge[cur].next=head[x],head[x]=cur++;}
stack<int>q;
void tarjan(int x)
{
low[x]=dfn[x]=++tot;
q.push(x);
vis[x]=1;
for(int i=head[x];i!=-1;i=edge[i].next)
{
int nx=edge[i].to;
if(!dfn[nx])
{
tarjan(nx);
low[x]=min(low[x],low[nx]);
}
else if(vis[nx])
low[x]=min(low[x],dfn[nx]);
}
if(low[x]==dfn[x])
{
int tmp=0;
while(!q.empty())
{
tmp=q.top();
q.pop();
color[tmp]=cur;
vis[tmp]=0;
if(tmp==x)break;
}
cur++;
}
}
int main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
memset(head,-1,sizeof(head));
cin>>n;
rep(i,1,n+1)
{
while(cin>>a&&a!=0)
addedge(i);
}
cur=0;
rep(i,1,n+1)if(!dfn[i])tarjan(i);
if(cur==1){cout<<1<<endl<<0<<endl;return 0;}
rep(i,1,n+1)
{
for(int x=head[i];x!=-1;x=edge[x].next)
if(color[i]!=color[edge[x].to]){out[color[i]]++;in[color[edge[x].to]]++;}
}
int t1=0,t2=0;
rep(i,0,cur)
{
if(in[i]==0)t1++;
if(out[i]==0)t2++;
}
cout<<t1<<endl<<max(t1,t2)<<endl;
return 0;
}