# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
200891 |
2020-02-08T12:09:06 Z |
EntityIT |
Mergers (JOI19_mergers) |
C++14 |
|
916 ms |
171320 KB |
#include<bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define sz(x) ( (int)(x).size() )
using LL = long long;
template<class T>
inline bool asMn(T &a, const T &b) { return a > b ? a = b, true : false; }
template<class T>
inline bool asMx(T &a, const T &b) { return a < b ? a = b, true : false; }
const int inf = 1e9;
mt19937 rng( (uint32_t)chrono::steady_clock::now().time_since_epoch().count() );
int n, k;
vector<vector<int> > gr;
vector<int> be, en, ver;
void dfs(int u, int pr) {
static int iTime = 0;
ver[iTime] = u;
be[u] = iTime++;
for (int v : gr[u]) if (v != pr) dfs(v, u);
en[u] = iTime - 1;
}
pair<int, int> operator + (const pair<int, int> &a, const pair<int, int> &b) {
return { min(a.first, b.first), max(a.second, b.second) };
}
vector<vector<pair<int, int> > > rmq;
pair<int, int> get(int l, int r) {
int i = __lg(r - l + 1);
return rmq[i][l] + rmq[i][r - (1 << i) + 1];
}
struct Dsu {
vector<int> pSet;
Dsu(int nSet = 0) { pSet.assign(nSet, 0); iota(all(pSet), 0); }
void reset(int nSet = 0) { pSet.assign(nSet, 0); iota(all(pSet), 0); }
int findSet(int i) { return i == pSet[i] ? i : pSet[i] = findSet(pSet[i]); }
void unite(int i, int j) {
i = findSet(i); j = findSet(j);
if (i == j) return ;
pSet[i] = j;
}
} dsu;
vector<pair<int, int> > edge;
void solve(int u, int pr) {
for (int v : gr[u]) if (v != pr) {
auto tmp = get(be[v], en[v]);
if (be[v] <= tmp.first && tmp.second <= en[v]) edge.emplace_back(u, v);
else dsu.unite(u, v);
solve(v, u);
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef FourLeafClover
freopen("input", "r", stdin);
#endif // FourLeafCLover
cin >> n >> k;
gr.assign(n, {} );
for (int i = 1; i < n; ++i) {
int u, v; cin >> u >> v; --u; --v;
gr[u].emplace_back(v);
gr[v].emplace_back(u);
}
be.assign(n, 0);
en.assign(n, 0);
ver.assign(n, 0);
dfs(0, -1);
vector<int> s(n), L(k, inf), R(k, -inf);
for (int u = 0; u < n; ++u) {
cin >> s[u]; --s[u];
asMn(L[ s[u] ], be[u]);
asMx(R[ s[u] ], be[u]);
}
rmq.assign(__lg(n) + 1, vector<pair<int, int> >(n, { 0, 0 } ) );
for (int u = 0; u < n; ++u) rmq[0][ be[u] ] = { L[ s[u] ], R[ s[u] ] };
for (int j = 1; j < sz(rmq); ++j) {
for (int i = 0; i + (1 << j) <= n; ++i) rmq[j][i] = rmq[j - 1][i] + rmq[j - 1][i + (1 << (j - 1) )];
}
dsu.reset(n);
solve(0, -1);
vector<int> cnt(n);
for (auto e : edge) {
++cnt[ dsu.findSet(e.first) ];
++cnt[ dsu.findSet(e.second) ];
}
cout << ( (int)(count(all(cnt), 1) ) + 1) / 2 << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
248 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
5 ms |
380 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
20 |
Correct |
5 ms |
376 KB |
Output is correct |
21 |
Correct |
5 ms |
376 KB |
Output is correct |
22 |
Correct |
5 ms |
376 KB |
Output is correct |
23 |
Correct |
5 ms |
376 KB |
Output is correct |
24 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
248 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
5 ms |
380 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
20 |
Correct |
5 ms |
376 KB |
Output is correct |
21 |
Correct |
5 ms |
376 KB |
Output is correct |
22 |
Correct |
5 ms |
376 KB |
Output is correct |
23 |
Correct |
5 ms |
376 KB |
Output is correct |
24 |
Correct |
5 ms |
376 KB |
Output is correct |
25 |
Correct |
5 ms |
376 KB |
Output is correct |
26 |
Correct |
7 ms |
888 KB |
Output is correct |
27 |
Correct |
8 ms |
888 KB |
Output is correct |
28 |
Correct |
7 ms |
1144 KB |
Output is correct |
29 |
Correct |
7 ms |
1016 KB |
Output is correct |
30 |
Correct |
6 ms |
888 KB |
Output is correct |
31 |
Correct |
5 ms |
376 KB |
Output is correct |
32 |
Correct |
7 ms |
1144 KB |
Output is correct |
33 |
Correct |
5 ms |
376 KB |
Output is correct |
34 |
Correct |
6 ms |
888 KB |
Output is correct |
35 |
Correct |
7 ms |
1016 KB |
Output is correct |
36 |
Correct |
7 ms |
888 KB |
Output is correct |
37 |
Correct |
7 ms |
888 KB |
Output is correct |
38 |
Correct |
5 ms |
380 KB |
Output is correct |
39 |
Correct |
7 ms |
888 KB |
Output is correct |
40 |
Correct |
6 ms |
888 KB |
Output is correct |
41 |
Correct |
6 ms |
888 KB |
Output is correct |
42 |
Correct |
7 ms |
1016 KB |
Output is correct |
43 |
Correct |
7 ms |
1272 KB |
Output is correct |
44 |
Correct |
5 ms |
376 KB |
Output is correct |
45 |
Correct |
8 ms |
1016 KB |
Output is correct |
46 |
Correct |
7 ms |
888 KB |
Output is correct |
47 |
Correct |
5 ms |
376 KB |
Output is correct |
48 |
Correct |
7 ms |
888 KB |
Output is correct |
49 |
Correct |
6 ms |
1016 KB |
Output is correct |
50 |
Correct |
7 ms |
1144 KB |
Output is correct |
51 |
Correct |
7 ms |
888 KB |
Output is correct |
52 |
Correct |
6 ms |
888 KB |
Output is correct |
53 |
Correct |
7 ms |
888 KB |
Output is correct |
54 |
Correct |
7 ms |
916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
248 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
5 ms |
380 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
20 |
Correct |
5 ms |
376 KB |
Output is correct |
21 |
Correct |
5 ms |
376 KB |
Output is correct |
22 |
Correct |
5 ms |
376 KB |
Output is correct |
23 |
Correct |
5 ms |
376 KB |
Output is correct |
24 |
Correct |
5 ms |
376 KB |
Output is correct |
25 |
Correct |
5 ms |
376 KB |
Output is correct |
26 |
Correct |
72 ms |
23408 KB |
Output is correct |
27 |
Correct |
99 ms |
23108 KB |
Output is correct |
28 |
Correct |
6 ms |
888 KB |
Output is correct |
29 |
Correct |
5 ms |
376 KB |
Output is correct |
30 |
Correct |
5 ms |
376 KB |
Output is correct |
31 |
Correct |
98 ms |
23032 KB |
Output is correct |
32 |
Correct |
6 ms |
888 KB |
Output is correct |
33 |
Correct |
117 ms |
34536 KB |
Output is correct |
34 |
Correct |
100 ms |
23032 KB |
Output is correct |
35 |
Correct |
7 ms |
888 KB |
Output is correct |
36 |
Correct |
110 ms |
24296 KB |
Output is correct |
37 |
Correct |
6 ms |
888 KB |
Output is correct |
38 |
Correct |
6 ms |
888 KB |
Output is correct |
39 |
Correct |
71 ms |
23408 KB |
Output is correct |
40 |
Correct |
7 ms |
1272 KB |
Output is correct |
41 |
Correct |
106 ms |
23044 KB |
Output is correct |
42 |
Correct |
117 ms |
26856 KB |
Output is correct |
43 |
Correct |
5 ms |
376 KB |
Output is correct |
44 |
Correct |
118 ms |
35048 KB |
Output is correct |
45 |
Correct |
105 ms |
29032 KB |
Output is correct |
46 |
Correct |
7 ms |
888 KB |
Output is correct |
47 |
Correct |
7 ms |
888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
22000 KB |
Output is correct |
2 |
Correct |
74 ms |
24160 KB |
Output is correct |
3 |
Correct |
7 ms |
888 KB |
Output is correct |
4 |
Correct |
7 ms |
888 KB |
Output is correct |
5 |
Correct |
5 ms |
380 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
6 ms |
888 KB |
Output is correct |
8 |
Correct |
101 ms |
24040 KB |
Output is correct |
9 |
Correct |
7 ms |
888 KB |
Output is correct |
10 |
Correct |
96 ms |
23160 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
120 ms |
23144 KB |
Output is correct |
13 |
Correct |
104 ms |
24168 KB |
Output is correct |
14 |
Correct |
108 ms |
25552 KB |
Output is correct |
15 |
Correct |
67 ms |
23280 KB |
Output is correct |
16 |
Correct |
7 ms |
892 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
74 ms |
25696 KB |
Output is correct |
19 |
Correct |
120 ms |
31976 KB |
Output is correct |
20 |
Correct |
7 ms |
888 KB |
Output is correct |
21 |
Correct |
5 ms |
376 KB |
Output is correct |
22 |
Correct |
69 ms |
24288 KB |
Output is correct |
23 |
Correct |
7 ms |
888 KB |
Output is correct |
24 |
Correct |
111 ms |
23528 KB |
Output is correct |
25 |
Correct |
116 ms |
29800 KB |
Output is correct |
26 |
Correct |
6 ms |
1016 KB |
Output is correct |
27 |
Correct |
7 ms |
1016 KB |
Output is correct |
28 |
Correct |
7 ms |
888 KB |
Output is correct |
29 |
Correct |
6 ms |
888 KB |
Output is correct |
30 |
Correct |
6 ms |
1016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
248 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
5 ms |
380 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
20 |
Correct |
5 ms |
376 KB |
Output is correct |
21 |
Correct |
5 ms |
376 KB |
Output is correct |
22 |
Correct |
5 ms |
376 KB |
Output is correct |
23 |
Correct |
5 ms |
376 KB |
Output is correct |
24 |
Correct |
5 ms |
376 KB |
Output is correct |
25 |
Correct |
5 ms |
376 KB |
Output is correct |
26 |
Correct |
7 ms |
888 KB |
Output is correct |
27 |
Correct |
8 ms |
888 KB |
Output is correct |
28 |
Correct |
7 ms |
1144 KB |
Output is correct |
29 |
Correct |
7 ms |
1016 KB |
Output is correct |
30 |
Correct |
6 ms |
888 KB |
Output is correct |
31 |
Correct |
5 ms |
376 KB |
Output is correct |
32 |
Correct |
7 ms |
1144 KB |
Output is correct |
33 |
Correct |
5 ms |
376 KB |
Output is correct |
34 |
Correct |
6 ms |
888 KB |
Output is correct |
35 |
Correct |
7 ms |
1016 KB |
Output is correct |
36 |
Correct |
7 ms |
888 KB |
Output is correct |
37 |
Correct |
7 ms |
888 KB |
Output is correct |
38 |
Correct |
5 ms |
380 KB |
Output is correct |
39 |
Correct |
7 ms |
888 KB |
Output is correct |
40 |
Correct |
6 ms |
888 KB |
Output is correct |
41 |
Correct |
6 ms |
888 KB |
Output is correct |
42 |
Correct |
7 ms |
1016 KB |
Output is correct |
43 |
Correct |
7 ms |
1272 KB |
Output is correct |
44 |
Correct |
5 ms |
376 KB |
Output is correct |
45 |
Correct |
8 ms |
1016 KB |
Output is correct |
46 |
Correct |
7 ms |
888 KB |
Output is correct |
47 |
Correct |
5 ms |
376 KB |
Output is correct |
48 |
Correct |
7 ms |
888 KB |
Output is correct |
49 |
Correct |
6 ms |
1016 KB |
Output is correct |
50 |
Correct |
7 ms |
1144 KB |
Output is correct |
51 |
Correct |
7 ms |
888 KB |
Output is correct |
52 |
Correct |
6 ms |
888 KB |
Output is correct |
53 |
Correct |
7 ms |
888 KB |
Output is correct |
54 |
Correct |
7 ms |
916 KB |
Output is correct |
55 |
Correct |
5 ms |
376 KB |
Output is correct |
56 |
Correct |
72 ms |
23408 KB |
Output is correct |
57 |
Correct |
99 ms |
23108 KB |
Output is correct |
58 |
Correct |
6 ms |
888 KB |
Output is correct |
59 |
Correct |
5 ms |
376 KB |
Output is correct |
60 |
Correct |
5 ms |
376 KB |
Output is correct |
61 |
Correct |
98 ms |
23032 KB |
Output is correct |
62 |
Correct |
6 ms |
888 KB |
Output is correct |
63 |
Correct |
117 ms |
34536 KB |
Output is correct |
64 |
Correct |
100 ms |
23032 KB |
Output is correct |
65 |
Correct |
7 ms |
888 KB |
Output is correct |
66 |
Correct |
110 ms |
24296 KB |
Output is correct |
67 |
Correct |
6 ms |
888 KB |
Output is correct |
68 |
Correct |
6 ms |
888 KB |
Output is correct |
69 |
Correct |
71 ms |
23408 KB |
Output is correct |
70 |
Correct |
7 ms |
1272 KB |
Output is correct |
71 |
Correct |
106 ms |
23044 KB |
Output is correct |
72 |
Correct |
117 ms |
26856 KB |
Output is correct |
73 |
Correct |
5 ms |
376 KB |
Output is correct |
74 |
Correct |
118 ms |
35048 KB |
Output is correct |
75 |
Correct |
105 ms |
29032 KB |
Output is correct |
76 |
Correct |
7 ms |
888 KB |
Output is correct |
77 |
Correct |
7 ms |
888 KB |
Output is correct |
78 |
Correct |
66 ms |
22000 KB |
Output is correct |
79 |
Correct |
74 ms |
24160 KB |
Output is correct |
80 |
Correct |
7 ms |
888 KB |
Output is correct |
81 |
Correct |
7 ms |
888 KB |
Output is correct |
82 |
Correct |
5 ms |
380 KB |
Output is correct |
83 |
Correct |
5 ms |
376 KB |
Output is correct |
84 |
Correct |
6 ms |
888 KB |
Output is correct |
85 |
Correct |
101 ms |
24040 KB |
Output is correct |
86 |
Correct |
7 ms |
888 KB |
Output is correct |
87 |
Correct |
96 ms |
23160 KB |
Output is correct |
88 |
Correct |
5 ms |
376 KB |
Output is correct |
89 |
Correct |
120 ms |
23144 KB |
Output is correct |
90 |
Correct |
104 ms |
24168 KB |
Output is correct |
91 |
Correct |
108 ms |
25552 KB |
Output is correct |
92 |
Correct |
67 ms |
23280 KB |
Output is correct |
93 |
Correct |
7 ms |
892 KB |
Output is correct |
94 |
Correct |
5 ms |
376 KB |
Output is correct |
95 |
Correct |
74 ms |
25696 KB |
Output is correct |
96 |
Correct |
120 ms |
31976 KB |
Output is correct |
97 |
Correct |
7 ms |
888 KB |
Output is correct |
98 |
Correct |
5 ms |
376 KB |
Output is correct |
99 |
Correct |
69 ms |
24288 KB |
Output is correct |
100 |
Correct |
7 ms |
888 KB |
Output is correct |
101 |
Correct |
111 ms |
23528 KB |
Output is correct |
102 |
Correct |
116 ms |
29800 KB |
Output is correct |
103 |
Correct |
6 ms |
1016 KB |
Output is correct |
104 |
Correct |
7 ms |
1016 KB |
Output is correct |
105 |
Correct |
7 ms |
888 KB |
Output is correct |
106 |
Correct |
6 ms |
888 KB |
Output is correct |
107 |
Correct |
6 ms |
1016 KB |
Output is correct |
108 |
Correct |
493 ms |
128200 KB |
Output is correct |
109 |
Correct |
859 ms |
151424 KB |
Output is correct |
110 |
Correct |
860 ms |
169136 KB |
Output is correct |
111 |
Correct |
916 ms |
171320 KB |
Output is correct |
112 |
Correct |
826 ms |
154160 KB |
Output is correct |
113 |
Correct |
527 ms |
135452 KB |
Output is correct |
114 |
Correct |
697 ms |
122160 KB |
Output is correct |
115 |
Correct |
729 ms |
122216 KB |
Output is correct |
116 |
Correct |
750 ms |
124848 KB |
Output is correct |
117 |
Correct |
763 ms |
134564 KB |
Output is correct |
118 |
Correct |
708 ms |
120700 KB |
Output is correct |
119 |
Correct |
772 ms |
134540 KB |
Output is correct |
120 |
Correct |
843 ms |
157168 KB |
Output is correct |
121 |
Correct |
793 ms |
134352 KB |
Output is correct |
122 |
Correct |
770 ms |
128428 KB |
Output is correct |
123 |
Correct |
501 ms |
135976 KB |
Output is correct |
124 |
Correct |
878 ms |
130668 KB |
Output is correct |