Naxocist

Test Drive

May 17th, 2022
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define INF 2e9
  5.  
  6. using tiii = tuple<int, int, int>;
  7. using pi = pair<int, int>;
  8. using ll = long long;
  9.  
  10. const int N = 1005;
  11. int in[N], dist[N][N];
  12. vector<int> adj[N];
  13.  
  14.  
  15. int main(){
  16.     cin.tie(nullptr)->sync_with_stdio(false);
  17.  
  18.     // freopen("input.txt", "r", stdin);
  19.     int n, m; cin >> n >> m;
  20.  
  21.     for(int i=0; i<m; ++i){
  22.         int u, v; cin >> u >> v;
  23.         if(u > v) swap(u, v);
  24.         adj[u].push_back(v);
  25.         in[v]++;
  26.     }
  27.  
  28.     queue<tiii> q;
  29.     for(int i=1; i<=n; ++i) if(!in[i]) q.emplace(i, i, 0);
  30.  
  31.     int mx = 0;
  32.     vector<tiii> v;
  33.     while(q.size()){
  34.         int u, p, t;
  35.         tie(u, p, t) = q.front(); q.pop();
  36.        
  37.         mx = dist[p][u] = t;
  38.         for(int v : adj[u]){
  39.             q.emplace(v, p, t+1);
  40.         }
  41.     }
  42.  
  43.     cout << mx << '\n';
  44.     for(int i=1; i<=n; ++i){
  45.         if(in[i]) continue ;
  46.  
  47.         for(int j=1; j<=n; ++j){
  48.             if(dist[i][j] == mx){
  49.                 cout << "(" << i << ","<< j << ") ";
  50.             }
  51.         }
  52.     }
  53.     return 0;
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment