답안 #952433

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
952433 2024-03-23T21:36:52 Z Yell0 Love Polygon (BOI18_polygon) C++17
100 / 100
131 ms 32664 KB
#include <bits/stdc++.h>
 
using namespace std;
const int MN=1e5+2;
int N;
set<int> fromgr[MN];
int togr[MN];
bool vis[MN];
int dpst[MN], dpew[MN];

void dfs(int u, int root) {
  vis[u] = 1;
  if(fromgr[u].empty()) {
    dpst[u] = 1;
    dpew[u] = 0;
    return;
  }
  dpst[u] = 1e9;
  dpew[u] = 0;
  for(int v : fromgr[u]) {
    if(v == root) continue;
    dfs(v, root);
    dpew[u] += dpst[v];
  }
  for(int v : fromgr[u]) {
    if(v == root) continue;
    dpst[u] = min(dpst[u], dpew[u] - dpst[v] + dpew[v] + 1);
  }
  if(dpst[u] == (int)1e9) dpst[u] = 1;
}

int solve(int u) {
  vector<bool> seen(N, 0);
  // find root
  seen[u] = 1;
  while(!seen[togr[u]]) {
    u = togr[u];
    seen[u] = 1;
  }
  // relationship
  if(togr[u] != u && togr[togr[u]] == u) {
    dfs(u, togr[u]);
    dfs(togr[u], u);
    return dpew[u] + dpew[togr[u]];
  }

  dfs(u, u);
  int ans = min(dpst[u], dpew[u] + 1);

  fromgr[togr[togr[u]]].erase(togr[u]);
  dfs(u, u);
  return min(ans, dpew[u] + dpew[togr[u]] + 1);
}

int main() {
  ios::sync_with_stdio(0); cin.tie(0);
  cin>>N;
  if(N%2) {
    cout<<"-1\n";
    return 0;
  }
  unordered_map<string, int> id;
  vector<string> pts;
  for(int i=0; i<N; ++i) {
    string a,b;
    cin>>a>>b;
    id[a] = i;
    pts.emplace_back(b);
  }
  for(int i=0; i<N; ++i) {
    togr[i] = id[pts[i]];
    fromgr[id[pts[i]]].emplace(i);
  }

  int ans = 0;
  for(int i=0; i<N; ++i) {
    if(vis[i]) continue;
    ans += solve(i);
  }
  // for(int i=0; i<N; ++i) cout<<dpst[i]<<' '<<dpew[i]<<'\n';
  cout<<ans<<'\n';
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6232 KB Output is correct
2 Correct 2 ms 6240 KB Output is correct
3 Correct 1 ms 6236 KB Output is correct
4 Correct 2 ms 6236 KB Output is correct
5 Correct 2 ms 6236 KB Output is correct
6 Correct 2 ms 6232 KB Output is correct
7 Correct 2 ms 6236 KB Output is correct
8 Correct 2 ms 6236 KB Output is correct
9 Correct 3 ms 6236 KB Output is correct
10 Correct 2 ms 6236 KB Output is correct
11 Correct 2 ms 6236 KB Output is correct
12 Correct 2 ms 6236 KB Output is correct
13 Correct 2 ms 6236 KB Output is correct
14 Correct 2 ms 6236 KB Output is correct
15 Correct 2 ms 6236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6236 KB Output is correct
2 Correct 2 ms 6236 KB Output is correct
3 Correct 2 ms 6232 KB Output is correct
4 Correct 90 ms 30648 KB Output is correct
5 Correct 73 ms 24504 KB Output is correct
6 Correct 131 ms 32664 KB Output is correct
7 Correct 2 ms 6236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 74 ms 21872 KB Output is correct
2 Correct 79 ms 23580 KB Output is correct
3 Correct 73 ms 22296 KB Output is correct
4 Correct 1 ms 6236 KB Output is correct
5 Correct 86 ms 29344 KB Output is correct
6 Correct 85 ms 21684 KB Output is correct
7 Correct 73 ms 22728 KB Output is correct
8 Correct 82 ms 21812 KB Output is correct
9 Correct 86 ms 21688 KB Output is correct
10 Correct 59 ms 21688 KB Output is correct
11 Correct 2 ms 6236 KB Output is correct
12 Correct 1 ms 6236 KB Output is correct
13 Correct 2 ms 6236 KB Output is correct
14 Correct 2 ms 6236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6232 KB Output is correct
2 Correct 2 ms 6240 KB Output is correct
3 Correct 1 ms 6236 KB Output is correct
4 Correct 2 ms 6236 KB Output is correct
5 Correct 2 ms 6236 KB Output is correct
6 Correct 2 ms 6232 KB Output is correct
7 Correct 2 ms 6236 KB Output is correct
8 Correct 2 ms 6236 KB Output is correct
9 Correct 3 ms 6236 KB Output is correct
10 Correct 2 ms 6236 KB Output is correct
11 Correct 2 ms 6236 KB Output is correct
12 Correct 2 ms 6236 KB Output is correct
13 Correct 2 ms 6236 KB Output is correct
14 Correct 2 ms 6236 KB Output is correct
15 Correct 2 ms 6236 KB Output is correct
16 Correct 2 ms 6236 KB Output is correct
17 Correct 2 ms 6236 KB Output is correct
18 Correct 2 ms 6232 KB Output is correct
19 Correct 90 ms 30648 KB Output is correct
20 Correct 73 ms 24504 KB Output is correct
21 Correct 131 ms 32664 KB Output is correct
22 Correct 2 ms 6236 KB Output is correct
23 Correct 74 ms 21872 KB Output is correct
24 Correct 79 ms 23580 KB Output is correct
25 Correct 73 ms 22296 KB Output is correct
26 Correct 1 ms 6236 KB Output is correct
27 Correct 86 ms 29344 KB Output is correct
28 Correct 85 ms 21684 KB Output is correct
29 Correct 73 ms 22728 KB Output is correct
30 Correct 82 ms 21812 KB Output is correct
31 Correct 86 ms 21688 KB Output is correct
32 Correct 59 ms 21688 KB Output is correct
33 Correct 2 ms 6236 KB Output is correct
34 Correct 1 ms 6236 KB Output is correct
35 Correct 2 ms 6236 KB Output is correct
36 Correct 2 ms 6236 KB Output is correct
37 Correct 87 ms 23996 KB Output is correct
38 Correct 82 ms 25868 KB Output is correct
39 Correct 74 ms 24252 KB Output is correct
40 Correct 75 ms 23988 KB Output is correct
41 Correct 74 ms 24760 KB Output is correct
42 Correct 95 ms 23988 KB Output is correct
43 Correct 69 ms 23996 KB Output is correct
44 Correct 69 ms 25020 KB Output is correct
45 Correct 73 ms 24316 KB Output is correct
46 Correct 71 ms 24276 KB Output is correct
47 Correct 73 ms 23992 KB Output is correct
48 Correct 73 ms 24028 KB Output is correct
49 Correct 76 ms 26272 KB Output is correct
50 Correct 73 ms 23992 KB Output is correct
51 Correct 2 ms 6236 KB Output is correct
52 Correct 92 ms 31108 KB Output is correct
53 Correct 95 ms 23992 KB Output is correct
54 Correct 88 ms 24024 KB Output is correct
55 Correct 75 ms 23992 KB Output is correct
56 Correct 72 ms 24768 KB Output is correct
57 Correct 60 ms 24248 KB Output is correct
58 Correct 2 ms 6236 KB Output is correct
59 Correct 2 ms 6236 KB Output is correct
60 Correct 2 ms 6264 KB Output is correct
61 Correct 2 ms 6236 KB Output is correct
62 Correct 3 ms 6236 KB Output is correct
63 Correct 2 ms 6236 KB Output is correct
64 Correct 2 ms 6236 KB Output is correct
65 Correct 88 ms 30736 KB Output is correct
66 Correct 70 ms 23900 KB Output is correct
67 Correct 90 ms 32184 KB Output is correct
68 Correct 2 ms 6236 KB Output is correct
69 Correct 2 ms 6236 KB Output is correct
70 Correct 2 ms 6236 KB Output is correct
71 Correct 2 ms 6236 KB Output is correct
72 Correct 2 ms 6236 KB Output is correct
73 Correct 2 ms 6236 KB Output is correct
74 Correct 2 ms 6236 KB Output is correct
75 Correct 2 ms 6256 KB Output is correct
76 Correct 2 ms 6236 KB Output is correct
77 Correct 2 ms 6236 KB Output is correct
78 Correct 1 ms 6236 KB Output is correct
79 Correct 3 ms 6260 KB Output is correct
80 Correct 2 ms 6488 KB Output is correct
81 Correct 2 ms 6236 KB Output is correct
82 Correct 2 ms 6236 KB Output is correct
83 Correct 2 ms 6236 KB Output is correct