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

Network of Schools
Time Limit: 1000MS Memory Limit: 10000K
Total 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

5
2 4 3 0
4 5 0
0
0
1 0

Sample Output

1
2

题意: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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值