답안 #821803

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
821803 2023-08-11T17:19:42 Z tch1cherin Love Polygon (BOI18_polygon) C++17
100 / 100
290 ms 34796 KB
#include <bits/stdc++.h>
using namespace std;
 
const int MAX_N = 1e5 + 5;
vector<int> G[MAX_N], comp;
bool vis[MAX_N] = {};
int dp[MAX_N][2] = {};
 
void DFS(int u, pair<int, int> edge = {-1, -1}) {
  vis[u] = true;
  dp[u][0] = dp[u][1] = 0;
  vector<int> children;
  for (int v : G[u]) {
    if (u == edge.first && v == edge.second) {
      continue;
    }
    if (v == edge.first && u == edge.second) {
      continue;
    }
    if (!vis[v]) {
      children.push_back(v);
      DFS(v, edge);
      dp[u][0] += max(dp[v][0], dp[v][1]);
    }
  }
  dp[u][1] = dp[u][0];
  for (int v : children) {
    if (u == edge.first && v == edge.second) {
      continue;
    }
    if (v == edge.first && u == edge.second) {
      continue;
    }
    dp[u][1] = max(dp[u][1], dp[u][0] - max(dp[v][0], dp[v][1]) + dp[v][0] + 1);
  }
  comp.push_back(u);
}
 
int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int N;
  cin >> N;
  map<string, int> val;
  int max_v = 0;
  vector<int> in_degree(N), go(N);
  for (int i = 0; i < N; i++) {
    string A, B;
    cin >> A >> B;
    if (!val.count(A)) {
      val[A] = max_v++;
    }
    if (!val.count(B)) {
      val[B] = max_v++;
    }
    go[val[A]] = val[B];
    if (val[A] != val[B]) {
      in_degree[val[A]]++;
      G[val[B]].push_back(val[A]);
    }
  }
  if (N % 2) {
    cout << -1 << "\n";
    exit(0);
  }
  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (go[go[i]] == i && !vis[i] && go[i] != i) {
      ans += 2;
      for (int v : G[i]) {
        in_degree[v]--;
      }
      for (int v : G[go[i]]) {
        in_degree[v]--;
      }
      vis[i] = vis[go[i]] = true;
    }
  }
  for (int i = 0; i < N; i++) {
    if (in_degree[i] == 0 && !vis[i]) {
      comp = vector<int>();
      DFS(i);
      ans += max(dp[i][0], dp[i][1]);
    }
  }
  vector<bool> used(N, false), in_cycle(N, false);
  vector<int> prv(N);
  for (int i = 0; i < N; i++) {
    if (!vis[i] && !used[i]) {
      used[i] = true;
      int j = go[i];
      vector<int> cycle;
      cycle.push_back(i);
      while (!used[j]) {
        used[j] = true;
        cycle.push_back(j);
        j = go[j];
      }
      bool occ = false;
      int last = cycle.back();
      for (int v : cycle) {
        if (v == j) {
          occ = true;
        }
        if (occ) {
          in_cycle[v] = true;
          prv[v] = last;
          last = v;
        }
      }
    }
  }
  for (int i = 0; i < N; i++) {
    if (in_cycle[i] && !vis[i]) {
      comp = vector<int>();
      DFS(i, {i, go[i]});
      int res = max(dp[i][0], dp[i][1]);
      for (int v : comp) {
        vis[v] = false;
      }
      DFS(prv[i], {i, prv[i]});
      res = max({res, dp[prv[i]][0], dp[prv[i]][1]});
      ans += res;
    }
  }
  cout << N - ans << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2676 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2684 KB Output is correct
11 Correct 1 ms 2644 KB Output is correct
12 Correct 1 ms 2644 KB Output is correct
13 Correct 1 ms 2644 KB Output is correct
14 Correct 1 ms 2644 KB Output is correct
15 Correct 1 ms 2676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 286 ms 32536 KB Output is correct
5 Correct 249 ms 17756 KB Output is correct
6 Correct 290 ms 34704 KB Output is correct
7 Correct 244 ms 16484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 254 ms 17004 KB Output is correct
2 Correct 257 ms 20812 KB Output is correct
3 Correct 156 ms 14652 KB Output is correct
4 Correct 239 ms 15000 KB Output is correct
5 Correct 286 ms 31840 KB Output is correct
6 Correct 262 ms 16732 KB Output is correct
7 Correct 257 ms 16336 KB Output is correct
8 Correct 207 ms 15884 KB Output is correct
9 Correct 233 ms 16060 KB Output is correct
10 Correct 173 ms 15940 KB Output is correct
11 Correct 1 ms 2676 KB Output is correct
12 Correct 2 ms 2676 KB Output is correct
13 Correct 1 ms 2644 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2676 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2684 KB Output is correct
11 Correct 1 ms 2644 KB Output is correct
12 Correct 1 ms 2644 KB Output is correct
13 Correct 1 ms 2644 KB Output is correct
14 Correct 1 ms 2644 KB Output is correct
15 Correct 1 ms 2676 KB Output is correct
16 Correct 1 ms 2644 KB Output is correct
17 Correct 1 ms 2644 KB Output is correct
18 Correct 1 ms 2644 KB Output is correct
19 Correct 286 ms 32536 KB Output is correct
20 Correct 249 ms 17756 KB Output is correct
21 Correct 290 ms 34704 KB Output is correct
22 Correct 244 ms 16484 KB Output is correct
23 Correct 254 ms 17004 KB Output is correct
24 Correct 257 ms 20812 KB Output is correct
25 Correct 156 ms 14652 KB Output is correct
26 Correct 239 ms 15000 KB Output is correct
27 Correct 286 ms 31840 KB Output is correct
28 Correct 262 ms 16732 KB Output is correct
29 Correct 257 ms 16336 KB Output is correct
30 Correct 207 ms 15884 KB Output is correct
31 Correct 233 ms 16060 KB Output is correct
32 Correct 173 ms 15940 KB Output is correct
33 Correct 1 ms 2676 KB Output is correct
34 Correct 2 ms 2676 KB Output is correct
35 Correct 1 ms 2644 KB Output is correct
36 Correct 2 ms 2644 KB Output is correct
37 Correct 282 ms 18324 KB Output is correct
38 Correct 270 ms 19596 KB Output is correct
39 Correct 268 ms 16340 KB Output is correct
40 Correct 257 ms 17528 KB Output is correct
41 Correct 265 ms 16544 KB Output is correct
42 Correct 254 ms 17856 KB Output is correct
43 Correct 277 ms 17584 KB Output is correct
44 Correct 277 ms 17992 KB Output is correct
45 Correct 263 ms 17984 KB Output is correct
46 Correct 265 ms 17888 KB Output is correct
47 Correct 238 ms 17168 KB Output is correct
48 Correct 250 ms 17028 KB Output is correct
49 Correct 274 ms 20836 KB Output is correct
50 Correct 157 ms 14708 KB Output is correct
51 Correct 232 ms 15092 KB Output is correct
52 Correct 269 ms 31804 KB Output is correct
53 Correct 253 ms 16812 KB Output is correct
54 Correct 244 ms 16276 KB Output is correct
55 Correct 196 ms 15828 KB Output is correct
56 Correct 227 ms 16040 KB Output is correct
57 Correct 211 ms 15944 KB Output is correct
58 Correct 1 ms 2644 KB Output is correct
59 Correct 1 ms 2676 KB Output is correct
60 Correct 1 ms 2644 KB Output is correct
61 Correct 1 ms 2680 KB Output is correct
62 Correct 1 ms 2684 KB Output is correct
63 Correct 1 ms 2644 KB Output is correct
64 Correct 1 ms 2644 KB Output is correct
65 Correct 287 ms 32576 KB Output is correct
66 Correct 275 ms 17736 KB Output is correct
67 Correct 275 ms 34796 KB Output is correct
68 Correct 242 ms 16504 KB Output is correct
69 Correct 1 ms 2644 KB Output is correct
70 Correct 1 ms 2644 KB Output is correct
71 Correct 1 ms 2672 KB Output is correct
72 Correct 1 ms 2644 KB Output is correct
73 Correct 1 ms 2644 KB Output is correct
74 Correct 1 ms 2644 KB Output is correct
75 Correct 1 ms 2644 KB Output is correct
76 Correct 1 ms 2644 KB Output is correct
77 Correct 1 ms 2644 KB Output is correct
78 Correct 1 ms 2672 KB Output is correct
79 Correct 2 ms 2672 KB Output is correct
80 Correct 1 ms 2644 KB Output is correct
81 Correct 1 ms 2644 KB Output is correct
82 Correct 1 ms 2644 KB Output is correct
83 Correct 1 ms 2644 KB Output is correct