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 ;
}
												

最新文章

  1. osgconv 批量转换
  2. 论C# java的基本类型
  3. big data vs HPC
  4. HTML增加删除邮件(table)
  5. Swing的设计是MVC的典范
  6. Android学习总结——强制下线功能(广播)
  7. java日期处理 calendar 和date
  8. DocsBuilderGUI 工具使用介绍
  9. Android ListView 设置单选
  10. docker环境中安装node、pm2,映射项目文件守护程序
  11. spring学习总结——介绍
  12. iOS-野指针与僵尸对象
  13. Lua 语言环境安装
  14. 基于SpringCloud的服务注册和调用
  15. Daily scrum 12.24
  16. 10.17JS日记
  17. C#语法文本字面量
  18. TP自动生成模块目录
  19. 国外接活网站Elance, Freelancer和ScriptLance的介绍和对比
  20. MyEclipse10.0 采用插件方式安装 SVN(转)

热门文章

  1. 详解----memcache服务端与客户端
  2. Q&amp;A - ABTesting是啥?
  3. Oracle - 存储过程、函数、包的使用练习-雇员
  4. 认识mysql(2)
  5. ZendFramework-2.4 源代码 - 关于MVC - Model层
  6. python 2.7版本解决TypeError: &#39;encoding&#39; is an invalid keyword argument for this function
  7. GoF23种设计模式之结构型模式之桥接模式
  8. 【转】Python操作MongoDB
  9. python数据排序
  10. 图学java基础篇之集合