# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
623134 |
2022-08-05T08:48:21 Z |
radal |
Mergers (JOI19_mergers) |
C++17 |
|
136 ms |
39488 KB |
#include <bits/stdc++.h>
#pragma GCC target("sse,sse2,avx2")
#pragma GCC optimize("unroll-loops,O2")
#define rep(i,l,r) for (int i = l; i < r; i++)
#define repr(i,r,l) for (int i = r; i >= l; i--)
#define X first
#define Y second
#define all(x) (x).begin() , (x).end()
#define pb push_back
#define endl '\n'
#define debug(x) cerr << #x << " : " << x << endl;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pll;
constexpr int N = 5e5+10,mod = 998244353,inf = 1e9+10,sq = 700;
inline int mkay(int a,int b){
if (a+b >= mod) return a+b-mod;
if (a+b < 0) return a+b+mod;
return a+b;
}
inline int poww(int a,int k){
if (k < 0) return 0;
int z = 1;
while (k){
if (k&1) z = 1ll*z*a%mod;
a = 1ll*a*a%mod;
k /= 2;
}
return z;
}
vector<int> adj[N],col[N];
int par[N][20],h[N],T,tin[N],calc[N],dp[N];
int odd[N];
void pre(int v,int p){
par[v][0] = p;
tin[v] = T++;
for (int u : adj[v]){
if (u == p) continue;
h[u] = h[v]+1;
pre(u,v);
}
}
bool cmp(int u,int v){
return (tin[u] < tin[v]);
}
int lca(int u,int v){
if (h[u] < h[v]) swap(u,v);
repr(i,19,0){
if ((1 << i) <= h[u]-h[v]) u = par[u][i];
}
if (u == v) return u;
repr(i,19,0){
if (par[u][i] != par[v][i]){
v = par[v][i];
u = par[u][i];
}
}
return par[v][0];
}
void dfs(int v,int p){
int t[4] = {0,0,0,0};
for (int u : adj[v]){
if (u != p){
dfs(u,v);
calc[v] += calc[u];
/*if (v == 1){
debug(u);
debug(calc[u]);
debug(dp[u]);
debug(odd[u]);
}*/
if (calc[u]){
if (odd[u]) t[1]++;
else t[3]++;
}
else{
if (odd[u]) t[0]++;
else t[2]++;
}
dp[v] += dp[u];
}
}
t[0] += t[1];
t[1] = 0;
if (t[0] == t[2]){
odd[v] = 0;
return;
}
if (t[0] < t[2]){
t[2] -= t[0];
dp[v] += (t[2]+1)/2;
odd[v] = t[2]%2;
return;
}
t[0] -= t[2];
odd[v] = t[0]%2;
dp[v] -= t[0]/2;
}
int main(){
ios :: sync_with_stdio(0); cin.tie(0);
int n,k;
cin >> n >> k;
rep(i,1,n){
int u,v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
rep(i,1,n+1){
int c;
cin >> c;
col[c].pb(i);
}
rep(i,1,k+1) sort(all(col[i]),cmp);
pre(1,0);
rep(j,1,20){
rep(i,2,n+1)
par[i][j] = par[par[i][j-1]][j-1];
}
rep(i,1,k+1){
int sz = col[i].size();
if (sz < 2) continue;
rep(j,1,sz){
calc[col[i][j]]++;
calc[col[i][j-1]]++;
calc[lca(col[i][j],col[i][j-1])] -= 2;
}
}
dfs(1,0);
cout << dp[1];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23768 KB |
Output is correct |
2 |
Correct |
13 ms |
23868 KB |
Output is correct |
3 |
Correct |
13 ms |
23868 KB |
Output is correct |
4 |
Correct |
11 ms |
23852 KB |
Output is correct |
5 |
Correct |
11 ms |
23724 KB |
Output is correct |
6 |
Correct |
11 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23784 KB |
Output is correct |
8 |
Correct |
15 ms |
23824 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
14 ms |
23764 KB |
Output is correct |
11 |
Correct |
14 ms |
23832 KB |
Output is correct |
12 |
Incorrect |
12 ms |
23764 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23768 KB |
Output is correct |
2 |
Correct |
13 ms |
23868 KB |
Output is correct |
3 |
Correct |
13 ms |
23868 KB |
Output is correct |
4 |
Correct |
11 ms |
23852 KB |
Output is correct |
5 |
Correct |
11 ms |
23724 KB |
Output is correct |
6 |
Correct |
11 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23784 KB |
Output is correct |
8 |
Correct |
15 ms |
23824 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
14 ms |
23764 KB |
Output is correct |
11 |
Correct |
14 ms |
23832 KB |
Output is correct |
12 |
Incorrect |
12 ms |
23764 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23768 KB |
Output is correct |
2 |
Correct |
13 ms |
23868 KB |
Output is correct |
3 |
Correct |
13 ms |
23868 KB |
Output is correct |
4 |
Correct |
11 ms |
23852 KB |
Output is correct |
5 |
Correct |
11 ms |
23724 KB |
Output is correct |
6 |
Correct |
11 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23784 KB |
Output is correct |
8 |
Correct |
15 ms |
23824 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
14 ms |
23764 KB |
Output is correct |
11 |
Correct |
14 ms |
23832 KB |
Output is correct |
12 |
Incorrect |
12 ms |
23764 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
82 ms |
37428 KB |
Output is correct |
2 |
Correct |
136 ms |
39488 KB |
Output is correct |
3 |
Incorrect |
14 ms |
24276 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23768 KB |
Output is correct |
2 |
Correct |
13 ms |
23868 KB |
Output is correct |
3 |
Correct |
13 ms |
23868 KB |
Output is correct |
4 |
Correct |
11 ms |
23852 KB |
Output is correct |
5 |
Correct |
11 ms |
23724 KB |
Output is correct |
6 |
Correct |
11 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23784 KB |
Output is correct |
8 |
Correct |
15 ms |
23824 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
14 ms |
23764 KB |
Output is correct |
11 |
Correct |
14 ms |
23832 KB |
Output is correct |
12 |
Incorrect |
12 ms |
23764 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |