답안 #630988

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
630988 2022-08-17T12:03:26 Z cadmiumsky Mergers (JOI19_mergers) C++14
100 / 100
1351 ms 130368 KB
#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;

using ll = long long;

//#define int ll
#define sz(x) ((int)(x).size())

using pii = pair<int,int>;
using tii = tuple<int,int,int>;
const int nmax = 5e5 + 5, nbit = 19;

int n, k;
vector<int> g[nmax];

int pin[nmax], pout[nmax], inp = 1;
namespace LCA {
  int p[nmax][nbit];
  void init(int node, int f) {
    pin[node] = inp++;
    p[node][0] = f;
    for(int i = 1; i < nbit; i++)
      p[node][i] = p[p[node][i - 1]][i - 1];
    for(auto x : g[node])
      if(x == f) continue;
      else init(x, node);
    pout[node] = inp++;
  }
  bool isanc(int x, int y) {return pin[x] <= pin[y] && pout[y] <= pout[x]; }
  int lca(int x, int y) {
    if(isanc(x, y)) return x;
    if(isanc(y, x)) return y;
    for(int i = nbit - 1; i >= 0; i--)
      if(!isanc(p[x][i], y))	
	x = p[x][i];
    return p[x][0];
  }
};

vector<int> ofcol[nmax];
int lca[nmax];
int start[nmax], fin[nmax];
int cnt;

vector<int> cuts;
int identify(int node, int f) {
  int sum = start[node] + fin[node];
  for(auto x : g[node]) {
    if(x == f) continue;
    int tmp = identify(x, node);
    if(tmp == 0)
      cuts.push_back(x);
    else
      sum += tmp;
  }
  return sum;
}

int noise_reduction(int node, int f) {
  int sum = 0;
  for(auto x : g[node]) {
    if(x == f) continue;
    sum += noise_reduction(x, node);
  }
  if((sum == 0 && start[node] == 1 && node != f) || (node == f && sum == 1))
    cnt++;
  return !start[node] * sum + start[node];
}

signed main() {
  cin >> n >> k;
  for(int i = 1, x, y; i < n; i++) {
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  LCA::init(1, 1);
  for(int i = 1, x; i <= n; i++) {
    cin >> x;
    ofcol[x].push_back(i); 
  }
  for(int i = 1; i <= k; i++) {
    lca[i] = ofcol[i].back();
    for(auto x : ofcol[i])
      lca[i] = LCA::lca(x, lca[i]);
    for(auto x : ofcol[i])
      start[x]++, 
      fin[lca[i]]--;
  }
  
  identify(1, 1);
  
  cuts.push_back(1);
  for(int i = 1; i <= n; i++)
    start[i] = 0;
  for(auto x : cuts)
    start[x] = 1;
   noise_reduction(1, 1);
   
  cout << (cnt + 1) / 2 << '\n';
}

/**
  De-atâtea nopți aud plouând,
  Aud materia plângând..
  Sînt singur, și mă duce un gând
  Spre locuințele lacustre.

  Și parcă dorm pe scânduri ude,
  În spate mă izbește-un val --
  Tresar prin somn și mi se pare
  Că n-am tras podul de la mal.

  Un gol istoric se întinde,
  Pe-același vremuri mă găsesc..
  Și simt cum de atâta ploaie
  Pilonii grei se prăbușesc.

  De-atâtea nopți aud plouând,
  Tot tresărind, tot așteptând..
  Sînt singur, și mă duce-un gând
  Spre locuințele lacustre.
-- George Bacovia, Lacustră
*/

# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 18 ms 23752 KB Output is correct
3 Correct 12 ms 23824 KB Output is correct
4 Correct 14 ms 23764 KB Output is correct
5 Correct 18 ms 23764 KB Output is correct
6 Correct 13 ms 23764 KB Output is correct
7 Correct 18 ms 23764 KB Output is correct
8 Correct 17 ms 23764 KB Output is correct
9 Correct 16 ms 23784 KB Output is correct
10 Correct 13 ms 23752 KB Output is correct
11 Correct 12 ms 23804 KB Output is correct
12 Correct 12 ms 23764 KB Output is correct
13 Correct 13 ms 23792 KB Output is correct
14 Correct 13 ms 23764 KB Output is correct
15 Correct 12 ms 23828 KB Output is correct
16 Correct 15 ms 23804 KB Output is correct
17 Correct 12 ms 23764 KB Output is correct
18 Correct 17 ms 23884 KB Output is correct
19 Correct 16 ms 23764 KB Output is correct
20 Correct 18 ms 23780 KB Output is correct
21 Correct 12 ms 23748 KB Output is correct
22 Correct 15 ms 23764 KB Output is correct
23 Correct 15 ms 23716 KB Output is correct
24 Correct 12 ms 23764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 18 ms 23752 KB Output is correct
3 Correct 12 ms 23824 KB Output is correct
4 Correct 14 ms 23764 KB Output is correct
5 Correct 18 ms 23764 KB Output is correct
6 Correct 13 ms 23764 KB Output is correct
7 Correct 18 ms 23764 KB Output is correct
8 Correct 17 ms 23764 KB Output is correct
9 Correct 16 ms 23784 KB Output is correct
10 Correct 13 ms 23752 KB Output is correct
11 Correct 12 ms 23804 KB Output is correct
12 Correct 12 ms 23764 KB Output is correct
13 Correct 13 ms 23792 KB Output is correct
14 Correct 13 ms 23764 KB Output is correct
15 Correct 12 ms 23828 KB Output is correct
16 Correct 15 ms 23804 KB Output is correct
17 Correct 12 ms 23764 KB Output is correct
18 Correct 17 ms 23884 KB Output is correct
19 Correct 16 ms 23764 KB Output is correct
20 Correct 18 ms 23780 KB Output is correct
21 Correct 12 ms 23748 KB Output is correct
22 Correct 15 ms 23764 KB Output is correct
23 Correct 15 ms 23716 KB Output is correct
24 Correct 12 ms 23764 KB Output is correct
25 Correct 12 ms 23808 KB Output is correct
26 Correct 21 ms 24236 KB Output is correct
27 Correct 15 ms 24200 KB Output is correct
28 Correct 16 ms 24376 KB Output is correct
29 Correct 17 ms 24328 KB Output is correct
30 Correct 15 ms 24176 KB Output is correct
31 Correct 13 ms 23844 KB Output is correct
32 Correct 16 ms 24396 KB Output is correct
33 Correct 14 ms 23764 KB Output is correct
34 Correct 19 ms 24140 KB Output is correct
35 Correct 17 ms 24328 KB Output is correct
36 Correct 16 ms 24200 KB Output is correct
37 Correct 15 ms 24276 KB Output is correct
38 Correct 16 ms 23764 KB Output is correct
39 Correct 15 ms 24292 KB Output is correct
40 Correct 16 ms 24200 KB Output is correct
41 Correct 18 ms 24196 KB Output is correct
42 Correct 15 ms 24276 KB Output is correct
43 Correct 16 ms 24444 KB Output is correct
44 Correct 20 ms 23796 KB Output is correct
45 Correct 21 ms 24276 KB Output is correct
46 Correct 22 ms 24276 KB Output is correct
47 Correct 17 ms 23764 KB Output is correct
48 Correct 17 ms 24276 KB Output is correct
49 Correct 20 ms 24328 KB Output is correct
50 Correct 16 ms 24404 KB Output is correct
51 Correct 15 ms 24204 KB Output is correct
52 Correct 21 ms 24280 KB Output is correct
53 Correct 15 ms 24276 KB Output is correct
54 Correct 20 ms 24148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 18 ms 23752 KB Output is correct
3 Correct 12 ms 23824 KB Output is correct
4 Correct 14 ms 23764 KB Output is correct
5 Correct 18 ms 23764 KB Output is correct
6 Correct 13 ms 23764 KB Output is correct
7 Correct 18 ms 23764 KB Output is correct
8 Correct 17 ms 23764 KB Output is correct
9 Correct 16 ms 23784 KB Output is correct
10 Correct 13 ms 23752 KB Output is correct
11 Correct 12 ms 23804 KB Output is correct
12 Correct 12 ms 23764 KB Output is correct
13 Correct 13 ms 23792 KB Output is correct
14 Correct 13 ms 23764 KB Output is correct
15 Correct 12 ms 23828 KB Output is correct
16 Correct 15 ms 23804 KB Output is correct
17 Correct 12 ms 23764 KB Output is correct
18 Correct 17 ms 23884 KB Output is correct
19 Correct 16 ms 23764 KB Output is correct
20 Correct 18 ms 23780 KB Output is correct
21 Correct 12 ms 23748 KB Output is correct
22 Correct 15 ms 23764 KB Output is correct
23 Correct 15 ms 23716 KB Output is correct
24 Correct 12 ms 23764 KB Output is correct
25 Correct 12 ms 23764 KB Output is correct
26 Correct 127 ms 36948 KB Output is correct
27 Correct 167 ms 36696 KB Output is correct
28 Correct 15 ms 24276 KB Output is correct
29 Correct 18 ms 23764 KB Output is correct
30 Correct 13 ms 23804 KB Output is correct
31 Correct 141 ms 36976 KB Output is correct
32 Correct 20 ms 24216 KB Output is correct
33 Correct 203 ms 44332 KB Output is correct
34 Correct 155 ms 36940 KB Output is correct
35 Correct 15 ms 24148 KB Output is correct
36 Correct 177 ms 37760 KB Output is correct
37 Correct 14 ms 24148 KB Output is correct
38 Correct 15 ms 24276 KB Output is correct
39 Correct 115 ms 37300 KB Output is correct
40 Correct 21 ms 24392 KB Output is correct
41 Correct 154 ms 36920 KB Output is correct
42 Correct 167 ms 39320 KB Output is correct
43 Correct 18 ms 23764 KB Output is correct
44 Correct 165 ms 44456 KB Output is correct
45 Correct 166 ms 40704 KB Output is correct
46 Correct 15 ms 24276 KB Output is correct
47 Correct 16 ms 24232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 108 ms 36464 KB Output is correct
2 Correct 151 ms 41324 KB Output is correct
3 Correct 19 ms 24276 KB Output is correct
4 Correct 23 ms 24140 KB Output is correct
5 Correct 15 ms 23788 KB Output is correct
6 Correct 18 ms 23844 KB Output is correct
7 Correct 15 ms 24252 KB Output is correct
8 Correct 164 ms 38600 KB Output is correct
9 Correct 21 ms 24148 KB Output is correct
10 Correct 176 ms 37388 KB Output is correct
11 Correct 17 ms 23764 KB Output is correct
12 Correct 160 ms 37264 KB Output is correct
13 Correct 186 ms 38696 KB Output is correct
14 Correct 218 ms 41028 KB Output is correct
15 Correct 109 ms 37268 KB Output is correct
16 Correct 17 ms 24268 KB Output is correct
17 Correct 15 ms 23772 KB Output is correct
18 Correct 146 ms 41084 KB Output is correct
19 Correct 232 ms 44988 KB Output is correct
20 Correct 21 ms 24272 KB Output is correct
21 Correct 15 ms 23804 KB Output is correct
22 Correct 134 ms 39012 KB Output is correct
23 Correct 17 ms 24188 KB Output is correct
24 Correct 160 ms 37696 KB Output is correct
25 Correct 202 ms 43556 KB Output is correct
26 Correct 21 ms 24276 KB Output is correct
27 Correct 20 ms 24328 KB Output is correct
28 Correct 16 ms 24228 KB Output is correct
29 Correct 15 ms 24276 KB Output is correct
30 Correct 22 ms 24236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 18 ms 23752 KB Output is correct
3 Correct 12 ms 23824 KB Output is correct
4 Correct 14 ms 23764 KB Output is correct
5 Correct 18 ms 23764 KB Output is correct
6 Correct 13 ms 23764 KB Output is correct
7 Correct 18 ms 23764 KB Output is correct
8 Correct 17 ms 23764 KB Output is correct
9 Correct 16 ms 23784 KB Output is correct
10 Correct 13 ms 23752 KB Output is correct
11 Correct 12 ms 23804 KB Output is correct
12 Correct 12 ms 23764 KB Output is correct
13 Correct 13 ms 23792 KB Output is correct
14 Correct 13 ms 23764 KB Output is correct
15 Correct 12 ms 23828 KB Output is correct
16 Correct 15 ms 23804 KB Output is correct
17 Correct 12 ms 23764 KB Output is correct
18 Correct 17 ms 23884 KB Output is correct
19 Correct 16 ms 23764 KB Output is correct
20 Correct 18 ms 23780 KB Output is correct
21 Correct 12 ms 23748 KB Output is correct
22 Correct 15 ms 23764 KB Output is correct
23 Correct 15 ms 23716 KB Output is correct
24 Correct 12 ms 23764 KB Output is correct
25 Correct 12 ms 23808 KB Output is correct
26 Correct 21 ms 24236 KB Output is correct
27 Correct 15 ms 24200 KB Output is correct
28 Correct 16 ms 24376 KB Output is correct
29 Correct 17 ms 24328 KB Output is correct
30 Correct 15 ms 24176 KB Output is correct
31 Correct 13 ms 23844 KB Output is correct
32 Correct 16 ms 24396 KB Output is correct
33 Correct 14 ms 23764 KB Output is correct
34 Correct 19 ms 24140 KB Output is correct
35 Correct 17 ms 24328 KB Output is correct
36 Correct 16 ms 24200 KB Output is correct
37 Correct 15 ms 24276 KB Output is correct
38 Correct 16 ms 23764 KB Output is correct
39 Correct 15 ms 24292 KB Output is correct
40 Correct 16 ms 24200 KB Output is correct
41 Correct 18 ms 24196 KB Output is correct
42 Correct 15 ms 24276 KB Output is correct
43 Correct 16 ms 24444 KB Output is correct
44 Correct 20 ms 23796 KB Output is correct
45 Correct 21 ms 24276 KB Output is correct
46 Correct 22 ms 24276 KB Output is correct
47 Correct 17 ms 23764 KB Output is correct
48 Correct 17 ms 24276 KB Output is correct
49 Correct 20 ms 24328 KB Output is correct
50 Correct 16 ms 24404 KB Output is correct
51 Correct 15 ms 24204 KB Output is correct
52 Correct 21 ms 24280 KB Output is correct
53 Correct 15 ms 24276 KB Output is correct
54 Correct 20 ms 24148 KB Output is correct
55 Correct 12 ms 23764 KB Output is correct
56 Correct 127 ms 36948 KB Output is correct
57 Correct 167 ms 36696 KB Output is correct
58 Correct 15 ms 24276 KB Output is correct
59 Correct 18 ms 23764 KB Output is correct
60 Correct 13 ms 23804 KB Output is correct
61 Correct 141 ms 36976 KB Output is correct
62 Correct 20 ms 24216 KB Output is correct
63 Correct 203 ms 44332 KB Output is correct
64 Correct 155 ms 36940 KB Output is correct
65 Correct 15 ms 24148 KB Output is correct
66 Correct 177 ms 37760 KB Output is correct
67 Correct 14 ms 24148 KB Output is correct
68 Correct 15 ms 24276 KB Output is correct
69 Correct 115 ms 37300 KB Output is correct
70 Correct 21 ms 24392 KB Output is correct
71 Correct 154 ms 36920 KB Output is correct
72 Correct 167 ms 39320 KB Output is correct
73 Correct 18 ms 23764 KB Output is correct
74 Correct 165 ms 44456 KB Output is correct
75 Correct 166 ms 40704 KB Output is correct
76 Correct 15 ms 24276 KB Output is correct
77 Correct 16 ms 24232 KB Output is correct
78 Correct 108 ms 36464 KB Output is correct
79 Correct 151 ms 41324 KB Output is correct
80 Correct 19 ms 24276 KB Output is correct
81 Correct 23 ms 24140 KB Output is correct
82 Correct 15 ms 23788 KB Output is correct
83 Correct 18 ms 23844 KB Output is correct
84 Correct 15 ms 24252 KB Output is correct
85 Correct 164 ms 38600 KB Output is correct
86 Correct 21 ms 24148 KB Output is correct
87 Correct 176 ms 37388 KB Output is correct
88 Correct 17 ms 23764 KB Output is correct
89 Correct 160 ms 37264 KB Output is correct
90 Correct 186 ms 38696 KB Output is correct
91 Correct 218 ms 41028 KB Output is correct
92 Correct 109 ms 37268 KB Output is correct
93 Correct 17 ms 24268 KB Output is correct
94 Correct 15 ms 23772 KB Output is correct
95 Correct 146 ms 41084 KB Output is correct
96 Correct 232 ms 44988 KB Output is correct
97 Correct 21 ms 24272 KB Output is correct
98 Correct 15 ms 23804 KB Output is correct
99 Correct 134 ms 39012 KB Output is correct
100 Correct 17 ms 24188 KB Output is correct
101 Correct 160 ms 37696 KB Output is correct
102 Correct 202 ms 43556 KB Output is correct
103 Correct 21 ms 24276 KB Output is correct
104 Correct 20 ms 24328 KB Output is correct
105 Correct 16 ms 24228 KB Output is correct
106 Correct 15 ms 24276 KB Output is correct
107 Correct 22 ms 24236 KB Output is correct
108 Correct 860 ms 95736 KB Output is correct
109 Correct 1104 ms 104724 KB Output is correct
110 Correct 1086 ms 116056 KB Output is correct
111 Correct 1351 ms 130368 KB Output is correct
112 Correct 1270 ms 119528 KB Output is correct
113 Correct 882 ms 105876 KB Output is correct
114 Correct 884 ms 86024 KB Output is correct
115 Correct 866 ms 85836 KB Output is correct
116 Correct 1076 ms 91796 KB Output is correct
117 Correct 1112 ms 106408 KB Output is correct
118 Correct 902 ms 87728 KB Output is correct
119 Correct 1141 ms 106268 KB Output is correct
120 Correct 1276 ms 120260 KB Output is correct
121 Correct 1102 ms 106372 KB Output is correct
122 Correct 1137 ms 97700 KB Output is correct
123 Correct 845 ms 108636 KB Output is correct
124 Correct 1262 ms 104624 KB Output is correct