Advertisement
pb_jiang

logu P2016

Jan 24th, 2023
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.09 KB | None | 0 0
  1. // Problem: P2016 战略游戏
  2. // Contest: Luogu
  3. // URL: https://www.luogu.com.cn/problem/P2016
  4. // Memory Limit: 125 MB
  5. // Time Limit: 1000 ms
  6. //
  7. // Powered by CP Editor (https://cpeditor.org)
  8.  
  9. #include <assert.h>
  10. #include <bits/stdc++.h>
  11. using namespace std;
  12. #define dbg(...) logger(#__VA_ARGS__, __VA_ARGS__)
  13. template <typename... Args> void logger(string vars, Args &&... values)
  14. {
  15.     cerr << vars << " = ";
  16.     string delim = "";
  17.     (..., (cerr << delim << values, delim = ", "));
  18.     cerr << endl;
  19. }
  20.  
  21. template <class T> inline auto vv(int m) { return vector<vector<T>>(m, vector<T>(m)); }
  22. template <class T> inline auto vv(int m, int n) { return vector<vector<T>>(m, vector<T>(n)); }
  23. template <class T, T init> inline auto vv(int m) { return vector<vector<T>>(m, vector<T>(m, init)); }
  24. template <class T, T init> inline auto vv(int m, int n) { return vector<vector<T>>(m, vector<T>(n, init)); }
  25.  
  26. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  27.  
  28. using ll = long long;
  29. using pii = pair<int, int>;
  30. using vl = vector<ll>;
  31. using vi = vector<int>;
  32.  
  33. int n;
  34. vi g[2048];
  35.  
  36. pii dfs(int u, int pa)
  37. {
  38.     pii ans = {1, 0};
  39.     for (auto v : g[u]) {
  40.         if (v == pa)
  41.             continue;
  42.         auto [place, no_place] = dfs(v, u);
  43.         ans.first += min(place, no_place);
  44.         ans.second += place;
  45.     }
  46.     return ans;
  47. }
  48. pii dfs1(int u, int pa)
  49. {
  50.     int all = 0;
  51.     int min_delta = INT_MAX / 2;
  52.     for (auto v : g[u]) {
  53.         if (v == pa)
  54.             continue;
  55.         auto [sw, swn] = dfs(v, u);
  56.         all += min(sw, swn);
  57.         min_delta = min(min_delta, sw - swn);
  58.     }
  59.     return {all + 1, all + (min_delta > 0 ? min_delta : 0)};
  60. }
  61.  
  62. int main(int argc, char **argv)
  63. {
  64.     cin >> n;
  65.     for (int i = 0; i < n; ++i) {
  66.         int u, v, k;
  67.         cin >> u >> k;
  68.         for (int j = 0; j < k; ++j) {
  69.             cin >> v;
  70.             g[u].push_back(v);
  71.             g[v].push_back(u);
  72.         }
  73.     }
  74.     auto ret = dfs(0, -1);
  75.     dbg(ret.first, ret.second);
  76.     cout << min(ret.first, ret.second) << endl;
  77.     return 0;
  78. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement