Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. using namespace std;
  2. #include                    <bits/stdc++.h>
  3. #define     DB(x)           cerr << #x << " is " << (x) << endl;
  4. #define     FIO             ios::sync_with_stdio(false);// cin.tie(nullptr); cout.tie(nullptr) // ===  === === === === === ===>
  5. #define     pb              push_back
  6. #define     fs              first
  7. #define     sc              second
  8. #define     endl            "\n"
  9. typedef     long long           ll;
  10. typedef     long double         ld;
  11. int const mxn = 2e3+2;
  12. int const mod = 1e9+7;
  13.  
  14. int n, m, ans=-1;
  15. vector<int> E[mxn];
  16. vector<int> dd(mxn);
  17. queue<int> Q;
  18.  
  19. int cal(int u){
  20.     Q.push(u);
  21.     dd[u] = 1;
  22.     while (Q.size()){
  23.         u = Q.front();
  24.         Q.pop();
  25.         for (auto v: E[u]){
  26.             if (dd[v]) continue;
  27.             dd[v] = dd[u]+1;
  28.             Q.push(v);
  29.         }
  30.     }
  31.     int f = 0, mx = 0;
  32.     for (int i=1; i<=n; ++i){
  33.         if (!dd[i]) f = 1;
  34.         mx = max(mx,dd[i]);
  35.         dd[i] = 0;
  36.     }
  37.     if (f) return -1;
  38.     return mx;
  39. }
  40.  
  41. void SOL(){
  42.     cin >> n >> m;
  43.     for (int i=1; i<=m; ++i){
  44.         int x, y; cin >> x >> y;
  45.         E[x].pb(y);
  46.         E[y].pb(x);
  47.     }
  48.     for (int i=1; i<=n; ++i){
  49.         ans = max(ans,cal(i));
  50.     }
  51.     if (ans == -1){
  52.         cout << "=[";  
  53.     } else {
  54.         cout << "=] " << ans-1;
  55.     }
  56. }  
  57. /*
  58. */
  59. int main(){FIO; SOL(); return 0;}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement