码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1236 tarjan缩点+度数

时间:2014-10-05 20:32:18      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   io   os   ar   for   strong   

Network of Schools
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 11441   Accepted: 4554

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


题目意思:
给出学校数目n,下面n行分别为i学校能到达的学校。问1、最少以多少个学校为起点才能到达所以学校 2、在学校之间连最少多少条边才能使得以任意一个学校为起点能到达所有学校。


思路:
先用trajan算法缩点把学校分成几块,然后算一下入度为0的块数a和出度为0的块数b,a即为答案1,max(a,b)即为答案2(当块数为1时答案2为0)。
第一个很容易想到,第二问由于题目第一句话"A number of schools are connected to a computer network.",也就是说若学校之间的路径为双向的,那么所有学校都连接在一起。那么若从任意一个学校出发都能到达其他所有学校,那么出度为0的块肯定有一条路径指向其他所有入度为0的块,那么很明显在a和b中取最大值即可。


代码:
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <iostream>
 5 #include <vector>
 6 #include <queue>
 7 #include <cmath>
 8 #include <stack>
 9 using namespace std;
10 
11 #define    N 105
12 
13 int low[N], dfn[N], dfn_clock;
14 int instack[N];
15 stack<int>st;
16 vector<int>ve[N];
17 int n;
18 int ans;
19 int in[N], chu[N];
20 int suo[N];
21 
22 void tarjan(int u){
23     int i, j, v;
24     dfn[u]=low[u]=dfn_clock++;
25     st.push(u);//instack[u]=1;
26     for(i=0;i<ve[u].size();i++){
27         v=ve[u][i];
28         if(!dfn[v]){
29             tarjan(v);
30             low[u]=min(low[u],low[v]);
31         }
32         else if(instack[v])
33         low[u]=min(low[u],dfn[v]);
34     }
35     if(low[u]==dfn[u]){
36         ans++;
37         
38          while(1)
39          {
40              v=st.top();
41              suo[v]=ans;
42              st.pop();
43              instack[v]=0;
44              if(v==u) break;
45          }
46     }
47 }
48 
49 main()
50 {
51     int i, j, k, x;
52     while(scanf("%d",&n)==1){
53         for(i=0;i<=n;i++) ve[i].clear(),instack[i]=1;
54         memset(in,0,sizeof(in));
55         memset(chu,0,sizeof(chu));
56         while(!st.empty()) st.pop();
57         for(i=1;i<=n;i++){
58             while(scanf("%d",&x)){
59                 if(!x) break;
60                 ve[i].push_back(x);
61         //    chu[i]++;in[x]++;
62             }
63         }
64         memset(dfn,0,sizeof(dfn));
65     //    memset(instack,0,sizeof(instack));
66         dfn_clock=1;
67         ans=0;
68         for(i=1;i<=n;i++)
69         if(!dfn[i])
70         tarjan(i);
71      //  for(i=1;i<=n;i++) printf("%d ",suo[i]);
72       //  cout<<endl<<endl;
73         for(i=1;i<=n;i++){
74             for(j=0;j<ve[i].size();j++){
75                 x=ve[i][j];
76                 if(suo[x]!=suo[i]){
77                     chu[suo[i]]++;
78                     in[suo[x]]++;
79                 }
80             }
81         }
82         int a=0, b=0;
83         for(i=1;i<=ans;i++){
84             if(!in[i]) a++;
85             if(!chu[i]) b++;
86         }
87     //    printf("%d\n",a);
88     if(ans==1) printf("1\n0\n");
89     else
90         printf("%d\n%d\n",a,max(a,b));
91     }
92 }

 

POJ 1236 tarjan缩点+度数

标签:des   style   blog   color   io   os   ar   for   strong   

原文地址:http://www.cnblogs.com/qq1012662902/p/4007293.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!