[POJ 1236] Network of Schools explanations

Title Translation:

Some schools are connected to the network, signed an agreement between the school: every school maintained a list of its schools to provide the software. Note B if A school that appears in the list, B may not appear in the list A schools. You have been asked to write a program to calculate how many schools need at least receive new software that allows software to be provided to all schools (A task) according to the agreement. As a further task, we want to make sure the software is given to any one school can make all schools access to software, we can accomplish this task by adding a new connection between the two schools. Computing requires a minimum number of new connections (B task) added.

answer:

  The school regarded as nodes, connected between each school and school software it needs to provide a directed edge, the whole relationship constitutes a directed graph, we can find Tarjan algorithm with a strong figure to all connected components, then each strongly connected component of a school can provide software with each other, and then condensing point, the strongly connected components each as a node, then the digraph constitute a new directed acyclic graph the answer is that the task a is the number of nodes 0, because 0-degree node software can not be provided to any other node. Answer Task B $ max {$ is the number of nodes is 0, the number of nodes is 0 $ $}, we have to do is the degree of node 0 and the node is added 0 side. Note that if the entire map itself is a strongly connected component, the task B answer is zero.

Attach Code:

#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
const int N=120;
int dfn[N],low[N],stac[N],ins[N],c[N],in[N],out[N];
vector<int> son[N];
int n,cnt,top,tot,p,q;

void Tarjan(int x){
    dfn[x]=low[x]=++cnt;
    stac[++top]=x,ins[x]=1;
    for(int i=0;i<son[x].size();++i){
        int y=son[x][i];
        if(!dfn[y]){
            Tarjan(y);
            low[x]=min(low[x],low[y]);
        }else if(ins[y]) low[x]=min(low[x],dfn[y]);
    }
    if(dfn[x]==low[x]){
        tot++;int y;
        do{
            y=stac[top--],ins[y]=0;
            c[y]=tot;
        }while(x!=y);
    }
}

int main(){
    scanf("%d",&n);
    for(int i=1,x;i<=n;++i) while(scanf("%d",&x) && x) son[i].push_back(x);
    for(int i=1;i<=n;++i) if(!dfn[i]) Tarjan(i);
    for(int x=1;x<=n;++x){
        for(int i=0;i<son[x].size();++i){
            int y=son[x][i];
            if(! C [X] =C [Y]) {
                 in [C [Y]] ++, OUT [C [X]] ++ ; 
            } 
        } 
    } 
    for ( int I = . 1 ; I <= TOT; ++ I) {// the statistical is 0 and the degree of the node number 0
         IF (! in P ++ [I]) ;
         IF (! oUT Q ++ [I]) ; 
    } 
    IF (TOT == . 1 ) { 
        the printf ( " . 1 \ N0 \ n- " ); 
    } the else the printf ( " % D \% n-D " , P, max (P, Q));
     return  0 ;
}

Guess you like

Origin www.cnblogs.com/Asika3912333/p/11831056.html