博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 1236 Network of Schools Tarjan缩点
阅读量:5075 次
发布时间:2019-06-12

本文共 4104 字,大约阅读时间需要 13 分钟。

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

52 4 3 04 5 0001 0

Sample Output

12 哦豁,这道水题。我wa了一天,从11点WA到晚上九点。。最后发现少写一句话orz... 忘记给u标记orzorzorzorz然后发现自己上午敲的板子就是错的 wori.... 以后我只用bin神的模板!!! 这个题很简单,还是tarjan缩点,分别找一下入度和出度为0的点。 最少通知几个,易得,考虑入度为0的强连通分量~ 最少加几条边,sum1和sum2的最大值?为什么呢,因为要让任意选一个点都满足条件,画一下图,得让每个强连通分量有入有出。所以要取最大值。 贴代码
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 using namespace std; 11 #define mem(a,b) memset(a,b,sizeof(a)) 12 #define ll long long 13 #define inf 1000000000 14 #define maxn 105 15 #define maxm 100005 16 struct node 17 { 18 int to,next; 19 } edge[maxm]; 20 int n,m,head[maxn],vis[maxn],dfn[maxn],low[maxn],cdu[maxn],num[maxn],cnt,timi,stack1[maxn],top,cut,rdu[maxn]; 21 stack
s; 22 void init() 23 { 24 memset(dfn,0,sizeof(dfn)); 25 memset(low,0,sizeof(low)); 26 memset(head,-1,sizeof(head)); 27 memset(vis,0,sizeof(vis)); 28 memset(num,0,sizeof(num)); 29 memset(cdu,0,sizeof(cdu)); 30 memset(rdu,0,sizeof(rdu)); 31 while(!s.empty()) s.pop(); 32 cnt=0; 33 timi=1; 34 top=0; 35 cut=0; 36 } 37 void addedge(int u,int v) 38 { 39 edge[cnt].to=v; 40 edge[cnt].next=head[u]; 41 head[u]=cnt; 42 cnt++; 43 } 44 void tarjan(int u) 45 { 46 dfn[u]=timi; 47 low[u]=timi; 48 timi++; 49 s.push(u); 50 vis[u]=1; 51 for(int i=head[u]; i!=-1; i=edge[i].next) 52 { 53 int v=edge[i].to; 54 if(!dfn[v]) 55 { 56 tarjan(v); 57 low[u]=min(low[u],low[v]); 58 } 59 else 60 { 61 if(vis[v]) 62 low[u]=min(low[u],dfn[v]); 63 } 64 } 65 if(low[u]==dfn[u]) 66 { 67 cut++; 68 int x=s.top(); 69 while(x!=u) 70 { 71 vis[x]=0; 72 num[x]=cut; 73 s.pop(); 74 x=s.top(); 75 } 76 num[x]=cut; 77 vis[x]=0; 78 s.pop(); 79 } 80 } 81 int mp[maxn][maxn]; 82 int main() 83 { 84 int a; 85 while(cin>>n) 86 { 87 init(); 88 for(int i=1; i<=n; ++i) 89 { 90 while(~scanf("%d",&a)&&a) { 91 addedge(i,a); 92 mp[i][a]=1; 93 } 94 } 95 for(int i=1; i<=n; ++i) 96 { 97 if(!dfn[i]) tarjan(i); 98 } 99 if(cut==1)100 {101 printf("1\n0\n");102 continue;103 }104 for(int i=1; i<=n; ++i)105 for(int j=1; j<=n; ++j)106 {107 if(mp[i][j]==1&&num[i]!=num[j]) 108 {cdu[num[i]]++;rdu[num[j]]++;}109 }110 int sum1=0,sum2=0;111 for(int i=1; i<=cut; ++i)112 {113 if(!cdu[i]) sum1++;114 if(!rdu[i]) sum2++;115 }116 //cout<
<<" "<
<
View Code

 

 

转载于:https://www.cnblogs.com/TYH-TYH/p/9392854.html

你可能感兴趣的文章
Unhandled event loop exception No more handles 解决办法
查看>>
策略模式
查看>>
Wp7客户端与Webservice的数据传输,json的序列化与反序列化
查看>>
LeetCode:Next Permutation
查看>>
架构 MVC MVP MVVM 简介 MD
查看>>
关于SimHash去重原理的理解(能力工场小马哥)
查看>>
【python 网络爬虫】之scrapy系列
查看>>
mybatis配置
查看>>
1005. Spell It Right (20)
查看>>
c#图片压缩
查看>>
GIS中的概念理解
查看>>
string.IsNullOrEmpty和string.IsNullOrWhiteSpace方法的区别
查看>>
Linux系列-安装经常使用软件
查看>>
MySQL中的锁(表锁、行锁)
查看>>
iOS---TextView显示HTML文本
查看>>
选择 GCD 还是 NSTimer ?
查看>>
十条服务器端优化Web性能的技巧
查看>>
Swift基础语法(常量变量、数据类型、元组、可选、断言等)
查看>>
20131005国庆作业例5-8,5-9
查看>>
个人工作总结13(冲刺二)
查看>>