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 题意: There are n computers in the school, each computer can communicate files to the designated computer through the network, but the designated computer may not be able to pass Come back, that is, the directed graph. The first question is how many files should be installed on the computer to make all computers have files. The second question is to add at least a few edges to make the files install at random. On a single computer, all computers can have files.

分析与思维:

First, each computer is a node. It can be seen that a ring is a strong connected component. As long as there is a computer that can receive the file, all the computers in the ring can Receiving a file, so you can think of a ring as a point, this question becomes a problem of seeking strong connected components and shrinking points. After the retraction point, find the indegree and outdegree of all points. Obviously, the point with 0 degree is the first answer. Because the ring can't accept files from other places, this ring must install a file by itself. Other loops with accessibility can receive files from other places. The answer to the second question is the number t1 of the rings with a degree of entry 0 and the number t2 of the rings with a degree of 0. The answer is the maximum value of t1 and t2. Of course, special judgment is required. If there is only one ring, Then the answer is 0. The reason is not difficult to understand. Readers can draw their own ideas to draw answers. AC code:

#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;
}