poj 1236 Network of Schools(强连通分量缩点)

来源:互联网 发布:文字填充图片软件 编辑:程序博客网 时间:2024/05/18 15:07
Network of Schools
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 17230 Accepted: 6838

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

52 4 3 04 5 0001 0

Sample Output

12题意:n个学校,学校可以发送物品到另一个学校(单向)求:1.最少需要几个学校初始有物品,才能使物品传递到所有学校。                                                       2.最少需要加几条边,才可以实现强连通分量。思路:很明显的强连通分量,经过强连通分量缩点之后,第一问是所有入度为0的点(因为没有谁能传递给他),第二问是max(出度为0的点数,入度为0的点数)。易错点:当强连通缩点后只有一个点时作为特殊情况,这个时候不需要加边,这个地方坑了好久~~~ 代码:
//#include<bits/stdc++.h>#include<stdio.h>#include<algorithm>#include<vector>#include<string.h>using namespace std;#define N 30100//N为最大点数#define M 150100//M为最大边数int n, m;//n m 为点数和边数struct Edge{int from, to, nex;bool sign;//是否为桥}edge[M<<1];int head[N], edgenum;void add(int u, int v){//边的起点和终点Edge E={u, v, head[u], false};edge[edgenum] = E;head[u] = edgenum++;}int DFN[N], Low[N], Stack[N], top, Time; //Low[u]是点集{u点及以u点为根的子树} 中(所有反向弧)能指向的(离根最近的祖先v) 的DFN[v]值(即v点时间戳)int taj;//连通分支标号,从1开始int Belong[N];//Belong[i] 表示i点属于的连通分支bool Instack[N];vector<int> bcc[N]; //标号从1开始void tarjan(int u ,int fa){DFN[u] = Low[u] = ++ Time ;Stack[top ++ ] = u ;Instack[u] = 1 ;for (int i = head[u] ; ~i ; i = edge[i].nex ){int v = edge[i].to ;if(DFN[v] == -1){tarjan(v , u) ;Low[u] = min(Low[u] ,Low[v]) ;if(DFN[u] < Low[v]){edge[i].sign = 1;//为割桥}}else if(Instack[v]) Low[u] = min(Low[u] ,DFN[v]) ;}if(Low[u] == DFN[u]){int now;taj ++ ; bcc[taj].clear();do{now = Stack[-- top] ;Instack[now] = 0 ;Belong [now] = taj ;bcc[taj].push_back(now);}while(now != u) ;}}void tarjan_init(int all){memset(DFN, -1, sizeof(DFN));memset(Instack, 0, sizeof(Instack));top = Time = taj = 0;for(int i=1;i<=all;i++)if(DFN[i]==-1 )tarjan(i, i); //注意开始点标!!!}vector<int>G[N];int cu[N],ru[N];void suodian(){memset(cu, 0, sizeof(cu));memset(ru,0,sizeof(ru));for(int i = 1; i <= taj; i++)G[i].clear();for(int i = 0; i < edgenum; i++){int u = Belong[edge[i].from], v = Belong[edge[i].to];if(u!=v)G[u].push_back(v), cu[u]++,ru[v]++;}}int vis[N];void init(){memset(head, -1, sizeof(head)); edgenum=0;}int main(){    init();    scanf("%d",&n);    int a;    for(int i=1;i<=n;i++)    {        while(scanf("%d",&a)&&a)            add(i,a);    }    tarjan_init(n);    for(int i=1;i<=n;i++)    if(DFN[i]==-1)      tarjan(i,-1);    suodian();    int ans1=0,ans2=0;    for(int i=1;i<=taj;i++)    {        if(cu[i]==0)        {            ans2++;        }        if(ru[i]==0)            ans1++;    }    if(taj==1)    {        printf("1\n0\n");        return 0;    }    ans2=max(ans1,ans2);    printf("%d\n%d\n",ans1,ans2);    return 0;}


0 0
原创粉丝点击