답안 #520910

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
520910 2022-01-31T13:01:22 Z cig32 Mergers (JOI19_mergers) C++17
10 / 100
3000 ms 44432 KB
#include "bits/stdc++.h"
#define int long long
using namespace std;
const int MAXN = 5e5 + 10;
vector<int> adj[MAXN];
vector<int> adj2[MAXN];
int s[MAXN];
int dsu[MAXN];
int set_of(int u) {
  if(dsu[u] == u) return u;
  return dsu[u] = set_of(dsu[u]);
}
void union_(int u, int v) {
  dsu[set_of(u)] = set_of(v);
}
int dfs(int node, int prev, int q, int dep) {
  if(dep > 101) return 0;
  //stat: do the ancestors contain color q
  int cnt = (s[node] == q);
  for(int x: adj[node]) {
    if(x != prev) {
      int d = dfs(x, node, q, dep + 1);
      cnt += d;
      if(d > 0 && set_of(node) != set_of(x)) {
        union_(node, x);
      }
    }
  }
  //ret: how many color q in subtree
  return cnt;
}
int32_t main() {
  int n,k;cin >> n >> k;
  for(int i=1;i<n;i++) {
    int a,b;cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  for(int i=1; i<=n; i++) cin >> s[i];
  for(int i=0; i<=n; i++) dsu[i] = i;
  for(int i=1; i<=k; i++) {
    for(int j=1; j<=n; j++) {
      if(s[j] == i) {
        dfs(j, -1, i, 0);
        break;
      }
    }
  }
  map<pair<int, int>, bool> mp;
  for(int i=1; i<=n; i++) {
    for(int x: adj[i]) {
      if(set_of(i) != set_of(x) && mp[{set_of(i), set_of(x)}] == 0) {
        mp[{set_of(i), set_of(x)}] = 1;
        mp[{set_of(x), set_of(i)}] = 1;
        adj2[set_of(i)].push_back(set_of(x));
        adj2[set_of(x)].push_back(set_of(i));
      }
    }
  }
  int s = 0;
  for(int i=1; i<=n; i++) {
    s += (adj2[i].size() == 1);
  }
  cout << (s + 1) / 2 << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23756 KB Output is correct
2 Correct 13 ms 23756 KB Output is correct
3 Correct 13 ms 23780 KB Output is correct
4 Correct 14 ms 23756 KB Output is correct
5 Correct 13 ms 23676 KB Output is correct
6 Correct 13 ms 23796 KB Output is correct
7 Correct 13 ms 23788 KB Output is correct
8 Correct 13 ms 23800 KB Output is correct
9 Correct 12 ms 23756 KB Output is correct
10 Correct 12 ms 23756 KB Output is correct
11 Correct 14 ms 23788 KB Output is correct
12 Correct 16 ms 23800 KB Output is correct
13 Correct 14 ms 23756 KB Output is correct
14 Correct 14 ms 23788 KB Output is correct
15 Correct 13 ms 23756 KB Output is correct
16 Correct 13 ms 23756 KB Output is correct
17 Correct 14 ms 23756 KB Output is correct
18 Correct 13 ms 23728 KB Output is correct
19 Correct 14 ms 23776 KB Output is correct
20 Correct 15 ms 23792 KB Output is correct
21 Correct 14 ms 23756 KB Output is correct
22 Correct 13 ms 23788 KB Output is correct
23 Correct 12 ms 23792 KB Output is correct
24 Correct 12 ms 23756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23756 KB Output is correct
2 Correct 13 ms 23756 KB Output is correct
3 Correct 13 ms 23780 KB Output is correct
4 Correct 14 ms 23756 KB Output is correct
5 Correct 13 ms 23676 KB Output is correct
6 Correct 13 ms 23796 KB Output is correct
7 Correct 13 ms 23788 KB Output is correct
8 Correct 13 ms 23800 KB Output is correct
9 Correct 12 ms 23756 KB Output is correct
10 Correct 12 ms 23756 KB Output is correct
11 Correct 14 ms 23788 KB Output is correct
12 Correct 16 ms 23800 KB Output is correct
13 Correct 14 ms 23756 KB Output is correct
14 Correct 14 ms 23788 KB Output is correct
15 Correct 13 ms 23756 KB Output is correct
16 Correct 13 ms 23756 KB Output is correct
17 Correct 14 ms 23756 KB Output is correct
18 Correct 13 ms 23728 KB Output is correct
19 Correct 14 ms 23776 KB Output is correct
20 Correct 15 ms 23792 KB Output is correct
21 Correct 14 ms 23756 KB Output is correct
22 Correct 13 ms 23788 KB Output is correct
23 Correct 12 ms 23792 KB Output is correct
24 Correct 12 ms 23756 KB Output is correct
25 Correct 12 ms 23756 KB Output is correct
26 Correct 104 ms 24224 KB Output is correct
27 Correct 16 ms 23932 KB Output is correct
28 Incorrect 40 ms 24384 KB Output isn't correct
29 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23756 KB Output is correct
2 Correct 13 ms 23756 KB Output is correct
3 Correct 13 ms 23780 KB Output is correct
4 Correct 14 ms 23756 KB Output is correct
5 Correct 13 ms 23676 KB Output is correct
6 Correct 13 ms 23796 KB Output is correct
7 Correct 13 ms 23788 KB Output is correct
8 Correct 13 ms 23800 KB Output is correct
9 Correct 12 ms 23756 KB Output is correct
10 Correct 12 ms 23756 KB Output is correct
11 Correct 14 ms 23788 KB Output is correct
12 Correct 16 ms 23800 KB Output is correct
13 Correct 14 ms 23756 KB Output is correct
14 Correct 14 ms 23788 KB Output is correct
15 Correct 13 ms 23756 KB Output is correct
16 Correct 13 ms 23756 KB Output is correct
17 Correct 14 ms 23756 KB Output is correct
18 Correct 13 ms 23728 KB Output is correct
19 Correct 14 ms 23776 KB Output is correct
20 Correct 15 ms 23792 KB Output is correct
21 Correct 14 ms 23756 KB Output is correct
22 Correct 13 ms 23788 KB Output is correct
23 Correct 12 ms 23792 KB Output is correct
24 Correct 12 ms 23756 KB Output is correct
25 Correct 14 ms 23756 KB Output is correct
26 Correct 165 ms 31088 KB Output is correct
27 Correct 447 ms 30508 KB Output is correct
28 Correct 16 ms 23884 KB Output is correct
29 Correct 18 ms 23756 KB Output is correct
30 Correct 17 ms 23756 KB Output is correct
31 Correct 286 ms 30464 KB Output is correct
32 Correct 16 ms 24012 KB Output is correct
33 Incorrect 229 ms 44432 KB Output isn't correct
34 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 185 ms 29856 KB Output is correct
2 Execution timed out 3067 ms 29368 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23756 KB Output is correct
2 Correct 13 ms 23756 KB Output is correct
3 Correct 13 ms 23780 KB Output is correct
4 Correct 14 ms 23756 KB Output is correct
5 Correct 13 ms 23676 KB Output is correct
6 Correct 13 ms 23796 KB Output is correct
7 Correct 13 ms 23788 KB Output is correct
8 Correct 13 ms 23800 KB Output is correct
9 Correct 12 ms 23756 KB Output is correct
10 Correct 12 ms 23756 KB Output is correct
11 Correct 14 ms 23788 KB Output is correct
12 Correct 16 ms 23800 KB Output is correct
13 Correct 14 ms 23756 KB Output is correct
14 Correct 14 ms 23788 KB Output is correct
15 Correct 13 ms 23756 KB Output is correct
16 Correct 13 ms 23756 KB Output is correct
17 Correct 14 ms 23756 KB Output is correct
18 Correct 13 ms 23728 KB Output is correct
19 Correct 14 ms 23776 KB Output is correct
20 Correct 15 ms 23792 KB Output is correct
21 Correct 14 ms 23756 KB Output is correct
22 Correct 13 ms 23788 KB Output is correct
23 Correct 12 ms 23792 KB Output is correct
24 Correct 12 ms 23756 KB Output is correct
25 Correct 12 ms 23756 KB Output is correct
26 Correct 104 ms 24224 KB Output is correct
27 Correct 16 ms 23932 KB Output is correct
28 Incorrect 40 ms 24384 KB Output isn't correct
29 Halted 0 ms 0 KB -