답안 #706643

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
706643 2023-03-07T09:00:43 Z Pety Logičari (COCI21_logicari) C++14
110 / 110
150 ms 26896 KB
#include <bits/stdc++.h>

using namespace std;

int n, p[100002], node1, node2;
vector<int>G[100002];
int preset[100002];
long long dp[100002][2][2];

int find (int x) {
  if (p[x] == x)
    return x;
  return p[x] = find(p[x]);
}

void dfs (int nod, int par) {
  vector<int>sons;
  for (auto it : G[nod])
    if (it != par) {
      sons.push_back(it);
      dfs(it, nod);
    }
  for (int cul = 0; cul < 2; cul++)
    for (int cul2 = 0; cul2 < 2; cul2++) {
      if (cul2 && nod == 1)
        continue;
      if (preset[nod] != -1 && preset[nod] != cul)
        continue;
      int blue = (cul2 + (preset[nod] != -1 && preset[node1 + node2 - nod] == 1));
      if (blue == 2 && par == node1 + node2 - nod)
        blue--;
      if (blue == 2)
        continue;
      dp[nod][cul][cul2] = 0;
      for (auto it : sons)
        dp[nod][cul][cul2] += dp[it][0][cul];
      if (!blue) {
        long long add = -1e9;
        for (auto it : sons)
          add = max(add, dp[it][1][cul] - dp[it][0][cul]);
        dp[nod][cul][cul2] += add;
      }
      dp[nod][cul][cul2] += (!cul);
    }
}

void reset() {
  for (int k = 1; k <= n; k++)
    for (int i = 0; i < 2; i++)
      for (int j = 0; j < 2; j++)
        dp[k][i][j] = -1e9;
}

int main () 
{
  ios_base::sync_with_stdio(false);
  cin.tie(0); cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++)
    p[i] = i;
  for (int i = 1; i <= n; i++) {
    int x, y;
    cin >> x >> y;
    if (find(x) == find(y)) 
      node1 = x, node2 = y;
    else {
      G[x].push_back(y);
      G[y].push_back(x);
    }
    p[find(x)] = find(y);
  }
  memset(preset, -1, sizeof(preset));
  long long ans = 1e18;
  for (int i = 0; i < 2; i++)
    for (int j = 0; j < 2; j++) {
      reset();
      preset[node1] = i;
      preset[node2] = j;
      dfs(1, 0);
      ans = min(ans, n - max(dp[1][0][0], dp[1][1][0]));
    }
  cout << (ans >= n ? -1 : ans);
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3028 KB Output is correct
2 Correct 2 ms 3028 KB Output is correct
3 Correct 1 ms 3028 KB Output is correct
4 Correct 1 ms 3028 KB Output is correct
5 Correct 131 ms 26896 KB Output is correct
6 Correct 150 ms 26444 KB Output is correct
7 Correct 133 ms 22152 KB Output is correct
8 Correct 125 ms 23236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3028 KB Output is correct
2 Correct 2 ms 3028 KB Output is correct
3 Correct 2 ms 3028 KB Output is correct
4 Correct 2 ms 3028 KB Output is correct
5 Correct 2 ms 3064 KB Output is correct
6 Correct 2 ms 3036 KB Output is correct
7 Correct 2 ms 3064 KB Output is correct
8 Correct 2 ms 3028 KB Output is correct
9 Correct 2 ms 3028 KB Output is correct
10 Correct 2 ms 3028 KB Output is correct
11 Correct 2 ms 3028 KB Output is correct
12 Correct 2 ms 3028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3028 KB Output is correct
2 Correct 2 ms 3028 KB Output is correct
3 Correct 2 ms 3028 KB Output is correct
4 Correct 2 ms 3028 KB Output is correct
5 Correct 2 ms 3064 KB Output is correct
6 Correct 2 ms 3036 KB Output is correct
7 Correct 2 ms 3064 KB Output is correct
8 Correct 2 ms 3028 KB Output is correct
9 Correct 2 ms 3028 KB Output is correct
10 Correct 2 ms 3028 KB Output is correct
11 Correct 2 ms 3028 KB Output is correct
12 Correct 2 ms 3028 KB Output is correct
13 Correct 2 ms 3028 KB Output is correct
14 Correct 2 ms 3028 KB Output is correct
15 Correct 2 ms 3056 KB Output is correct
16 Correct 3 ms 3028 KB Output is correct
17 Correct 2 ms 3028 KB Output is correct
18 Correct 3 ms 3072 KB Output is correct
19 Correct 2 ms 3028 KB Output is correct
20 Correct 2 ms 3028 KB Output is correct
21 Correct 2 ms 3064 KB Output is correct
22 Correct 2 ms 3156 KB Output is correct
23 Correct 2 ms 3156 KB Output is correct
24 Correct 2 ms 3156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3028 KB Output is correct
2 Correct 2 ms 3028 KB Output is correct
3 Correct 1 ms 3028 KB Output is correct
4 Correct 1 ms 3028 KB Output is correct
5 Correct 131 ms 26896 KB Output is correct
6 Correct 150 ms 26444 KB Output is correct
7 Correct 133 ms 22152 KB Output is correct
8 Correct 125 ms 23236 KB Output is correct
9 Correct 2 ms 3028 KB Output is correct
10 Correct 2 ms 3028 KB Output is correct
11 Correct 2 ms 3028 KB Output is correct
12 Correct 2 ms 3028 KB Output is correct
13 Correct 2 ms 3064 KB Output is correct
14 Correct 2 ms 3036 KB Output is correct
15 Correct 2 ms 3064 KB Output is correct
16 Correct 2 ms 3028 KB Output is correct
17 Correct 2 ms 3028 KB Output is correct
18 Correct 2 ms 3028 KB Output is correct
19 Correct 2 ms 3028 KB Output is correct
20 Correct 2 ms 3028 KB Output is correct
21 Correct 2 ms 3028 KB Output is correct
22 Correct 2 ms 3028 KB Output is correct
23 Correct 2 ms 3056 KB Output is correct
24 Correct 3 ms 3028 KB Output is correct
25 Correct 2 ms 3028 KB Output is correct
26 Correct 3 ms 3072 KB Output is correct
27 Correct 2 ms 3028 KB Output is correct
28 Correct 2 ms 3028 KB Output is correct
29 Correct 2 ms 3064 KB Output is correct
30 Correct 2 ms 3156 KB Output is correct
31 Correct 2 ms 3156 KB Output is correct
32 Correct 2 ms 3156 KB Output is correct
33 Correct 122 ms 9980 KB Output is correct
34 Correct 87 ms 10096 KB Output is correct
35 Correct 90 ms 10100 KB Output is correct
36 Correct 81 ms 10096 KB Output is correct
37 Correct 17 ms 4724 KB Output is correct
38 Correct 80 ms 10076 KB Output is correct
39 Correct 8 ms 3668 KB Output is correct
40 Correct 119 ms 9936 KB Output is correct
41 Correct 47 ms 12320 KB Output is correct
42 Correct 51 ms 11752 KB Output is correct
43 Correct 131 ms 23352 KB Output is correct
44 Correct 106 ms 20340 KB Output is correct