# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
828637 |
2023-08-17T13:00:59 Z |
Ronin13 |
Mergers (JOI19_mergers) |
C++17 |
|
101 ms |
26708 KB |
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define f first
#define s second
#define pii pair<ll,ll>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
using namespace std;
const int nmax = 2e5 + 1;
int in[nmax], out[nmax];
int timer = 0;
int p[nmax][22];
vector <vector <int> > g(nmax);
void dfs(int v, int e){
in[v] = timer++;
p[v][0] = e;
for(int j = 1; j <= 20; j++)
p[v][j] = p[p[v][j - 1]][j - 1];
for(int to : g[v]){
if(to == e) continue;
dfs(to, v);
}
out[v] = timer++;
}
bool isancestor(int u, int v){
return in[u] <= in[v] && out[u] >= out[v];
}
int lca(int u, int v){
if(isancestor(u, v)) return u;
if(isancestor(v, u)) return v;
for(int j = 20; j >= 0; j--){
if(!isancestor(p[u][j], v)) u = p[u][j];
}
return p[u][0];
}
vector <int> sz(nmax);
vector <int> a(nmax);
void DFS(int v, int e){
sz[v] = 1;
for(int to : g[v]){
if(to == e) continue;
DFS(to, v);
sz[v] += sz[to];
}
sz[v] -= a[v];
}
int ansdfs(int v, int e){
int sum = 0;
for(int to : g[v]){
if(to == e) continue;
sum += ansdfs(to, v);
}
if(sz[v] || v == 1) return sum;
else{
return max(sum, 1);
}
}
int main(){
// ios_base::sync_with_stdio(false); cin.tie(0);
int n; cin >> n;
int k; cin >> k;
vector <vector <int> > vec(k + 1);
for(int i = 1; i < n; i++){
int u, v; cin >> u >> v;
g[u].pb(v); g[v].pb(u);
}
dfs(1, 1);
for(int i = 1; i <= n; ++i){
int x; cin >> x;
vec[x].pb(i);
}
for(int i = 1; i <= k; i++){
if(vec[i].empty()) continue;
int lc = vec[i][0];
for(int to : vec[i]) lc = lca(lc, to);
a[lc] += vec[i].size();
}
DFS(1, 1);
cout << (ansdfs(1, 1) + 1) / 2 << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6612 KB |
Output is correct |
3 |
Correct |
3 ms |
6612 KB |
Output is correct |
4 |
Correct |
3 ms |
6588 KB |
Output is correct |
5 |
Correct |
3 ms |
6576 KB |
Output is correct |
6 |
Correct |
3 ms |
6484 KB |
Output is correct |
7 |
Correct |
3 ms |
6484 KB |
Output is correct |
8 |
Correct |
3 ms |
6584 KB |
Output is correct |
9 |
Correct |
3 ms |
6588 KB |
Output is correct |
10 |
Correct |
3 ms |
6588 KB |
Output is correct |
11 |
Correct |
3 ms |
6580 KB |
Output is correct |
12 |
Correct |
3 ms |
6612 KB |
Output is correct |
13 |
Correct |
3 ms |
6484 KB |
Output is correct |
14 |
Correct |
3 ms |
6484 KB |
Output is correct |
15 |
Correct |
3 ms |
6496 KB |
Output is correct |
16 |
Incorrect |
3 ms |
6484 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6612 KB |
Output is correct |
3 |
Correct |
3 ms |
6612 KB |
Output is correct |
4 |
Correct |
3 ms |
6588 KB |
Output is correct |
5 |
Correct |
3 ms |
6576 KB |
Output is correct |
6 |
Correct |
3 ms |
6484 KB |
Output is correct |
7 |
Correct |
3 ms |
6484 KB |
Output is correct |
8 |
Correct |
3 ms |
6584 KB |
Output is correct |
9 |
Correct |
3 ms |
6588 KB |
Output is correct |
10 |
Correct |
3 ms |
6588 KB |
Output is correct |
11 |
Correct |
3 ms |
6580 KB |
Output is correct |
12 |
Correct |
3 ms |
6612 KB |
Output is correct |
13 |
Correct |
3 ms |
6484 KB |
Output is correct |
14 |
Correct |
3 ms |
6484 KB |
Output is correct |
15 |
Correct |
3 ms |
6496 KB |
Output is correct |
16 |
Incorrect |
3 ms |
6484 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6612 KB |
Output is correct |
3 |
Correct |
3 ms |
6612 KB |
Output is correct |
4 |
Correct |
3 ms |
6588 KB |
Output is correct |
5 |
Correct |
3 ms |
6576 KB |
Output is correct |
6 |
Correct |
3 ms |
6484 KB |
Output is correct |
7 |
Correct |
3 ms |
6484 KB |
Output is correct |
8 |
Correct |
3 ms |
6584 KB |
Output is correct |
9 |
Correct |
3 ms |
6588 KB |
Output is correct |
10 |
Correct |
3 ms |
6588 KB |
Output is correct |
11 |
Correct |
3 ms |
6580 KB |
Output is correct |
12 |
Correct |
3 ms |
6612 KB |
Output is correct |
13 |
Correct |
3 ms |
6484 KB |
Output is correct |
14 |
Correct |
3 ms |
6484 KB |
Output is correct |
15 |
Correct |
3 ms |
6496 KB |
Output is correct |
16 |
Incorrect |
3 ms |
6484 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
87 ms |
21412 KB |
Output is correct |
2 |
Incorrect |
101 ms |
26708 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6612 KB |
Output is correct |
3 |
Correct |
3 ms |
6612 KB |
Output is correct |
4 |
Correct |
3 ms |
6588 KB |
Output is correct |
5 |
Correct |
3 ms |
6576 KB |
Output is correct |
6 |
Correct |
3 ms |
6484 KB |
Output is correct |
7 |
Correct |
3 ms |
6484 KB |
Output is correct |
8 |
Correct |
3 ms |
6584 KB |
Output is correct |
9 |
Correct |
3 ms |
6588 KB |
Output is correct |
10 |
Correct |
3 ms |
6588 KB |
Output is correct |
11 |
Correct |
3 ms |
6580 KB |
Output is correct |
12 |
Correct |
3 ms |
6612 KB |
Output is correct |
13 |
Correct |
3 ms |
6484 KB |
Output is correct |
14 |
Correct |
3 ms |
6484 KB |
Output is correct |
15 |
Correct |
3 ms |
6496 KB |
Output is correct |
16 |
Incorrect |
3 ms |
6484 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |