forked from fatemehkarimi/uvaSolutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuva-1056.cpp
77 lines (62 loc) · 1.43 KB
/
uva-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
//uva 1056
//Degrees of Separation
#include <iostream>
#include <climits>
#include <vector>
#include <string>
#include <map>
#include <set>
using namespace std;
int main(void)
{
int n, m;
int T = 0;
while(true){
cin >> n >> m;
if(n == 0 && m == 0)
break;
++T;
vector < vector <int> > Graph(n, vector <int> (n, INT_MAX));
map <string, int> name_code;
for(int i = 0; i < n; ++i)//everyone has a relation to itself
Graph[i][i] = true;
for(int i = 0; i < m; ++i){
string name1, name2;
cin >> name1 >> name2;
int code1, code2;
auto f1 = name_code.find(name1);
if(f1 == name_code.end()){
name_code[name1] = --n;
code1 = n;
}
else
code1 = f1->second;
auto f2 = name_code.find(name2);
if(f2 == name_code.end()){
name_code[name2] = --n;
code2 = n;
}
else
code2 = f2->second;
Graph[code1][code2] = true;
Graph[code2][code1] = true;
}
n = Graph.size();
for(int k = 0; k < n; ++k)
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
if(Graph[i][k] != INT_MAX && Graph[k][j] != INT_MAX)
Graph[i][j] = min(Graph[i][j], Graph[i][k] + Graph[k][j]);
int max_degree = INT_MIN;
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
max_degree = max(max_degree, Graph[i][j]);
cout << "Network " << T << ": ";
if(max_degree != INT_MAX)
cout << max_degree << endl;
else
cout << "DISCONNECTED" << endl;
cout << endl;
}
return 0;
}