# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
941175 |
2024-03-08T08:53:22 Z |
vjudge1 |
Mergers (JOI19_mergers) |
C++17 |
|
990 ms |
177256 KB |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define ull unsigned long long
#define pii pair<int,int>
#define pll pair<long long, long long>
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define lwb lower_bound
#define upb upper_bound
#define TASKNAME "NAME"
void init()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
///freopen(TASKNAME".INP","r",stdin); freopen(TASKNAME".OUT","w",stdout);
}
const int SZ = 5e5+5;
const ll INF = INT_MAX / 2, MOD = 1e9+7, INFLL = 2e18;
const double epsilon = 1e-3;
int n,k, s[SZ], h[SZ], up[SZ][20], sum[SZ];
vector<int> adj[SZ], adj2[SZ], vec[SZ];
vector<pii> edges;
struct DSU
{
int par[SZ], s[SZ];
void init(int n)
{
for(int i = 1; i <= n; i++)
{
par[i] = i;
s[i] = 1;
}
}
int get(int u)
{
return (par[u] == u ? u : par[u] = get(par[u]));
}
void join(int u, int v)
{
u = get(u);
v = get(v);
if(u == v) return;
if(s[u] < s[v]) swap(u,v);
par[v] = u;
s[u] += s[v];
}
bool joined(int u, int v)
{
u = get(u);
v = get(v);
return u == v;
}
} dsu;
void dfs1(int u, int pre)
{
for(int v : adj[u])
{
if(v == pre) continue;
up[v][0] = u;
for(int i = 1; i < 20; i++)
{
up[v][i] = up[up[v][i-1]][i-1];
}
h[v] = h[u] + 1;
dfs1(v, u);
}
}
int lca(int u, int v)
{
if(h[u] != h[v])
{
if(h[u] < h[v]) swap(u,v);
int dif = h[u] - h[v];
for(int i = 0; i < 20; i++)
{
if(dif >> i & 1) u = up[u][i];
}
}
if(u == v) return u;
for(int i = 19; i >= 0; i--)
{
if(up[u][i] == up[v][i]) continue;
u = up[u][i];
v = up[v][i];
}
return up[u][0];
}
void dfs2(int u, int pre)
{
for(int v : adj[u])
{
if(v == pre) continue;
dfs2(v, u);
sum[u] += sum[v];
}
if(sum[u] > 0) dsu.join(s[u],s[pre]);
}
int leaf = 0;
void dfs3(int u, int pre)
{
if(adj2[u].size() == 1) leaf++;
for(int v : adj2[u])
{
if(v == pre) continue;
dfs3(v, u);
}
}
int main()
{
init();
cin >> n >> k;
dsu.init(n);
for(int i = 1; i < n; i++)
{
int u,v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
edges.pb({u,v});
}
for(int i = 1; i <= n; i++)
{
cin >> s[i];
vec[s[i]].pb(i);
}
dfs1(1, 0);
for(int i = 1; i <= k; i++)
{
int root = vec[i][0];
for(int j = 1; j < vec[i].size(); j++)
{
root = lca(root, vec[i][j]);
}
for(int j : vec[i])
{
sum[j]++;
}
sum[root] -= vec[i].size();
}
dfs2(1, 0);
for(pii e : edges)
{
int u = dsu.get(s[e.fi]), v = dsu.get(s[e.se]);
if(u == v) continue;
adj2[u].pb(v);
adj2[v].pb(u);
}
for(int i = 1; i <= k; i++)
{
if(adj2[i].size())
{
dfs3(i,0);
break;
}
}
cout << (leaf + 1) / 2;
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:148:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
148 | for(int j = 1; j < vec[i].size(); j++)
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
43356 KB |
Output is correct |
2 |
Correct |
9 ms |
43356 KB |
Output is correct |
3 |
Correct |
10 ms |
43468 KB |
Output is correct |
4 |
Correct |
9 ms |
43472 KB |
Output is correct |
5 |
Correct |
10 ms |
43356 KB |
Output is correct |
6 |
Correct |
9 ms |
43356 KB |
Output is correct |
7 |
Correct |
8 ms |
41304 KB |
Output is correct |
8 |
Correct |
9 ms |
43356 KB |
Output is correct |
9 |
Correct |
9 ms |
43352 KB |
Output is correct |
10 |
Correct |
9 ms |
43356 KB |
Output is correct |
11 |
Correct |
9 ms |
43568 KB |
Output is correct |
12 |
Correct |
9 ms |
43352 KB |
Output is correct |
13 |
Correct |
9 ms |
43356 KB |
Output is correct |
14 |
Correct |
9 ms |
43356 KB |
Output is correct |
15 |
Correct |
9 ms |
43352 KB |
Output is correct |
16 |
Correct |
10 ms |
43544 KB |
Output is correct |
17 |
Correct |
10 ms |
43356 KB |
Output is correct |
18 |
Correct |
9 ms |
43468 KB |
Output is correct |
19 |
Correct |
9 ms |
43356 KB |
Output is correct |
20 |
Correct |
9 ms |
43352 KB |
Output is correct |
21 |
Correct |
9 ms |
43356 KB |
Output is correct |
22 |
Correct |
9 ms |
43356 KB |
Output is correct |
23 |
Correct |
9 ms |
43356 KB |
Output is correct |
24 |
Correct |
9 ms |
43560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
43356 KB |
Output is correct |
2 |
Correct |
9 ms |
43356 KB |
Output is correct |
3 |
Correct |
10 ms |
43468 KB |
Output is correct |
4 |
Correct |
9 ms |
43472 KB |
Output is correct |
5 |
Correct |
10 ms |
43356 KB |
Output is correct |
6 |
Correct |
9 ms |
43356 KB |
Output is correct |
7 |
Correct |
8 ms |
41304 KB |
Output is correct |
8 |
Correct |
9 ms |
43356 KB |
Output is correct |
9 |
Correct |
9 ms |
43352 KB |
Output is correct |
10 |
Correct |
9 ms |
43356 KB |
Output is correct |
11 |
Correct |
9 ms |
43568 KB |
Output is correct |
12 |
Correct |
9 ms |
43352 KB |
Output is correct |
13 |
Correct |
9 ms |
43356 KB |
Output is correct |
14 |
Correct |
9 ms |
43356 KB |
Output is correct |
15 |
Correct |
9 ms |
43352 KB |
Output is correct |
16 |
Correct |
10 ms |
43544 KB |
Output is correct |
17 |
Correct |
10 ms |
43356 KB |
Output is correct |
18 |
Correct |
9 ms |
43468 KB |
Output is correct |
19 |
Correct |
9 ms |
43356 KB |
Output is correct |
20 |
Correct |
9 ms |
43352 KB |
Output is correct |
21 |
Correct |
9 ms |
43356 KB |
Output is correct |
22 |
Correct |
9 ms |
43356 KB |
Output is correct |
23 |
Correct |
9 ms |
43356 KB |
Output is correct |
24 |
Correct |
9 ms |
43560 KB |
Output is correct |
25 |
Correct |
9 ms |
43352 KB |
Output is correct |
26 |
Correct |
11 ms |
43868 KB |
Output is correct |
27 |
Correct |
12 ms |
45660 KB |
Output is correct |
28 |
Correct |
11 ms |
44124 KB |
Output is correct |
29 |
Correct |
11 ms |
43868 KB |
Output is correct |
30 |
Correct |
11 ms |
43896 KB |
Output is correct |
31 |
Correct |
10 ms |
43496 KB |
Output is correct |
32 |
Correct |
11 ms |
44120 KB |
Output is correct |
33 |
Correct |
9 ms |
43352 KB |
Output is correct |
34 |
Correct |
12 ms |
45660 KB |
Output is correct |
35 |
Correct |
11 ms |
43868 KB |
Output is correct |
36 |
Correct |
11 ms |
43868 KB |
Output is correct |
37 |
Correct |
12 ms |
43868 KB |
Output is correct |
38 |
Correct |
9 ms |
43352 KB |
Output is correct |
39 |
Correct |
11 ms |
45660 KB |
Output is correct |
40 |
Correct |
10 ms |
43868 KB |
Output is correct |
41 |
Correct |
11 ms |
45660 KB |
Output is correct |
42 |
Correct |
12 ms |
45660 KB |
Output is correct |
43 |
Correct |
11 ms |
46052 KB |
Output is correct |
44 |
Correct |
10 ms |
43356 KB |
Output is correct |
45 |
Correct |
11 ms |
45780 KB |
Output is correct |
46 |
Correct |
11 ms |
43736 KB |
Output is correct |
47 |
Correct |
9 ms |
43356 KB |
Output is correct |
48 |
Correct |
11 ms |
43868 KB |
Output is correct |
49 |
Correct |
11 ms |
43864 KB |
Output is correct |
50 |
Correct |
11 ms |
45916 KB |
Output is correct |
51 |
Correct |
11 ms |
45660 KB |
Output is correct |
52 |
Correct |
10 ms |
43900 KB |
Output is correct |
53 |
Correct |
11 ms |
43744 KB |
Output is correct |
54 |
Correct |
13 ms |
45660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
43356 KB |
Output is correct |
2 |
Correct |
9 ms |
43356 KB |
Output is correct |
3 |
Correct |
10 ms |
43468 KB |
Output is correct |
4 |
Correct |
9 ms |
43472 KB |
Output is correct |
5 |
Correct |
10 ms |
43356 KB |
Output is correct |
6 |
Correct |
9 ms |
43356 KB |
Output is correct |
7 |
Correct |
8 ms |
41304 KB |
Output is correct |
8 |
Correct |
9 ms |
43356 KB |
Output is correct |
9 |
Correct |
9 ms |
43352 KB |
Output is correct |
10 |
Correct |
9 ms |
43356 KB |
Output is correct |
11 |
Correct |
9 ms |
43568 KB |
Output is correct |
12 |
Correct |
9 ms |
43352 KB |
Output is correct |
13 |
Correct |
9 ms |
43356 KB |
Output is correct |
14 |
Correct |
9 ms |
43356 KB |
Output is correct |
15 |
Correct |
9 ms |
43352 KB |
Output is correct |
16 |
Correct |
10 ms |
43544 KB |
Output is correct |
17 |
Correct |
10 ms |
43356 KB |
Output is correct |
18 |
Correct |
9 ms |
43468 KB |
Output is correct |
19 |
Correct |
9 ms |
43356 KB |
Output is correct |
20 |
Correct |
9 ms |
43352 KB |
Output is correct |
21 |
Correct |
9 ms |
43356 KB |
Output is correct |
22 |
Correct |
9 ms |
43356 KB |
Output is correct |
23 |
Correct |
9 ms |
43356 KB |
Output is correct |
24 |
Correct |
9 ms |
43560 KB |
Output is correct |
25 |
Correct |
9 ms |
43356 KB |
Output is correct |
26 |
Correct |
51 ms |
57036 KB |
Output is correct |
27 |
Correct |
65 ms |
56520 KB |
Output is correct |
28 |
Correct |
10 ms |
45656 KB |
Output is correct |
29 |
Correct |
9 ms |
43352 KB |
Output is correct |
30 |
Correct |
9 ms |
43356 KB |
Output is correct |
31 |
Correct |
68 ms |
56496 KB |
Output is correct |
32 |
Correct |
10 ms |
45600 KB |
Output is correct |
33 |
Correct |
95 ms |
66540 KB |
Output is correct |
34 |
Correct |
80 ms |
56632 KB |
Output is correct |
35 |
Correct |
12 ms |
43868 KB |
Output is correct |
36 |
Correct |
77 ms |
57700 KB |
Output is correct |
37 |
Correct |
10 ms |
43864 KB |
Output is correct |
38 |
Correct |
11 ms |
45788 KB |
Output is correct |
39 |
Correct |
54 ms |
56788 KB |
Output is correct |
40 |
Correct |
11 ms |
45916 KB |
Output is correct |
41 |
Correct |
64 ms |
56776 KB |
Output is correct |
42 |
Correct |
100 ms |
59816 KB |
Output is correct |
43 |
Correct |
9 ms |
43352 KB |
Output is correct |
44 |
Correct |
89 ms |
67272 KB |
Output is correct |
45 |
Correct |
85 ms |
61840 KB |
Output is correct |
46 |
Correct |
10 ms |
43868 KB |
Output is correct |
47 |
Correct |
11 ms |
45660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
55604 KB |
Output is correct |
2 |
Correct |
75 ms |
61508 KB |
Output is correct |
3 |
Correct |
11 ms |
43868 KB |
Output is correct |
4 |
Correct |
11 ms |
45660 KB |
Output is correct |
5 |
Correct |
11 ms |
43356 KB |
Output is correct |
6 |
Correct |
9 ms |
43356 KB |
Output is correct |
7 |
Correct |
11 ms |
45660 KB |
Output is correct |
8 |
Correct |
93 ms |
58328 KB |
Output is correct |
9 |
Correct |
10 ms |
43868 KB |
Output is correct |
10 |
Correct |
68 ms |
56772 KB |
Output is correct |
11 |
Correct |
9 ms |
43356 KB |
Output is correct |
12 |
Correct |
72 ms |
56728 KB |
Output is correct |
13 |
Correct |
93 ms |
58568 KB |
Output is correct |
14 |
Correct |
98 ms |
62748 KB |
Output is correct |
15 |
Correct |
56 ms |
56824 KB |
Output is correct |
16 |
Correct |
11 ms |
45868 KB |
Output is correct |
17 |
Correct |
9 ms |
43356 KB |
Output is correct |
18 |
Correct |
77 ms |
62268 KB |
Output is correct |
19 |
Correct |
139 ms |
68020 KB |
Output is correct |
20 |
Correct |
10 ms |
43864 KB |
Output is correct |
21 |
Correct |
10 ms |
43356 KB |
Output is correct |
22 |
Correct |
66 ms |
58736 KB |
Output is correct |
23 |
Correct |
11 ms |
43864 KB |
Output is correct |
24 |
Correct |
77 ms |
57332 KB |
Output is correct |
25 |
Correct |
122 ms |
66244 KB |
Output is correct |
26 |
Correct |
11 ms |
43868 KB |
Output is correct |
27 |
Correct |
11 ms |
45944 KB |
Output is correct |
28 |
Correct |
11 ms |
45660 KB |
Output is correct |
29 |
Correct |
10 ms |
43868 KB |
Output is correct |
30 |
Correct |
10 ms |
43864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
43356 KB |
Output is correct |
2 |
Correct |
9 ms |
43356 KB |
Output is correct |
3 |
Correct |
10 ms |
43468 KB |
Output is correct |
4 |
Correct |
9 ms |
43472 KB |
Output is correct |
5 |
Correct |
10 ms |
43356 KB |
Output is correct |
6 |
Correct |
9 ms |
43356 KB |
Output is correct |
7 |
Correct |
8 ms |
41304 KB |
Output is correct |
8 |
Correct |
9 ms |
43356 KB |
Output is correct |
9 |
Correct |
9 ms |
43352 KB |
Output is correct |
10 |
Correct |
9 ms |
43356 KB |
Output is correct |
11 |
Correct |
9 ms |
43568 KB |
Output is correct |
12 |
Correct |
9 ms |
43352 KB |
Output is correct |
13 |
Correct |
9 ms |
43356 KB |
Output is correct |
14 |
Correct |
9 ms |
43356 KB |
Output is correct |
15 |
Correct |
9 ms |
43352 KB |
Output is correct |
16 |
Correct |
10 ms |
43544 KB |
Output is correct |
17 |
Correct |
10 ms |
43356 KB |
Output is correct |
18 |
Correct |
9 ms |
43468 KB |
Output is correct |
19 |
Correct |
9 ms |
43356 KB |
Output is correct |
20 |
Correct |
9 ms |
43352 KB |
Output is correct |
21 |
Correct |
9 ms |
43356 KB |
Output is correct |
22 |
Correct |
9 ms |
43356 KB |
Output is correct |
23 |
Correct |
9 ms |
43356 KB |
Output is correct |
24 |
Correct |
9 ms |
43560 KB |
Output is correct |
25 |
Correct |
9 ms |
43352 KB |
Output is correct |
26 |
Correct |
11 ms |
43868 KB |
Output is correct |
27 |
Correct |
12 ms |
45660 KB |
Output is correct |
28 |
Correct |
11 ms |
44124 KB |
Output is correct |
29 |
Correct |
11 ms |
43868 KB |
Output is correct |
30 |
Correct |
11 ms |
43896 KB |
Output is correct |
31 |
Correct |
10 ms |
43496 KB |
Output is correct |
32 |
Correct |
11 ms |
44120 KB |
Output is correct |
33 |
Correct |
9 ms |
43352 KB |
Output is correct |
34 |
Correct |
12 ms |
45660 KB |
Output is correct |
35 |
Correct |
11 ms |
43868 KB |
Output is correct |
36 |
Correct |
11 ms |
43868 KB |
Output is correct |
37 |
Correct |
12 ms |
43868 KB |
Output is correct |
38 |
Correct |
9 ms |
43352 KB |
Output is correct |
39 |
Correct |
11 ms |
45660 KB |
Output is correct |
40 |
Correct |
10 ms |
43868 KB |
Output is correct |
41 |
Correct |
11 ms |
45660 KB |
Output is correct |
42 |
Correct |
12 ms |
45660 KB |
Output is correct |
43 |
Correct |
11 ms |
46052 KB |
Output is correct |
44 |
Correct |
10 ms |
43356 KB |
Output is correct |
45 |
Correct |
11 ms |
45780 KB |
Output is correct |
46 |
Correct |
11 ms |
43736 KB |
Output is correct |
47 |
Correct |
9 ms |
43356 KB |
Output is correct |
48 |
Correct |
11 ms |
43868 KB |
Output is correct |
49 |
Correct |
11 ms |
43864 KB |
Output is correct |
50 |
Correct |
11 ms |
45916 KB |
Output is correct |
51 |
Correct |
11 ms |
45660 KB |
Output is correct |
52 |
Correct |
10 ms |
43900 KB |
Output is correct |
53 |
Correct |
11 ms |
43744 KB |
Output is correct |
54 |
Correct |
13 ms |
45660 KB |
Output is correct |
55 |
Correct |
9 ms |
43356 KB |
Output is correct |
56 |
Correct |
51 ms |
57036 KB |
Output is correct |
57 |
Correct |
65 ms |
56520 KB |
Output is correct |
58 |
Correct |
10 ms |
45656 KB |
Output is correct |
59 |
Correct |
9 ms |
43352 KB |
Output is correct |
60 |
Correct |
9 ms |
43356 KB |
Output is correct |
61 |
Correct |
68 ms |
56496 KB |
Output is correct |
62 |
Correct |
10 ms |
45600 KB |
Output is correct |
63 |
Correct |
95 ms |
66540 KB |
Output is correct |
64 |
Correct |
80 ms |
56632 KB |
Output is correct |
65 |
Correct |
12 ms |
43868 KB |
Output is correct |
66 |
Correct |
77 ms |
57700 KB |
Output is correct |
67 |
Correct |
10 ms |
43864 KB |
Output is correct |
68 |
Correct |
11 ms |
45788 KB |
Output is correct |
69 |
Correct |
54 ms |
56788 KB |
Output is correct |
70 |
Correct |
11 ms |
45916 KB |
Output is correct |
71 |
Correct |
64 ms |
56776 KB |
Output is correct |
72 |
Correct |
100 ms |
59816 KB |
Output is correct |
73 |
Correct |
9 ms |
43352 KB |
Output is correct |
74 |
Correct |
89 ms |
67272 KB |
Output is correct |
75 |
Correct |
85 ms |
61840 KB |
Output is correct |
76 |
Correct |
10 ms |
43868 KB |
Output is correct |
77 |
Correct |
11 ms |
45660 KB |
Output is correct |
78 |
Correct |
61 ms |
55604 KB |
Output is correct |
79 |
Correct |
75 ms |
61508 KB |
Output is correct |
80 |
Correct |
11 ms |
43868 KB |
Output is correct |
81 |
Correct |
11 ms |
45660 KB |
Output is correct |
82 |
Correct |
11 ms |
43356 KB |
Output is correct |
83 |
Correct |
9 ms |
43356 KB |
Output is correct |
84 |
Correct |
11 ms |
45660 KB |
Output is correct |
85 |
Correct |
93 ms |
58328 KB |
Output is correct |
86 |
Correct |
10 ms |
43868 KB |
Output is correct |
87 |
Correct |
68 ms |
56772 KB |
Output is correct |
88 |
Correct |
9 ms |
43356 KB |
Output is correct |
89 |
Correct |
72 ms |
56728 KB |
Output is correct |
90 |
Correct |
93 ms |
58568 KB |
Output is correct |
91 |
Correct |
98 ms |
62748 KB |
Output is correct |
92 |
Correct |
56 ms |
56824 KB |
Output is correct |
93 |
Correct |
11 ms |
45868 KB |
Output is correct |
94 |
Correct |
9 ms |
43356 KB |
Output is correct |
95 |
Correct |
77 ms |
62268 KB |
Output is correct |
96 |
Correct |
139 ms |
68020 KB |
Output is correct |
97 |
Correct |
10 ms |
43864 KB |
Output is correct |
98 |
Correct |
10 ms |
43356 KB |
Output is correct |
99 |
Correct |
66 ms |
58736 KB |
Output is correct |
100 |
Correct |
11 ms |
43864 KB |
Output is correct |
101 |
Correct |
77 ms |
57332 KB |
Output is correct |
102 |
Correct |
122 ms |
66244 KB |
Output is correct |
103 |
Correct |
11 ms |
43868 KB |
Output is correct |
104 |
Correct |
11 ms |
45944 KB |
Output is correct |
105 |
Correct |
11 ms |
45660 KB |
Output is correct |
106 |
Correct |
10 ms |
43868 KB |
Output is correct |
107 |
Correct |
10 ms |
43864 KB |
Output is correct |
108 |
Correct |
459 ms |
123816 KB |
Output is correct |
109 |
Correct |
774 ms |
141260 KB |
Output is correct |
110 |
Correct |
798 ms |
157376 KB |
Output is correct |
111 |
Correct |
983 ms |
177256 KB |
Output is correct |
112 |
Correct |
822 ms |
158284 KB |
Output is correct |
113 |
Correct |
541 ms |
144284 KB |
Output is correct |
114 |
Correct |
546 ms |
115740 KB |
Output is correct |
115 |
Correct |
546 ms |
116084 KB |
Output is correct |
116 |
Correct |
673 ms |
119000 KB |
Output is correct |
117 |
Correct |
829 ms |
146296 KB |
Output is correct |
118 |
Correct |
565 ms |
115652 KB |
Output is correct |
119 |
Correct |
796 ms |
146436 KB |
Output is correct |
120 |
Correct |
990 ms |
166000 KB |
Output is correct |
121 |
Correct |
760 ms |
146360 KB |
Output is correct |
122 |
Correct |
697 ms |
126900 KB |
Output is correct |
123 |
Correct |
525 ms |
149356 KB |
Output is correct |
124 |
Correct |
815 ms |
139188 KB |
Output is correct |