POJ1236 - Network of Schools tarjan

                                                 Network of Schools
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5354   Accepted: 2106

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

Source

 

题意:

有N个学校...每个学校可能向几个学校进行数据传输,问至少需要把一个文件给几个学校可以使给的N个学校都收到文件,再问在加几个通信线路可以使各个学校之间都能直接或间接的传递文件;

题解:首先跑一发tarjan缩点,

第一问就是问重建图后入度为0的点,如果没有至少是1;

第二问就是max(入度为0,出度为0);

///
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <typeinfo>
#include <map>
typedef long long ll;
using namespace std;
#define inf 10000000
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//***************************************************************
struct ss
{
int to,next;
} e[**];
int belong[],hav[];
int scc,n,top;
int in[],out[],ansl,ansr;
int head[],dfn[],low[],cnt,t,vis[],q[],inq[];
void init()
{
memset(in,,sizeof(in));
memset(out,,sizeof(out));
memset(head,,sizeof(head));
top=;
scc=;
cnt=;
t=;
memset(inq,,sizeof(inq));
memset(hav,,sizeof(hav));
memset(vis,,sizeof(vis));
}
void add(int u,int v)
{
e[t].to=v;
e[t].next=head[u];
head[u]=t++;
}
void dfs(int a)
{
int now=;
dfn[a]=low[a]=++cnt;
vis[a]=inq[a]=;
q[++top]=a;
for(int i=head[a]; i; i=e[i].next)
{
if(!vis[e[i].to])
{
dfs(e[i].to);
low[a]=min(low[a],low[e[i].to]);
}
else
{
if(inq[e[i].to])low[a]=min(low[a],dfn[e[i].to]);///在队列中
}
}
if(low[a]==dfn[a])
{
scc++;
while(now!=a)
{
now=q[top--];
inq[now]=;
belong[now]=scc;
++hav[scc];
}
}
}
void rebuild()
{
for(int i=; i<=n; i++)
{
for(int j=head[i]; j; j=e[j].next)
{
if(belong[i]!=belong[e[j].to])
{
out[belong[i]]++;
in[belong[e[j].to]]++;
}
}
}
int A=,B=;
for(int i=; i<=scc; i++)
{
if(in[i]==)A++;
if(out[i]==)B++;
}
ansr=A;
ansl=max(A,B);
}
void tarjan()
{
for(int i=; i<=n; i++)if(!vis[i])dfs(i);
rebuild();
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
init();
int x;
for(int i=; i<=n; i++)
{
while(scanf("%d",&x)!=EOF)
{
if(x==)break;
add(i,x);
}
}
tarjan();
if(scc==)ansl=,ansr=;
printf("%d\n%d\n",ansr,ansl);
} return ;
}

代码

上一篇:在标准实体特殊消息上注册插件及Dynamics CRM 2015中计算字段的使用


下一篇:统计学习方法笔记--EM算法--三硬币例子补充