Advertisement
BK5008
Dec 27th, 2022
54
0
Never
This is comment for paste HEHEHE
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct edge
  5. {
  6.     int a;
  7.     int b;
  8. };
  9.  
  10. void sol(vector<string> id, vector<edge> edges)
  11. {
  12.     int n = id.size(), m = edges.size(), MAX = 1;
  13.     bool flag = true;
  14.     vector<vector<int>> cost;
  15.     cost.resize(n);
  16.     for(int i=0;i<n;i++){
  17.         cost[i].resize(n, INT_MAX);
  18.         cost[i][i] = 0;
  19.     }
  20.     for(int i=0;i<m;i++){
  21.         cost[edges[i].a][edges[i].b] = 1;
  22.         cost[edges[i].b][edges[i].a] = 1;
  23.     }
  24.     for(int i=0;i<(int)cost.size();i++){
  25.         for(int j=0;j<(int)cost.size();j++){
  26.             for(int k=0;k<(int)cost.size();k++){
  27.                 if((cost[j][i]+cost[i][k])<cost[j][k]){
  28.                     cost[j][k] = cost[j][i] + cost[i][k];
  29.                 }
  30.             }
  31.         }
  32.     }
  33.     for(int i=0;i<(int)cost.size();i++){
  34.         for(int j=0;j<(int)cost.size();j++){
  35.             cout << cost[i][j] << " ";
  36.         }
  37.     }
  38.     for(int i=0;i<(int)cost.size();i++){
  39.         for(int j=0;j<(int)cost.size();j++){
  40.             if(cost[i][j] == INT_MAX){
  41.                 flag = false;
  42.                 MAX = -1;
  43.                 break;
  44.             }
  45.             MAX = max(MAX, cost[i][j]);
  46.         }
  47.         if(!flag){
  48.             break;
  49.         }
  50.     }
  51.     cout << MAX << "\n";
  52.     return;
  53. }
  54.  
  55. int main()
  56. {
  57.     int n, a, b, idx=0;
  58.     while(cin >> n){
  59.         vector<string> id;
  60.         vector<edge> edges;
  61.         string s1, s2;
  62.         for(int i=0;i<n;i++){
  63.             idx = 0;
  64.             cin >> s1 >> s2;
  65.             for(int j=0;j<(int)id.size();j++){
  66.                 if(s1==id[j]){
  67.                     idx += 10;
  68.                     a = j;
  69.                 }
  70.                 if(s2==id[j]){
  71.                     idx += 1;
  72.                     b = j;
  73.                 }
  74.             }
  75.             if(s1==s2 && idx==0){
  76.                 a = (int)id.size();
  77.                 id.push_back(s1);
  78.                 b = a;
  79.             }
  80.             else{
  81.                 if(idx==0){
  82.                     a = (int)id.size();
  83.                     id.push_back(s1);
  84.                     b = (int)id.size();
  85.                     id.push_back(s2);
  86.                 }
  87.                 else if(idx==1){
  88.                     a = (int)id.size();
  89.                     id.push_back(s1);
  90.                 }
  91.                 else if(idx>1){
  92.                     b = (int)id.size();
  93.                     id.push_back(s2);
  94.                 }
  95.                 if(a!=b){
  96.                     edges.push_back({a, b});
  97.                 }
  98.             }
  99.         }
  100.         sol(id, edges);
  101.     }
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement