POJ - 1236 - Network of Schools (targan求强联通分量缩点)

POJ - 1236 - Network of Schools (targan求强联通分量缩点)

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

题目大意:

有一个有向图
要给这个图中的点 电脑,如果给了一个点,那么它能到达的点就都可以拥有这个电脑。

问:最少需要多少电脑,才能让所有的点拥有电脑 这是ans1
最少再加多少条边,才能随便给一个点电脑,剩下的所有的点就都有电脑了。

解题思路:

用targan缩点,一个强连通分量缩成一个点,因为这其中的点都是可以互相到达的。
然后再缩完后的图中,看入度为0的点有多少,这些点只能自己直接拥有电脑,不能间接拥有电脑。所以有多少入度为0的点,ans1 就是多少。

再加多少条边,让整个图的强连通分量为1 ? 我们在新图中,让出度为0的点都连上入度为0的点,这样就能让整个图都可到达了。所以 ans2 = max(入度为0的个数,出度为0的个数)。

另外特判一下,如果本来就是强联通图,那么ans1=1 ans2=0.

A代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <stack>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 1e2+10;
const int maxm = 2e4+10;
int n;
int DFN[maxn];
int LOW[maxn];
int timing;
int ins[maxn];
int in[maxn];//入度
int out[maxn];//出度
int color[maxn];//染色 
stack<int> s;
int cnt = 0;
vector<int> v[maxn];
inline void add(int x,int y){
	v[x].push_back(y);
}
void targan(int u){
	++timing;
	DFN[u] = LOW[u] = timing;
	s.push(u);
	ins[u] = 1;
	for(int i=0;i<v[u].size();i++){
		int vv=v[u][i];
		if(!DFN[vv]){//没走过这个点
			targan(vv);
			LOW[u] = min(LOW[u],LOW[vv]);
		}else if(ins[vv]){//走过这个点 并且这个点在栈里
			LOW[u] = min(LOW[u],DFN[vv]);
		}
	}
	if(LOW[u]==DFN[u]){//强联通分量的第一个点
		++cnt;//强联通分量的标号
		while(1){
            int now = s.top();
            s.pop();
            ins[now] = 0;
            color[now] = cnt;
            if(now==u) break;
        }
	}
}
void solve(){
	int x,y;
	int ans1 = 0,ans2 = 0;
	for(int i=1;i<=n;i++){
		x = color[i];
		for(int j=0;j<v[i].size();j++){
			y = color[v[i][j]];
			if(x!=y){
				out[x]++;
				in[y]++;
			}
		}
	}
	for(int i=1;i<=cnt;i++){
		if(in[i]==0) ans1++;
		if(out[i]==0) ans2++;
	}
	cout<<ans1<<endl;
	cout<<max(ans1,ans2)<<endl;
	return;

}
int main(){
	std::ios::sync_with_stdio(0);
	cin>>n;
	int x;
	for(int i=1;i<=n;i++){
		while(cin>>x&&x){
			add(i,x);
		}
	}
	for(int i=1;i<=n;i++){
		if(!DFN[i]){
			targan(i);
		}
	}
	if(cnt==1){
		printf("1\n0\n");
	}else{
		solve();
	} 

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值