poj1236 Network of Schools

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

今天模拟赛的第一题
题意是给一个图,第一问求最少选多少个点,使得从这些点出发能遍历整张图。第二问是最少添加多少有向边,使得整个图变成强连通图
第一问比较sb。tarjan缩点完入度为0的点的个数
第二问有点坑……令G{E,V}表示某一弱连通图,I(G)表示G中入度为0的点的个数,O(G)表示G中出度为0的点的个数。
原来我以为是Σmax(I(i),O(i))。其实是max(ΣI(i),ΣO(i))。这区别很容易看出来吧
#include<cstdio>
#include<iostream>
#define LL long long
using namespace std;
inline LL read()
{
    LL x=0,f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
struct edge{int to,next;}e[1000010];
int n,cnt,cnt3,tt,sumi,sumo;
int head[100010];
bool inset[100010];
int dfn[100010],low[100010];
int zhan[100010],top;
int belong[100010];
int I[100010],O[100010];
inline void ins(int u,int v)
{
	e[++cnt].to=v;
	e[cnt].next=head[u];
	head[u]=cnt;
}
inline void dfs(int x)
{
	zhan[++top]=x;inset[x]=1;
	dfn[x]=low[x]=++tt;
	for (int i=head[x];i;i=e[i].next)
	  {
	  	if (!dfn[e[i].to])
	  	{
	  		dfs(e[i].to);
	  		low[x]=min(low[x],low[e[i].to]);
	  	}else if (inset[e[i].to]) low[x]=min(low[x],dfn[e[i].to]);
	  }
	if (low[x]==dfn[x])
	{
		cnt3++;
		int p=-1;
		while (p!=x)
		{
			p=zhan[top--];
			belong[p]=cnt3;
			inset[p]=0;
		}
	}
}
inline void tarjan()
{for (int i=1;i<=n;i++)if (!dfn[i]) dfs(i);}
int main()
{
	n=read();
	for (int i=1;i<=n;i++)
	{
		int x;
		while (scanf("%d",&x)&&x)ins(i,x);
	}
	tarjan();
	for (int i=1;i<=n;i++)
	{
		for (int j=head[i];j;j=e[j].next)
		  if (belong[i]!=belong[e[j].to])
		  {
		  	O[belong[i]]++;
		  	I[belong[e[j].to]]++;
		  }
	}
	if (cnt3==1)
	{
		printf("1\n0\n");
		return 0;
	}
	for (int i=1;i<=cnt3;i++)
	{
	  if (!I[i])sumi++;
	  if (!O[i])sumo++;
	}
	printf("%d\n%d\n",sumi,max(sumi,sumo));
	return 0;
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。