题目链接:http://poj.org/problem?id=1236
Network of Schools
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 10132 | Accepted: 4023 |
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.
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
题目大意:N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输,问题1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件。2,至少需要添加几条传输线路(边),使任意向一个学校发放软件后,经过若干次传送,网络内所有的学校最终都能得到软件。
和上道题差不多,有向无环图中所有入度不为0的点,一定可以由某个入度为0的点出发可达,那么求出强连通缩点后统计入度为0的点个数即为第一问的结果。第二问可以yy结果应该是入度为0和出度为0的点的最大值,证明不太会(求路过大神给个证明
),不过这样有个特殊情况,就是原图只有一个强连通分量,那么结果应该是0.
![可怜](http://static.blog.csdn.net/xheditor/xheditor_emot/default/cute.gif)
具体代码如下:
/**
* @author neko01
*/
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <queue>
using namespace std;
typedef long long LL;
#define INF 100000000
const double pi=acos(-1.0);
const double eqs=1e-10;
int head[105];
int low[105],dfn[105];
int out[105],in[105];
int belong[105];
int isstack[105];
int stack[105];
struct node{
int to,next;
}a[10005];
int scc,top,cnt=0;
int n;
void add(int u,int v)
{
a[cnt].to=v;
a[cnt].next=head[u];
head[u]=cnt++;
}
void tarjan(int u)
{
low[u]=dfn[u]=++cnt;
stack[++top]=u;
isstack[u]=1;
for(int i=head[u];i!=-1;i=a[i].next)
{
int v=a[i].to;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(isstack[v])
{
low[u]=min(dfn[v],low[u]);
}
}
if(dfn[u]==low[u])
{
int t;
scc++;
do
{
t=stack[top--];
belong[t]=scc;
isstack[t]=0;
}while(t!=u);
}
}
void solve()
{
top=scc=cnt=0;
for(int i=1;i<=n;i++)
if(!dfn[i])
tarjan(i);
for(int i=1;i<=n;i++)
{
for(int j=head[i];j!=-1;j=a[j].next)
{
int u=belong[i];
int v=belong[a[j].to];
if(u!=v)
out[u]++,in[v]++;
}
}
int x=0,y=0;
for(int i=1;i<=scc;i++)
{
if(out[i]==0)
x++;
if(in[i]==0)
y++;
}
if(scc==1)
printf("1\n0\n");
else
printf("%d\n%d\n",y,max(x,y));
}
int main()
{
scanf("%d",&n);
memset(head,-1,sizeof(head));
for(int i=1;i<=n;i++)
{
int v;
while(scanf("%d",&v)==1&&v)
{
add(i,v);
}
}
solve();
return 0;
}
想到一个简单点的证明,要想满足条件2,就是使原图只有一个连通分量,那么就要使入度为0和出度为0的点之间相连,所以结果应该是两者的最大值,。,。感觉不太对,,求路过大牛指教,,。