# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
236454 |
2020-06-02T10:10:19 Z |
Charis02 |
Mergers (JOI19_mergers) |
C++14 |
|
190 ms |
262148 KB |
#include<iostream>
#include<stdio.h>
#include<vector>
#include<cmath>
#include<queue>
#include<string.h>
#include<map>
#include<set>
#include<algorithm>
#define ll long long
#define pi pair < ll,ll >
#define mp(a,b) make_pair(a,b)
#define mid (low+high)/2
#define rep(i,a,b) for(int i = a;i < b;i++)
#define N 1000004
using namespace std;
struct dsu{
ll par[N];
ll sz[N];
ll col[N];
ll get(ll x)
{
return (par[x] == x) ? x : get(par[x]);
}
void join(ll x,ll y)
{
ll fx=get(x);
ll fy = get(y);
if(fx==fy)
return;
if(sz[fx]<sz[fy])
swap(fx,fy);
sz[fx]+=sz[fy];
par[fy]=fx;
}
ll get_col(ll x)
{
return col[get(x)];
}
};
struct segment_tree{
ll val[4*N];
void upd(ll low,ll high,ll pos,ll slow,ll v)
{
if(low==high&&low==slow)
{
val[pos]+=v;
return;
}
if(low>slow||high<slow)
return;
upd(low,mid,pos*2+1,slow,v);
upd(mid+1,high,pos*2+2,slow,v);
val[pos]=val[pos*2+1]+val[pos*2+2];
return;
}
ll query(ll low,ll high,ll pos,ll slow,ll shigh)
{
if(low>=slow&&high<=shigh)
return val[pos];
if(low>shigh||high<slow)
return 0;
return query(low,mid,pos*2+1,slow,shigh)+query(mid+1,high,pos*2+2,slow,shigh);
}
};
ll n,k,answer;
vector < ll > graph[N];
vector < ll > cities[N];
vector < ll > queries[N];
ll cnt,colors;
ll ar[N],deg[N];
ll dp[N][20],tin[N],tout[N];
segment_tree seg;
dsu comp;
ll get_par(ll x,ll i)
{
if(dp[x][i] != -1)
return dp[x][i];
return dp[x][i] = get_par(get_par(x,i-1),i-1);
}
bool is_ancestor(ll x,ll y)
{
return (tin[x] <= tin[y] && tout[x] >= tout[y]);
}
ll get_lca(ll x,ll y)
{
if(is_ancestor(x,y))
return x;
if(is_ancestor(y,x))
return y;
for(int i = 19;i >= 0;i--)
{
if(!is_ancestor(get_par(x,i),y))
x = get_par(x,i);
}
return get_par(x,0);
}
void init(ll cur,ll par)
{
dp[cur][0] = par;
tin[cur] = cnt++;
rep(i,0,graph[cur].size())
{
ll v = graph[cur][i];
if(v==par)
continue;
init(v,cur);
}
tout[cur]=cnt++;
return;
}
void dfs(ll cur,ll par)
{
rep(i,0,queries[cur].size())
seg.upd(0,2*n,0,tin[queries[cur][i]],1);
rep(i,0,graph[cur].size())
{
ll v= graph[cur][i];
if(v==par)
continue;
if(seg.query(0,2*n,0,tin[v],tout[v])!=0)
comp.join(cur,v);
dfs(v,cur);
}
rep(i,0,queries[cur].size())
seg.upd(0,2*n,0,tin[queries[cur][i]],-1);
}
void process_paths()
{
rep(s,1,k+1)
{
ll x = cities[s][0];
rep(j,1,cities[s].size())
{
ll y = cities[s][j];
ll lca = get_lca(x,y);
if(lca==x)
queries[x].push_back(y);
else if(lca==y)
queries[y].push_back(x);
else
{
queries[lca].push_back(x);
queries[lca].push_back(y);
}
}
}
dfs(1,1);
rep(i,1,n+1)
{
if(comp.get_col(i) == 0)
comp.col[comp.get(i)] = ++colors;
ar[i] = comp.get_col(i);
}
return;
}
void calc()
{
rep(i,1,n+1)
{
rep(j,0,graph[i].size())
{
ll v = graph[i][j];
if(ar[i] != ar[v])
deg[ar[i]]++;
}
}
ll leaves = 0;
rep(i,1,colors+1)
{
if(deg[i] == 1)
leaves++;
}
answer = (leaves+1)/2;
return;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
rep(i,0,n-1)
{
ll a,b;
cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
rep(i,1,n+1)
{
ll s;
comp.par[i]=i;
comp.col[i] = 0;
comp.sz[i]=1;
cin >> s;
cities[s].push_back(i);
}
memset(seg.val,0,sizeof seg.val);
memset(dp,-1,sizeof dp);
init(1,1);
process_paths();
calc();
cout << answer << endl;
return 0;
}
/*
5 4
1 2
2 3
3 4
4 5
1
2
3
4
1
3 2
2 1
3 1
2
2
1
5 2
1 2
4 5
4 2
3 4
2
1
2
1
2
*/
Compilation message
mergers.cpp: In function 'void init(long long int, long long int)':
mergers.cpp:14:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define rep(i,a,b) for(int i = a;i < b;i++)
mergers.cpp:118:9:
rep(i,0,graph[cur].size())
~~~~~~~~~~~~~~~~~~~~~
mergers.cpp:118:5: note: in expansion of macro 'rep'
rep(i,0,graph[cur].size())
^~~
mergers.cpp: In function 'void dfs(long long int, long long int)':
mergers.cpp:14:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define rep(i,a,b) for(int i = a;i < b;i++)
mergers.cpp:132:9:
rep(i,0,queries[cur].size())
~~~~~~~~~~~~~~~~~~~~~~~
mergers.cpp:132:5: note: in expansion of macro 'rep'
rep(i,0,queries[cur].size())
^~~
mergers.cpp:14:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define rep(i,a,b) for(int i = a;i < b;i++)
mergers.cpp:135:9:
rep(i,0,graph[cur].size())
~~~~~~~~~~~~~~~~~~~~~
mergers.cpp:135:5: note: in expansion of macro 'rep'
rep(i,0,graph[cur].size())
^~~
mergers.cpp:14:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define rep(i,a,b) for(int i = a;i < b;i++)
mergers.cpp:147:9:
rep(i,0,queries[cur].size())
~~~~~~~~~~~~~~~~~~~~~~~
mergers.cpp:147:5: note: in expansion of macro 'rep'
rep(i,0,queries[cur].size())
^~~
mergers.cpp: In function 'void process_paths()':
mergers.cpp:14:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define rep(i,a,b) for(int i = a;i < b;i++)
mergers.cpp:157:13:
rep(j,1,cities[s].size())
~~~~~~~~~~~~~~~~~~~~
mergers.cpp:157:9: note: in expansion of macro 'rep'
rep(j,1,cities[s].size())
^~~
mergers.cpp: In function 'void calc()':
mergers.cpp:14:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define rep(i,a,b) for(int i = a;i < b;i++)
mergers.cpp:190:13:
rep(j,0,graph[i].size())
~~~~~~~~~~~~~~~~~~~
mergers.cpp:190:9: note: in expansion of macro 'rep'
rep(j,0,graph[i].size())
^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
138 ms |
258680 KB |
Output is correct |
2 |
Correct |
132 ms |
258680 KB |
Output is correct |
3 |
Correct |
135 ms |
258680 KB |
Output is correct |
4 |
Correct |
147 ms |
258684 KB |
Output is correct |
5 |
Correct |
134 ms |
258808 KB |
Output is correct |
6 |
Correct |
134 ms |
258680 KB |
Output is correct |
7 |
Correct |
142 ms |
258680 KB |
Output is correct |
8 |
Correct |
141 ms |
258808 KB |
Output is correct |
9 |
Correct |
137 ms |
258680 KB |
Output is correct |
10 |
Correct |
142 ms |
258784 KB |
Output is correct |
11 |
Correct |
142 ms |
258680 KB |
Output is correct |
12 |
Correct |
137 ms |
258680 KB |
Output is correct |
13 |
Correct |
150 ms |
258808 KB |
Output is correct |
14 |
Correct |
141 ms |
258680 KB |
Output is correct |
15 |
Correct |
137 ms |
258680 KB |
Output is correct |
16 |
Correct |
149 ms |
258664 KB |
Output is correct |
17 |
Correct |
147 ms |
258680 KB |
Output is correct |
18 |
Correct |
145 ms |
258740 KB |
Output is correct |
19 |
Correct |
140 ms |
258680 KB |
Output is correct |
20 |
Correct |
139 ms |
258680 KB |
Output is correct |
21 |
Correct |
147 ms |
258936 KB |
Output is correct |
22 |
Correct |
141 ms |
258808 KB |
Output is correct |
23 |
Correct |
143 ms |
258680 KB |
Output is correct |
24 |
Correct |
149 ms |
258680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
138 ms |
258680 KB |
Output is correct |
2 |
Correct |
132 ms |
258680 KB |
Output is correct |
3 |
Correct |
135 ms |
258680 KB |
Output is correct |
4 |
Correct |
147 ms |
258684 KB |
Output is correct |
5 |
Correct |
134 ms |
258808 KB |
Output is correct |
6 |
Correct |
134 ms |
258680 KB |
Output is correct |
7 |
Correct |
142 ms |
258680 KB |
Output is correct |
8 |
Correct |
141 ms |
258808 KB |
Output is correct |
9 |
Correct |
137 ms |
258680 KB |
Output is correct |
10 |
Correct |
142 ms |
258784 KB |
Output is correct |
11 |
Correct |
142 ms |
258680 KB |
Output is correct |
12 |
Correct |
137 ms |
258680 KB |
Output is correct |
13 |
Correct |
150 ms |
258808 KB |
Output is correct |
14 |
Correct |
141 ms |
258680 KB |
Output is correct |
15 |
Correct |
137 ms |
258680 KB |
Output is correct |
16 |
Correct |
149 ms |
258664 KB |
Output is correct |
17 |
Correct |
147 ms |
258680 KB |
Output is correct |
18 |
Correct |
145 ms |
258740 KB |
Output is correct |
19 |
Correct |
140 ms |
258680 KB |
Output is correct |
20 |
Correct |
139 ms |
258680 KB |
Output is correct |
21 |
Correct |
147 ms |
258936 KB |
Output is correct |
22 |
Correct |
141 ms |
258808 KB |
Output is correct |
23 |
Correct |
143 ms |
258680 KB |
Output is correct |
24 |
Correct |
149 ms |
258680 KB |
Output is correct |
25 |
Correct |
147 ms |
258724 KB |
Output is correct |
26 |
Correct |
146 ms |
259064 KB |
Output is correct |
27 |
Correct |
150 ms |
259148 KB |
Output is correct |
28 |
Correct |
142 ms |
259320 KB |
Output is correct |
29 |
Correct |
144 ms |
259060 KB |
Output is correct |
30 |
Correct |
145 ms |
259068 KB |
Output is correct |
31 |
Correct |
134 ms |
258680 KB |
Output is correct |
32 |
Correct |
150 ms |
259320 KB |
Output is correct |
33 |
Correct |
144 ms |
258808 KB |
Output is correct |
34 |
Correct |
136 ms |
259192 KB |
Output is correct |
35 |
Correct |
142 ms |
259064 KB |
Output is correct |
36 |
Correct |
145 ms |
259064 KB |
Output is correct |
37 |
Correct |
151 ms |
259064 KB |
Output is correct |
38 |
Correct |
144 ms |
258808 KB |
Output is correct |
39 |
Correct |
137 ms |
259064 KB |
Output is correct |
40 |
Correct |
136 ms |
259064 KB |
Output is correct |
41 |
Correct |
142 ms |
259168 KB |
Output is correct |
42 |
Correct |
148 ms |
259064 KB |
Output is correct |
43 |
Correct |
139 ms |
259448 KB |
Output is correct |
44 |
Correct |
134 ms |
258680 KB |
Output is correct |
45 |
Correct |
139 ms |
259192 KB |
Output is correct |
46 |
Correct |
142 ms |
259140 KB |
Output is correct |
47 |
Correct |
136 ms |
258808 KB |
Output is correct |
48 |
Correct |
152 ms |
259132 KB |
Output is correct |
49 |
Correct |
138 ms |
259132 KB |
Output is correct |
50 |
Correct |
144 ms |
259192 KB |
Output is correct |
51 |
Correct |
139 ms |
259120 KB |
Output is correct |
52 |
Correct |
143 ms |
259204 KB |
Output is correct |
53 |
Correct |
163 ms |
259064 KB |
Output is correct |
54 |
Correct |
146 ms |
259064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
138 ms |
258680 KB |
Output is correct |
2 |
Correct |
132 ms |
258680 KB |
Output is correct |
3 |
Correct |
135 ms |
258680 KB |
Output is correct |
4 |
Correct |
147 ms |
258684 KB |
Output is correct |
5 |
Correct |
134 ms |
258808 KB |
Output is correct |
6 |
Correct |
134 ms |
258680 KB |
Output is correct |
7 |
Correct |
142 ms |
258680 KB |
Output is correct |
8 |
Correct |
141 ms |
258808 KB |
Output is correct |
9 |
Correct |
137 ms |
258680 KB |
Output is correct |
10 |
Correct |
142 ms |
258784 KB |
Output is correct |
11 |
Correct |
142 ms |
258680 KB |
Output is correct |
12 |
Correct |
137 ms |
258680 KB |
Output is correct |
13 |
Correct |
150 ms |
258808 KB |
Output is correct |
14 |
Correct |
141 ms |
258680 KB |
Output is correct |
15 |
Correct |
137 ms |
258680 KB |
Output is correct |
16 |
Correct |
149 ms |
258664 KB |
Output is correct |
17 |
Correct |
147 ms |
258680 KB |
Output is correct |
18 |
Correct |
145 ms |
258740 KB |
Output is correct |
19 |
Correct |
140 ms |
258680 KB |
Output is correct |
20 |
Correct |
139 ms |
258680 KB |
Output is correct |
21 |
Correct |
147 ms |
258936 KB |
Output is correct |
22 |
Correct |
141 ms |
258808 KB |
Output is correct |
23 |
Correct |
143 ms |
258680 KB |
Output is correct |
24 |
Correct |
149 ms |
258680 KB |
Output is correct |
25 |
Correct |
136 ms |
258680 KB |
Output is correct |
26 |
Runtime error |
190 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
27 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
175 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
138 ms |
258680 KB |
Output is correct |
2 |
Correct |
132 ms |
258680 KB |
Output is correct |
3 |
Correct |
135 ms |
258680 KB |
Output is correct |
4 |
Correct |
147 ms |
258684 KB |
Output is correct |
5 |
Correct |
134 ms |
258808 KB |
Output is correct |
6 |
Correct |
134 ms |
258680 KB |
Output is correct |
7 |
Correct |
142 ms |
258680 KB |
Output is correct |
8 |
Correct |
141 ms |
258808 KB |
Output is correct |
9 |
Correct |
137 ms |
258680 KB |
Output is correct |
10 |
Correct |
142 ms |
258784 KB |
Output is correct |
11 |
Correct |
142 ms |
258680 KB |
Output is correct |
12 |
Correct |
137 ms |
258680 KB |
Output is correct |
13 |
Correct |
150 ms |
258808 KB |
Output is correct |
14 |
Correct |
141 ms |
258680 KB |
Output is correct |
15 |
Correct |
137 ms |
258680 KB |
Output is correct |
16 |
Correct |
149 ms |
258664 KB |
Output is correct |
17 |
Correct |
147 ms |
258680 KB |
Output is correct |
18 |
Correct |
145 ms |
258740 KB |
Output is correct |
19 |
Correct |
140 ms |
258680 KB |
Output is correct |
20 |
Correct |
139 ms |
258680 KB |
Output is correct |
21 |
Correct |
147 ms |
258936 KB |
Output is correct |
22 |
Correct |
141 ms |
258808 KB |
Output is correct |
23 |
Correct |
143 ms |
258680 KB |
Output is correct |
24 |
Correct |
149 ms |
258680 KB |
Output is correct |
25 |
Correct |
147 ms |
258724 KB |
Output is correct |
26 |
Correct |
146 ms |
259064 KB |
Output is correct |
27 |
Correct |
150 ms |
259148 KB |
Output is correct |
28 |
Correct |
142 ms |
259320 KB |
Output is correct |
29 |
Correct |
144 ms |
259060 KB |
Output is correct |
30 |
Correct |
145 ms |
259068 KB |
Output is correct |
31 |
Correct |
134 ms |
258680 KB |
Output is correct |
32 |
Correct |
150 ms |
259320 KB |
Output is correct |
33 |
Correct |
144 ms |
258808 KB |
Output is correct |
34 |
Correct |
136 ms |
259192 KB |
Output is correct |
35 |
Correct |
142 ms |
259064 KB |
Output is correct |
36 |
Correct |
145 ms |
259064 KB |
Output is correct |
37 |
Correct |
151 ms |
259064 KB |
Output is correct |
38 |
Correct |
144 ms |
258808 KB |
Output is correct |
39 |
Correct |
137 ms |
259064 KB |
Output is correct |
40 |
Correct |
136 ms |
259064 KB |
Output is correct |
41 |
Correct |
142 ms |
259168 KB |
Output is correct |
42 |
Correct |
148 ms |
259064 KB |
Output is correct |
43 |
Correct |
139 ms |
259448 KB |
Output is correct |
44 |
Correct |
134 ms |
258680 KB |
Output is correct |
45 |
Correct |
139 ms |
259192 KB |
Output is correct |
46 |
Correct |
142 ms |
259140 KB |
Output is correct |
47 |
Correct |
136 ms |
258808 KB |
Output is correct |
48 |
Correct |
152 ms |
259132 KB |
Output is correct |
49 |
Correct |
138 ms |
259132 KB |
Output is correct |
50 |
Correct |
144 ms |
259192 KB |
Output is correct |
51 |
Correct |
139 ms |
259120 KB |
Output is correct |
52 |
Correct |
143 ms |
259204 KB |
Output is correct |
53 |
Correct |
163 ms |
259064 KB |
Output is correct |
54 |
Correct |
146 ms |
259064 KB |
Output is correct |
55 |
Correct |
136 ms |
258680 KB |
Output is correct |
56 |
Runtime error |
190 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
57 |
Halted |
0 ms |
0 KB |
- |