Naxocist

Weakpoint

May 30th, 2022
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 5e5 + 3;
  5. vector<int> adj[N];
  6. bitset<N> vis;
  7. int node = 1e9, num = 0, n, x;
  8.  
  9. int dfs(int u) {
  10.  
  11.     int s = 0;
  12.     bool no = 0;
  13.     for(int v : adj[u]) {
  14.         if(v == x) no = 1;
  15.         if(vis[v] || v == x) continue ;
  16.         vis[v] = 1;
  17.  
  18.         int t = dfs(v);
  19.         if(!t) no = 1;
  20.  
  21.         s += t;
  22.     }
  23.    
  24.     if(s > num) node = u, num = s;  
  25.     else if(s == num) node = min(u, node);
  26.  
  27.     if(no) return 0;
  28.     return s + 1;
  29. }
  30.  
  31. int main() {
  32.     ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  33.  
  34.     cin >> n >> x;
  35.  
  36.     while(n--) {
  37.         int u, v; cin >> u >> v;
  38.         adj[u].push_back(v);
  39.         adj[v].push_back(u);
  40.     }
  41.     vis[x] = 1;
  42.     dfs(x);
  43.  
  44.     cout << node << '\n' << num;
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment