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
|
#include<iostream> #include<string> #include<map> using namespace std;
int main(){ int P, R; int kase = 0; while(1){ cin >> P >> R; if(P == 0 && R == 0){ break; } string name1, name2; int ids = 0; map<string, int> name2id; int remap[50][50] = {0}; for(int i = 0; i < 50; ++i) for(int j = 0; j < 50; ++j) remap[i][j] = 100000;
for(int i = 0; i < R; ++i){ cin >> name1 >> name2; int id1, id2; map<string, int>::iterator re = name2id.find(name1); if(re == name2id.end()){ name2id[name1] = ids++; } id1 = name2id[name1]; re = name2id.find(name2); if(re == name2id.end()){ name2id[name2] = ids++; } id2 = name2id[name2];
remap[id1][id2] = 1; remap[id2][id1] = 1; } for(int k = 0; k < P; ++k){ for(int i = 0; i < P; ++i){ for(int j = 0; j < P; ++j){ int dis = remap[i][k] + remap[k][j]; if(dis < remap[i][j] && i != j && i != k && j != k){ remap[i][j] = dis; remap[j][i] = dis; } } } } int ans = 0; for(int i = 0; i < P; ++i){ for(int j = 0; j < P; ++j){ if(i != j && remap[i][j] == 100000){ ans = -1; continue; } if(remap[i][j] != 100000 && ans != -1 && ans < remap[i][j]){ ans = remap[i][j]; } } } kase++; if(ans == -1){ printf("Network %d: DISCONNECTED\n\n", kase); }else{ printf("Network %d: %d\n\n", kase, ans); } } return 0; }
|