POJ 1236 Network of Schools Tarjan缩点

Network of Schools
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 22729   Accepted: 8926

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
哦豁,这道水题。我wa了一天,从11点WA到晚上九点。。最后发现少写一句话orz...
忘记给u标记orzorzorzorz然后发现自己上午敲的板子就是错的 wori....
以后我只用bin神的模板!!!
这个题很简单,还是tarjan缩点,分别找一下入度和出度为0的点。
最少通知几个,易得,考虑入度为0的强连通分量~
最少加几条边,sum1和sum2的最大值?为什么呢,因为要让任意选一个点都满足条件,画一下图,得让每个强连通分量有入有出。所以要取最大值。
贴代码
 #include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<vector>
#include<stack>
#include<map>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define ll long long
#define inf 1000000000
#define maxn 105
#define maxm 100005
struct node
{
int to,next;
} edge[maxm];
int n,m,head[maxn],vis[maxn],dfn[maxn],low[maxn],cdu[maxn],num[maxn],cnt,timi,stack1[maxn],top,cut,rdu[maxn];
stack <int> s;
void init()
{
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(head,-,sizeof(head));
memset(vis,,sizeof(vis));
memset(num,,sizeof(num));
memset(cdu,,sizeof(cdu));
memset(rdu,,sizeof(rdu));
while(!s.empty()) s.pop();
cnt=;
timi=;
top=;
cut=;
}
void addedge(int u,int v)
{
edge[cnt].to=v;
edge[cnt].next=head[u];
head[u]=cnt;
cnt++;
}
void tarjan(int u)
{
dfn[u]=timi;
low[u]=timi;
timi++;
s.push(u);
vis[u]=;
for(int i=head[u]; i!=-; i=edge[i].next)
{
int v=edge[i].to;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else
{
if(vis[v])
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u])
{
cut++;
int x=s.top();
while(x!=u)
{
vis[x]=;
num[x]=cut;
s.pop();
x=s.top();
}
num[x]=cut;
vis[x]=;
s.pop();
}
}
int mp[maxn][maxn];
int main()
{
int a;
while(cin>>n)
{
init();
for(int i=; i<=n; ++i)
{
while(~scanf("%d",&a)&&a) {
addedge(i,a);
mp[i][a]=;
}
}
for(int i=; i<=n; ++i)
{
if(!dfn[i]) tarjan(i);
}
if(cut==)
{
printf("1\n0\n");
continue;
}
for(int i=; i<=n; ++i)
for(int j=; j<=n; ++j)
{
if(mp[i][j]==&&num[i]!=num[j])
{cdu[num[i]]++;rdu[num[j]]++;}
}
int sum1=,sum2=;
for(int i=; i<=cut; ++i)
{
if(!cdu[i]) sum1++;
if(!rdu[i]) sum2++;
}
//cout<<sum1<<" "<<sum2<<endl;
printf("%d\n%d\n",sum2,max(sum1,sum2));
}
return ;
}
上一篇:scikit-learn 机器学习工具包


下一篇:POJ 1236 Network of Schools (强连通分量缩点求度数)