Advertisement
ThegeekKnight16

Railways

Jun 14th, 2023
1,130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.15 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int MAXN = 1e5 + 10;
  4. const int MAXK = 21;
  5. array<vector<pair<int, int> >, MAXN> grafo;
  6. array<int, MAXN> prof, tin, marcVert;
  7. array<pair<int, int>, MAXN> pai;
  8. int dp[MAXN][MAXK];
  9. int temp = 0;
  10.  
  11. void dfs(int v, int p, int edge)
  12. {
  13.     prof[v] = prof[p]+1; tin[v] = ++temp;
  14.     dp[v][0] = p;
  15.     for (int k = 1; k < MAXK; k++) dp[ dp[v][k-1] ][k-1];
  16.     pai[v] = make_pair(p, edge);
  17.  
  18.     for (auto [viz, e] : grafo[v])
  19.     {
  20.         if (viz == p) continue;
  21.         dfs(viz, v, e);
  22.     }
  23. }
  24.  
  25. int lca(int a, int b)
  26. {
  27.     if (prof[a] < prof[b]) swap(a, b);
  28.     int D = prof[a] - prof[b];
  29.     for (int k = 0; k < MAXK; k++) if (D & (1 << k)) a = dp[a][k];
  30.     if (a == b) return a;
  31.     for (int k = MAXK-1; k >= 0; k--)
  32.     {
  33.         if (dp[a][k] != dp[b][k])
  34.         {
  35.             a = dp[a][k];
  36.             b = dp[b][k];
  37.         }
  38.     }
  39.     return dp[a][0];
  40. }
  41.  
  42. void solveMarc(int v, int p)
  43. {
  44.     for (auto [viz, e] : grafo[v])
  45.     {
  46.         if (viz == p) continue;
  47.         solveMarc(viz, v);
  48.         marcVert[v] += marcVert[viz];
  49.     }
  50. }
  51.  
  52. int main()
  53. {
  54.     ios_base::sync_with_stdio(false);
  55.     cin.tie(NULL);
  56.     int N, M, K;
  57.     cin >> N >> M >> K;
  58.     for (int i = 1; i < N; i++)
  59.     {
  60.         int X, Y;
  61.         cin >> X >> Y;
  62.         grafo[X].emplace_back(Y, i);
  63.         grafo[Y].emplace_back(X, i);
  64.     }
  65.  
  66.     dfs(1, 1, -1);
  67.  
  68.     for (int i = 1; i <= M; i++)
  69.     {
  70.         int S;
  71.         cin >> S;
  72.         vector<int> verts(S);
  73.         for (auto &x : verts) cin >> x;
  74.         sort(verts.begin(), verts.end(), [&](const int &a, const int &b) {return tin[a] < tin[b];});
  75.         verts.push_back(verts[0]);
  76.         for (int i = 0; i < verts.size()-1; i++)
  77.         {
  78.             marcVert[verts[i]]++; marcVert[verts[(i+1)]]++;
  79.             marcVert[lca(verts[i], verts[(i+1)])] -= 2;
  80.         }
  81.     }
  82.  
  83.     solveMarc(1, 1);
  84.  
  85.     vector<int> resp;
  86.     for (int i = 2; i <= N; i++) if (marcVert[i] >= 2*K) resp.push_back(pai[i].second);
  87.     sort(resp.begin(), resp.end());
  88.     cout << resp.size() << '\n';
  89.     for (auto x : resp) cout << x << " ";
  90.     cout << '\n';
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement