1050: Diffuse Secret
Time Limit: 500 MS Memory Limit: 64 MB
Submit: 10 Solved: 8
[Submit][Status][Web Board]Description
A secret is good in one, better in two, ill in three and worse in four.
----Hi, I know a secret, come on I will tell you. But you must promise that you won’t tell it to other guys.
----OK, of course I won't.
Do you think they will keep the secret? No no, just like most people, they share their secrets with their own friends. If everybody knows a secret, it will never be secret any more.
For some reasons that know to all, TheBeet keep the salaries of employees as secret. But the employees don’t. At the beginning, everyone only knows how much he/she get. Everyday they share every secret they know with their friends (They don’t share the secret that they just heard today). For Example, A and B are good friends, B and C are good friends, A knows the secret a and secret d. B knows the secret b and secret c. C knows the secret e. After they share their secrets, A will know the secret abcd, B will know the secret abcde, C will know the secret bce. Today TheBeet increases every employee’s salary and he wonders when will be known to all employees.Input
The input file contains several test cases.
Each test case begins with two integers N, M 0 < N <= 100, 0 <= M <= (N * (N – 1)), N is the number of employee and M indicates there are M pairs of friend.
The following M lines contain 2 integers each line describing the pair of friends.
N = 0 and M = 0 indicates the end of input and should not be processed.Output
For each test case, for each test case, output “Case #:” on the first line, '#' is the number of the test case. Then output a single integer as after such days, these secrets will never be secret. If there is one secret that some one will never know, you just output “Secret.” (Without quotes)
Sample Input
5 5
1 2
2 3
3 4
4 5
5 1
3 1
1 2
0 0Sample Output
Case 1:
2
Case 2:
Secret.HINT
Source
[Submit][Status][Web Board]
题目链接:
http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1050
题目大意:
每个人都有个秘密,他们每一天都会把自己的所有知道的秘密与他朋友分享
当天得到的秘密不会分享。问所有人都知道所有秘密时是第几天。
题目思路:
【最短路】
考虑第i个人的秘密最迟到j,则秘密从i到j的传播为从i到j的最短路。
那么所有人的秘密都到达最远的人的时间就为答案,即求所有人到所有人的最短路里的最大值。
用floyd即可。
1 /**************************************************** 2 3 Author : Coolxxx 4 Copyright 2017 by Coolxxx. All rights reserved. 5 BLOG : http://blog.csdn.net/u010568270 6 7 ****************************************************/ 8 #include<bits/stdc++.h> 9 #pragma comment(linker,"/STACK:1024000000,1024000000") 10 #define abs(a) ((a)>0?(a):(-(a))) 11 #define lowbit(a) (a&(-a)) 12 #define sqr(a) ((a)*(a)) 13 #define mem(a,b) memset(a,b,sizeof(a)) 14 const double EPS=1e-8; 15 const int J=10000; 16 const int MOD=100000007; 17 const int MAX=0x7f7f7f7f; 18 const double PI=3.14159265358979323; 19 const int N=104; 20 using namespace std; 21 typedef long long LL; 22 double anss; 23 LL aans; 24 int cas,cass; 25 int n,m,lll,ans; 26 int e[N][N],f[N][N]; 27 void floyd() 28 { 29 int i,j,k; 30 for(k=1;k<=n;k++) 31 { 32 for(i=1;i<=n;i++) 33 { 34 if(i==k)continue; 35 for(j=1;j<=n;j++) 36 { 37 if(i==j || k==j)continue; 38 f[i][j]=min(f[i][j],f[i][k]+f[k][j]); 39 } 40 } 41 } 42 } 43 int main() 44 { 45 #ifndef ONLINE_JUDGE 46 freopen("1.txt","r",stdin); 47 // freopen("2.txt","w",stdout); 48 #endif 49 int i,j,k,l; 50 int x,y,z; 51 // for(scanf("%d",&cass);cass;cass--) 52 // for(scanf("%d",&cas),cass=1;cass<=cas;cass++) 53 // while(~scanf("%s",s)) 54 while(~scanf("%d",&n)) 55 { 56 scanf("%d",&m); 57 if(!n && !m)break; 58 ans=0;mem(f,0x01); 59 printf("Case %d: ",++cass); 60 for(i=1;i<=m;i++) 61 { 62 scanf("%d%d",&x,&y); 63 f[x][y]=f[y][x]=e[x][y]=e[y][x]=1; 64 } 65 floyd(); 66 for(i=1;i<=n;i++) 67 { 68 for(j=1;j<=n;j++) 69 { 70 if(i==j)continue; 71 ans=max(ans,f[i][j]); 72 } 73 } 74 if(ans==0x01010101)puts("Secret."); 75 else printf("%d ",ans); 76 } 77 return 0; 78 } 79 /* 80 // 81 82 // 83 */