Advertisement
pb_jiang

CF1883G WA

May 22nd, 2023
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.26 KB | None | 0 0
  1. #include <assert.h>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #ifdef __DEBUG__
  5. #include "dbg.h"
  6. #else
  7. #define dbg(...) 42
  8. #endif
  9. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  10.  
  11. using ll = long long;
  12. using pii = pair<int, int>;
  13. using pll = pair<ll, ll>;
  14. using vl = vector<ll>;
  15. using vi = vector<int>;
  16. using a3b = array<bool, 3>;
  17.  
  18. a3b dfs(int u, int fa, const map<pii, int> &e, const vector<vector<int>> &g, vector<a3b> &c)
  19. {
  20.     a3b ret = {true, false, false};
  21.     int need0 = 0, need1 = 0, need2 = 0, ssize = 0;
  22.     for (auto v : g[u]) {
  23.         if (v == fa)
  24.             continue;
  25.         auto sub = dfs(v, u, e, g, c);
  26.         need0 += sub[2] == true;
  27.         need1 += sub[1] == true;
  28.         need2 += sub[0] == true;
  29.         ssize++;
  30.     }
  31.     ret[0] = ssize == need0;
  32.     ret[1] = ssize - 1 == need0 && need2 == 1;
  33.     ret[2] = (ssize - 2 == need0 && need2 == 2) || (ssize - 1 == need0 && need1 == 1);
  34.     return c[u] = ret;
  35. }
  36.  
  37. void process(int u, int fa, int given, const map<pii, int> &e, const vector<vector<int>> &g, const vector<a3b> &c,
  38.              vi &cut)
  39. {
  40.     if (given == 0) {
  41.         int a = min(fa, u), b = max(fa, u);
  42.         if (e.count({a, b}))
  43.             cut.push_back(e.at({a, b}));
  44.         vi n0, n1, n2;
  45.         for (auto v : g[u]) {
  46.             if (v == fa)
  47.                 continue;
  48.             if (c[v][0])
  49.                 n2.push_back(v);
  50.             if (c[v][1])
  51.                 n1.push_back(v);
  52.             if (c[v][2])
  53.                 n0.push_back(v);
  54.         }
  55.         for (auto v : n0)
  56.             process(v, u, 0, e, g, c, cut);
  57.         if (n2.size() == 2) {
  58.             for (auto v : n2)
  59.                 process(v, u, 1, e, g, c, cut);
  60.         } else {
  61.             for (auto v : n1)
  62.                 process(v, u, 1, e, g, c, cut);
  63.         }
  64.     } else if (given == 2)
  65.         for (auto v : g[u]) {
  66.             if (v == fa)
  67.                 continue;
  68.             process(v, u, 0, e, g, c, cut);
  69.         }
  70.     else {
  71.         // given == 1
  72.         int choosen = -1;
  73.         for (auto v : g[u]) {
  74.             if (v == fa)
  75.                 continue;
  76.             if (c[v][1] == true && c[v][2] == false) {
  77.                 process(v, u, 2, e, g, c, cut);
  78.             } else {
  79.                 process(v, u, 0, e, g, c, cut);
  80.             }
  81.         }
  82.     }
  83. }
  84.  
  85. int main(int argc, char **argv)
  86. {
  87.     int t, n;
  88.     cin >> t;
  89.     while (t--) {
  90.         dbg(t);
  91.         cin >> n;
  92.         vector<vector<int>> g(n + 1);
  93.         vector<a3b> c(n + 1);
  94.         vi cut;
  95.         map<pii, int> e;
  96.         for (int i = 1; i < n; ++i) {
  97.             int x, y;
  98.             cin >> x >> y;
  99.             e[{min(x, y), max(x, y)}] = i;
  100.             g[x].push_back(y), g[y].push_back(x);
  101.         }
  102.         if (n % 3) {
  103.             dbg(n % 3);
  104.             cout << -1 << endl;
  105.             continue;
  106.         }
  107.         auto ans = dfs(1, -1, e, g, c);
  108.         if (ans[2] == false) {
  109.             dbg(ans[2]);
  110.             cout << -1 << endl;
  111.             continue;
  112.         }
  113.         process(1, -1, 0, e, g, c, cut);
  114.         if (cut.size()) {
  115.             cout << cut.size() << endl;
  116.             for (auto c : cut)
  117.                 cout << c << ' ';
  118.         }
  119.         cout << endl;
  120.     }
  121.     return 0;
  122. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement