-
Notifications
You must be signed in to change notification settings - Fork 0
/
1056.cpp
83 lines (80 loc) · 1.69 KB
/
1056.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
1056 - Degrees of Separation
*/
#include<bits/stdc++.h>
using namespace std;
const int INF = (1e6);
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int n,m,tc=0,first=0;
while(cin>>n>>m && !(n==0 && m==0))
{
map<string,int>mp;
vector<vector<int>>graph(n,vector<int>(n,INF));
int idx=0;
string one,two;
int x,y;
for(int i=0; i<m; i++)
{
cin>>one>>two;
if(mp.find(one)==mp.end())
{
mp[one]=idx++;
}
x=mp[one];
if(mp.find(two)==mp.end())
{
mp[two]=idx++;
}
y = mp[two];
graph[x][y]=1;
graph[y][x]=1;
}
for(int k=0; k<n; k++)
{
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
graph[i][j]=min(graph[i][j],graph[i][k]+graph[k][j]);
}
}
}
int ans = 0;
for(int i=0; i<n; i++)
{
for(int j=i+1; j<n; j++)
{
ans = max(ans,graph[i][j]);
}
}
if(ans==INF)
{
cout<<"Network "<<(++tc)<<": DISCONNECTED"<<endl;
}
else
{
cout<<"Network "<<(++tc)<<": "<<ans<<endl;
}
printf("\n");
}
return 0;
}
/*
Sample Input
4 4
Ashok Kiyoshi Ursala Chun Ursala Kiyoshi
Kiyoshi Chun
4 2
Ashok Chun Ursala Kiyoshi
6 5
Bubba Cooter Ashok Kiyoshi Ursala Chun
Ursala Kiyoshi Kiyoshi Chun
0 0
Sample Output
Network 1: 2
Network 2: DISCONNECTED
Network 3: DISCONNECTED
*/