POJ-1236 (Tarjin)

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

ion 1996

Subject to the effect: some schools can give some school message, and then asked if we at least need to give a message to several schools so that all schools have received (answer A), to increase the minimum number of edges can easily send a message that can make all schools have received (answer B).
Outline of Solution: It can be seen, points in one ring are mutually reachable, then we point to a ring shrunk, and then seek a connected graph, the case of 0 degree is the number of points that we requires answers a, we finally find out a diagram that has some degree of the number of the points 0 and 0 degree point, and we need a larger FIG ring, so in this case we answer B is to get no penetration point 0 and a degree view of point of 0 to increase the minimum edge, in fact, we can easily think of, if only one penetration of point 0 and one out-degree is 0 point, we just need to connect these two points in a it can be, so the answer is 1, and if there is more penetration into and out of 0 for 0 point, we have to get one of the largest ring, then we use a connected graph tarjin algorithm end up with x strands, then we put the strands end to end x series can be friends. So the answer of the point B is 0 and the number of the maximum number of points to zero.
Code:

#include <bits/stdc++.h>
using namespace std;
const int maxn=5e5+7;
const int maxm=4e5+7;
struct edge
{
    int v,next;
}e[maxm];
int cnt,head[maxn],dfn[maxn],low[maxn],tot,vis[maxn],num[maxn],st[maxn],top,col,n,m;
void add(int a,int b)
{
    e[++cnt]=edge{b,head[a]};
    head[a]=cnt;
}
void tarjin(int u)
{
    dfn[u]=low[u]=++tot;
    st[++top]=u;
    for(int i=head[u];i;i=e[i].next){
        int v=e[i].v;
        if(!dfn[v]){
            tarjin(v);
            low[u]=min(low[u],low[v]);
        }
        else if(!vis[v]) low[u]=min(low[u],dfn[v]);
    }
    if(low[u]==dfn[u]){
        num[u]=++col;
        vis[u]=1;
        while(st[top]!=u){
            int v=st[top];
            num[v]=col;
            vis[v]=1;
            top--;
        }
        top--;
    }
}
int in[maxn],out[maxn];
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        int x;
        while(scanf("%d",&x) && x){
            add(i,x);
        }
    }
    for(int i=1;i<=n;i++){
        if(!dfn[i])tarjin(i);
    }
    for(int i=1;i<=n;i++){
        for(int j=head[i];j;j=e[j].next){
            int v=e[j].v;
            if(num[i]!=num[v])in[num[v]]++,out[num[i]]++;
        }
    }
    if(col==1){
        printf("1\n0\n");
        return 0;
    }
    int ans1=0,ans2=0;
    for(int i=1;i<=col;i++){
        if(!in[i])ans1++;
        if(!out[i])ans2++;
    }
    //cout<<ans1<<' '<<ans2<<endl;
    ans2=max(ans1,ans2);
    printf("%d\n%d\n",ans1,ans2);
    return 0;
}
Published 34 original articles · won praise 3 · Views 238

Guess you like

Origin blog.csdn.net/qq_44641782/article/details/103303534