Submission #842264

# Submission time Handle Problem Language Result Execution time Memory
842264 2023-09-02T16:28:00 Z WLZ Logičari (COCI21_logicari) C++17
110 / 110
79 ms 36608 KB
#include <bits/stdc++.h>
using namespace std;

const int MAXN = (int) 1e5;
const int INF = 0x3f3f3f3f;

class dsu {
  private: vector<int> p, rank, sz;
  public:
    dsu(int n) {
      p.assign(n, -1);
      rank.assign(n, 0);
      sz.assign(n, 1);
    }

    int root(int x) {
      if (p[x] == -1) return x;
      return p[x] = root(p[x]);
    }

    bool connected(int x, int y) {
      return root(x) == root(y);
    }

    void connect(int x, int y) {
      x = root(x); y = root(y);
      if (x != y) {
        if (rank[x] > rank[y]) p[y] = x, sz[x] += sz[y];
        else {
          p[x] = y;
          sz[y] += sz[x];
          if (rank[x] == rank[y]) rank[y]++;
        }
      }
    }

    int size(int x) {
      return sz[root(x)];
    }
};

int n, root, special;
vector< vector<int> > g;
long long dp[MAXN + 1][2][2][2][2];

void dfs(int u, int p) {
  for (auto &v : g[u]) if (v != p) dfs(v, u);
  for (int val = 0; val < 2; val++) {
    for (int up = 0; up < 2; up++) {
      for (int rt = 0; rt < 2; rt++) {
        for (int sp = 0; sp < 2; sp++) {
          if (u == special && rt && up) continue;
          if (u == root && rt != val) continue;
          if (u == special && sp != val) continue;
          if (p == root && rt != up) continue;
          if (u == root && up) continue;
          bool found = (u == root && sp) || (u == special && rt) || up;
          long long tmp = val;
          for (auto &v : g[u]) if (v != p) tmp += dp[v][0][val][rt][sp];
          if (found) dp[u][val][up][rt][sp] = tmp;
          else for (auto &v : g[u]) if (v != p) dp[u][val][up][rt][sp] = min((long long) dp[u][val][up][rt][sp], tmp - dp[v][0][val][rt][sp] + dp[v][1][val][rt][sp]);
        }
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  g.resize(n + 1);
  dsu g2(n + 1);
  for (int i = 0; i < n; i++) {
    int u, v;
    cin >> u >> v;
    if (g2.connected(u, v)) root = u, special = v;
    else {
      g[u].push_back(v);
      g[v].push_back(u);    
    }
    g2.connect(u, v);
  }
  for (int u = 1; u <= n; u++) for (int val = 0; val < 2; val++) for (int up = 0; up < 2; up++) for (int rt = 0; rt < 2; rt++) for (int sp = 0; sp < 2; sp++) dp[u][val][up][rt][sp] = INF;
  dfs(root, -1);
  long long ans = INF;
  for (int val = 0; val < 2; val++) for (int up = 0; up < 2; up++) for (int rt = 0; rt < 2; rt++) for (int sp = 0; sp < 2; sp++) ans = min(ans, dp[root][val][up][rt][sp]);
  if (ans == INF) cout << -1 << '\n';
  else cout << ans << '\n';
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 71 ms 36180 KB Output is correct
6 Correct 71 ms 36424 KB Output is correct
7 Correct 79 ms 36608 KB Output is correct
8 Correct 68 ms 36432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 352 KB Output is correct
3 Correct 0 ms 356 KB Output is correct
4 Correct 0 ms 608 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 352 KB Output is correct
9 Correct 0 ms 468 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 352 KB Output is correct
3 Correct 0 ms 356 KB Output is correct
4 Correct 0 ms 608 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 352 KB Output is correct
9 Correct 0 ms 468 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 464 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 71 ms 36180 KB Output is correct
6 Correct 71 ms 36424 KB Output is correct
7 Correct 79 ms 36608 KB Output is correct
8 Correct 68 ms 36432 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 352 KB Output is correct
11 Correct 0 ms 356 KB Output is correct
12 Correct 0 ms 608 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 352 KB Output is correct
17 Correct 0 ms 468 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 464 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 55 ms 19796 KB Output is correct
34 Correct 56 ms 19976 KB Output is correct
35 Correct 52 ms 19732 KB Output is correct
36 Correct 47 ms 19876 KB Output is correct
37 Correct 10 ms 6492 KB Output is correct
38 Correct 54 ms 19804 KB Output is correct
39 Correct 4 ms 3164 KB Output is correct
40 Correct 48 ms 19888 KB Output is correct
41 Correct 41 ms 21092 KB Output is correct
42 Correct 41 ms 21068 KB Output is correct
43 Correct 70 ms 36436 KB Output is correct
44 Correct 63 ms 28756 KB Output is correct