首页 > 代码库 > POJ 1236 tarjan缩点+度数
POJ 1236 tarjan缩点+度数
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.
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
题目意思:
给出学校数目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 10512 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 else90 printf("%d\n%d\n",a,max(a,b));91 }92 }
POJ 1236 tarjan缩点+度数
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。