poj1236:强连通分量

1.求有多少个入度为0即可2.和上一题相同。

--------------------------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<stack>
using namespace std;
#define rep(i,n) for(int i=1;i<=n;i++)
#define clr(x,c) memset(x,c,sizeof(x))
const int nmax=105;
int read(){
    int x=0;char c=getchar();
    while(!isdigit(c)) c=getchar();
    while(isdigit(c)){
        x=x*10+c-'0';
        c=getchar();
    }
    return x;
}
struct edge{
    int to;
    edge *next;
};

edge e[100005],*head[nmax],*pt=e;
int sccno[nmax],pre[nmax],low[nmax],l[nmax],r[nmax],dfn_clock=0,scc_cnt=0;
stack<int>S;
void adde(int s,int t){
    pt->to=t;
    pt->next=head[s];
    head[s]=pt++;
}
void dfs(int x){
    low[x]=pre[x]=++dfn_clock;
    S.push(x);
    for(edge *ee=head[x];ee;ee=ee->next){
        int to=ee->to;
        if(!pre[to]){
            dfs(to);
            low[x]=min(low[x],low[to]);
        }else if(!sccno[to])
            low[x]=min(low[x],pre[to]);
    }
    if(pre[x]==low[x]){
        scc_cnt++;
        while(1){
            int u=S.top();S.pop();
            sccno[u]=scc_cnt;
            if(u==x) break;
        }
    }
    return ;
}
int main(){
    int n=read();
    rep(i,n){
        int tmp;
        while(tmp=read()){
            adde(i,tmp);
        }
    }
    /*rep(i,n){
        cout<<i<<":"<<endl;
        for(edge *ee=head[i];ee;ee=ee->next)
          printf("%d ",ee->to);
        cout<<endl;
    }*/
    rep(i,n)
      if(!pre[i]) dfs(i);
    rep(i,n){
        for(edge *ee=head[i];ee;ee=ee->next){
            if(sccno[i]!=sccno[ee->to]){
                r[sccno[i]]++;
                l[sccno[ee->to]]++;
            }
        }
    }
    int lsum=0,rsum=0;
    rep(i,scc_cnt){
        if(!l[i]) lsum++;
        if(!r[i]) rsum++;
    }
    printf("%d\n%d\n",lsum,scc_cnt==1?0:max(lsum,rsum));
    return 0;
}

--------------------------------------------------------------------------------------------

Network of Schools
Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %I64d & %I64u

Submit Status

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

Submit Status

你可能感兴趣的:(poj1236:强连通分量)