Advertisement
BaoJIaoPisu

Untitled

Nov 3rd, 2022
542
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define fi first
  3. #define se second
  4. #define pb push_back
  5. #define pf push_front
  6. #define popb pop_back
  7. #define popf pop_front
  8. #define ins insert
  9. #define pq priority_queue
  10. #define minele min_element
  11. #define maxele max_element
  12. #define mp make_pair
  13. #define lb lower_bound //first pos >= val
  14. #define ub upper_bound // first pos > val
  15. #define cnt_bit __builtin_popcount
  16. #define all(x) x.begin(), x.end()
  17. #define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
  18. //#pragma GCC optimize("Ofast")
  19. //#pragma GCC target("avx,avx2,fma")
  20. using namespace std;
  21.  
  22. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  23.  
  24. typedef long long ll;
  25. typedef unsigned long long ull;
  26. typedef pair<ll, ll> pll;
  27. typedef pair<int, int> pii;
  28.  
  29. int d4x[4] = {1, 0, -1, 0}; int d4y[4] = {0, 1, 0, -1};
  30. int d8x[8] = {0, 1, 1, 1, 0, -1, -1, -1};
  31. int d8y[8] = {1, 1, 0, -1, -1, -1, 0, 1};
  32.  
  33. const ll oo = 1e18;
  34. const ll maxN = 1e6;
  35.  
  36. /* Author : Le Ngoc Bao Anh, 11A5, LQD High School for Gifted Student */
  37.  
  38. const int N = 4e5 + 10;
  39. vector<int> g[N];
  40. int dp[N];
  41. bool in[N], out[N];
  42.  
  43. void solve() {
  44.     int t; cin >> t;
  45.     int n; cin >> n;
  46.     for(int i = 1; i <= n; i++) {
  47.         int sz; cin >> sz;
  48.         g[i].resize(sz);
  49.         for(auto &u : g[i]) cin >> u;
  50.     }  
  51.  
  52.     for(int i = 1; i <= n; i++) sort(all(g[i]));
  53.  
  54.     int cnt = 0;
  55.     auto add = [&](int u, int v) -> void {
  56.         ++cnt;
  57.         dp[u]++; dp[v]++;
  58.         in[u] = true;
  59.         out[v] = true;
  60.     };
  61.  
  62.     if(t == 1) {
  63.         for(int i = 1; i <= n; i++) {
  64.             for(auto v : g[i]) {
  65.                 auto iter = lower_bound(all(g[v]), i);
  66.                 if(iter == g[v].end()) {
  67.                     add(i, v);
  68.                 } else {
  69.                     if((*iter) != i) add(i, v);
  70.                 }
  71.             }
  72.         }
  73.  
  74.         for(int i = 1; i <= n; i++) {
  75.             if(dp[i] == cnt) {
  76.                 cout << i;
  77.                 return;
  78.             }
  79.         }
  80.     } else {
  81.         for(int i = 1; i <= n; i++) {
  82.             for(auto v : g[i]) {
  83.                 auto iter = lower_bound(all(g[v]), i);
  84.                 if(iter == g[v].end()) {
  85.                     add(i, v);
  86.                 } else {
  87.                     if((*iter) != i) add(i, v);
  88.                 }
  89.             }
  90.         }
  91.  
  92.         pii ans = make_pair(0, 0);
  93.         for(int i = 1; i <= n; i++) {
  94.             if(!in[i] || !out[i]) continue;
  95.             if(dp[i] > dp[ans.fi]) {
  96.                 ans.se = ans.fi;
  97.                 ans.fi = i;
  98.             } else {
  99.                 if(dp[i] > dp[ans.se]) ans.se = i;
  100.             }
  101.         }
  102.  
  103.         if(ans.fi > ans.se) swap(ans.fi, ans.se);
  104.         cout << ans.fi << " " << ans.se;
  105.     }
  106. }
  107.  
  108. int main()
  109. {
  110.     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  111.     #ifndef ONLINE_JUDGE
  112.     freopen("input.txt", "r", stdin);
  113.     freopen("output.txt", "w", stdout);
  114.     #else
  115.     //online
  116.     #endif
  117.  
  118.     int tc = 1, ddd = 0;
  119.     // cin >> tc;
  120.     while(tc--) {
  121.         //ddd++;
  122.         //cout << "Case #" << ddd << ": ";
  123.         solve();
  124.     }
  125. }
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement