#include <bits/stdc++.h>
#define gibon ios::sync_with_stdio(false); cin.tie(0);
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define pdd pair<long double, long double>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pvv pair<vector<int>, vector<int>>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
typedef long long ll;
using namespace std;
const int mxN=200005;
const int mxM=10000010;
const int lgN=20;
const ll MOD=1000000007;
const ll INF=1e11;
int dx[4]={1, 0, -1, 0}, dy[4]={0, 1, 0, -1};
int ddx[8]={1, 1, 0, -1, -1, -1, 0, 1}, ddy[8]={0, -1, -1, -1, 0, 1, 1, 1};
int N, M;
int C[mxN];
vector <int> v[mxN];
int r1, r2;
int dep[mxN], sub[mxN], par[mxN][lgN];
multiset <pii> s[mxN];
bool pr2[mxN]; ///parent of r2
pii pnt[mxN];
int ans[mxN];
void dfs1(int now, int pre=-1)
{
sub[now]=0;
for(int nxt : v[now]) if(nxt!=pre)
{
dep[nxt]=dep[now]+1;
par[nxt][0]=now;
dfs1(nxt, now);
sub[now]=max(sub[nxt]+1, sub[now]);
}
}
void make_sps()
{
for(int i=1;i<lgN;i++) for(int j=1;j<=N;j++) par[j][i]=par[par[j][i-1]][i-1];
}
int lca(int a, int b)
{
if(dep[a]<dep[b]) swap(a, b);
for(int i=lgN-1;i>=0;i--) if(dep[a]-(1<<i)>=dep[b]) a=par[a][i];
if(a==b) return a;
for(int i=lgN-1;i>=0;i--) if(par[a][i]!=par[b][i]) a=par[a][i], b=par[b][i];
return par[a][0];
}
int dis(int a, int b)
{
int c=lca(a, b);
return dep[a]+dep[b]-2*dep[c];
}
int mov(int s, int e, int val)
{
int c=lca(s, e);
if(val<=dep[s]-dep[c])
{
for(int i=lgN-1;i>=0;i--) if(val&(1<<i)) s=par[s][i];
return s;
}
else return mov(e, s, dep[s]+dep[e]-2*dep[c]-val);
}
void make_s()
{
int now=r2;
while(now) pr2[now]=true, now=par[now][0];
for(int i=1;i<=N;i++)
{
for(int nxt : v[i]) if(dep[nxt]>dep[i])
{
s[i].insert(pii(sub[nxt]+1, nxt));
int udep=dep[i];
if(!pr2[nxt]) udep=max(udep, dis(i, r2));
s[nxt].insert(pii(udep+1, i));
}
}
}
struct qry{
pii typ; ///ci:(0, idx), ct: (1, idx), idx: (2, 0)
int f, t;
pii pf, pt;
qry(): typ(), f(), t(), pf(), pt() {}
qry(int a, int b, int f, int t): typ(a, b), f(f), t(t), pf(), pt() {}
qry (int a, int b, pii pf, pii pt) : typ(a, b), f(), t(), pf(pf), pt(pt) {}
};
pii ct[mxN]; ///dep, col
int idx;
vector <qry> ht;
int ci[mxN];
void dfs2(int now, int pre, int nd, int root)
{
//printf("now=%d, pre=%d, nd=%d\n", now, pre, nd);
if(dis(now, root)>dis(now, r1+r2-root) || (dis(now, root)==dis(now, r1+r2-root) && root<r1+r2-root))
{
if(v[now].size()==1) ans[now]+=idx; ///leaf or root
else
{
int ns=0;
auto it=s[now].rbegin();
if(it->se!=pre) ns=it->fi;
else
{
it++;
ns=it->fi;
}
//printf("ns=%d\n", ns);
ans[now]+=lower_bound(ct, ct+idx, pii(nd-ns, -1))-ct;
}
}
//for(int i=0;i<idx;i++) printf("(%d, %d) ", ct[i].fi, ct[i].se);
//printf("\n");
for(int nxt : v[now]) if(nxt!=pre)
{
int cnt1=0, cnt2=0;
int nc=C[now];
if(v[now].size()>=3) ///root의 degree=1이라서 괜찮다
{
int ns=0;
auto it=s[now].rbegin();
while(it->se==pre || it->se==nxt) it++;
ns=it->fi;
//printf("now=%d, nxt=%d, ns=%d\n", now, nxt, ns);
int ni=lower_bound(ct, ct+idx, pii(nd-ns, -1))-ct;
ht.emplace_back(2, 0, idx, ni);
cnt2++;
idx=ni;
}
if(ci[nc]==-1 || ci[nc]>=idx || ct[ci[nc]].se!=nc)
{
ht.emplace_back(0, nc, ci[nc], idx);
ht.emplace_back(1, idx, ct[idx], pii(nd, nc));
ht.emplace_back(2, 0, idx, idx+1);
cnt1+=3;
ci[nc]=idx;
ct[idx]=pii(nd, nc);
idx++;
}
dfs2(nxt, now, nd+1, root);
if(cnt1)
{
idx=ht.back().f;
ht.pop_back();
ct[idx]=ht.back().pf;
ht.pop_back();
ci[nc]=ht.back().f;
ht.pop_back();
}
if(cnt2)
{
idx=ht.back().f;
ht.pop_back();
}
//printf("now=%d, nxt=%d\n", now, nxt);
//for(int i=0;i<idx;i++) printf("(%d, %d) ", ct[i].fi, ct[i].se);
//printf("\n");
}
}
void init()
{
for(int i=1;i<=M;i++) ci[i]=-1;
idx=0;
}
int main()
{
gibon
cin >> N >> M;
for(int i=1;i<N;i++)
{
int a, b;
cin >> a >> b;
v[b].push_back(a);
v[a].push_back(b);
}
for(int i=1;i<=N;i++) cin >> C[i];
dfs1(1);
r1=1;
for(int i=1;i<=N;i++) if(dep[i]>dep[r1]) r1=i;
dep[r1]=0;
par[r1][0]=0;
dfs1(r1);
r2=r1;
for(int i=1;i<=N;i++) if(dep[i]>dep[r2]) r2=i;
make_sps();
make_s();
/*for(int i=1;i<=N;i++)
{
printf("%d: ", i);
for(auto [x, y] : s[i]) printf("(%d, %d) ", x, y);
printf("\n");
}*/
init();
dfs2(r1, -1, 0, r1);
//for(int i=1;i<=N;i++) printf("ans[%d]=%d\n", i, ans[i]);
init();
dfs2(r2, -1, 0, r2);
for(int i=1;i<=N;i++) cout << ans[i] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
9 ms |
14852 KB |
Output is correct |
3 |
Correct |
10 ms |
14956 KB |
Output is correct |
4 |
Correct |
10 ms |
15188 KB |
Output is correct |
5 |
Correct |
10 ms |
14880 KB |
Output is correct |
6 |
Correct |
12 ms |
15208 KB |
Output is correct |
7 |
Correct |
11 ms |
15060 KB |
Output is correct |
8 |
Correct |
11 ms |
14828 KB |
Output is correct |
9 |
Correct |
10 ms |
14932 KB |
Output is correct |
10 |
Correct |
10 ms |
14932 KB |
Output is correct |
11 |
Correct |
10 ms |
14932 KB |
Output is correct |
12 |
Correct |
10 ms |
14936 KB |
Output is correct |
13 |
Correct |
9 ms |
15208 KB |
Output is correct |
14 |
Correct |
11 ms |
15012 KB |
Output is correct |
15 |
Correct |
10 ms |
14972 KB |
Output is correct |
16 |
Correct |
10 ms |
14872 KB |
Output is correct |
17 |
Correct |
11 ms |
15188 KB |
Output is correct |
18 |
Correct |
9 ms |
15060 KB |
Output is correct |
19 |
Correct |
10 ms |
14884 KB |
Output is correct |
20 |
Correct |
10 ms |
15464 KB |
Output is correct |
21 |
Correct |
10 ms |
15340 KB |
Output is correct |
22 |
Correct |
9 ms |
14828 KB |
Output is correct |
23 |
Correct |
10 ms |
15096 KB |
Output is correct |
24 |
Correct |
11 ms |
14932 KB |
Output is correct |
25 |
Correct |
10 ms |
14932 KB |
Output is correct |
26 |
Correct |
9 ms |
14932 KB |
Output is correct |
27 |
Correct |
12 ms |
15472 KB |
Output is correct |
28 |
Correct |
10 ms |
15336 KB |
Output is correct |
29 |
Correct |
10 ms |
15240 KB |
Output is correct |
30 |
Correct |
11 ms |
14920 KB |
Output is correct |
31 |
Correct |
10 ms |
15188 KB |
Output is correct |
32 |
Correct |
10 ms |
15276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
268 ms |
41752 KB |
Output is correct |
2 |
Correct |
344 ms |
64960 KB |
Output is correct |
3 |
Correct |
58 ms |
24008 KB |
Output is correct |
4 |
Correct |
516 ms |
61984 KB |
Output is correct |
5 |
Correct |
640 ms |
102444 KB |
Output is correct |
6 |
Correct |
724 ms |
82200 KB |
Output is correct |
7 |
Correct |
562 ms |
61860 KB |
Output is correct |
8 |
Correct |
594 ms |
66732 KB |
Output is correct |
9 |
Correct |
683 ms |
65096 KB |
Output is correct |
10 |
Correct |
676 ms |
64996 KB |
Output is correct |
11 |
Correct |
481 ms |
62424 KB |
Output is correct |
12 |
Correct |
725 ms |
88744 KB |
Output is correct |
13 |
Correct |
623 ms |
82584 KB |
Output is correct |
14 |
Correct |
683 ms |
80904 KB |
Output is correct |
15 |
Correct |
456 ms |
62168 KB |
Output is correct |
16 |
Correct |
564 ms |
89304 KB |
Output is correct |
17 |
Correct |
713 ms |
82280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
522 ms |
53264 KB |
Output is correct |
2 |
Correct |
796 ms |
130744 KB |
Output is correct |
3 |
Correct |
86 ms |
27112 KB |
Output is correct |
4 |
Correct |
614 ms |
63680 KB |
Output is correct |
5 |
Correct |
798 ms |
132800 KB |
Output is correct |
6 |
Correct |
891 ms |
97704 KB |
Output is correct |
7 |
Correct |
573 ms |
63520 KB |
Output is correct |
8 |
Correct |
665 ms |
78704 KB |
Output is correct |
9 |
Correct |
704 ms |
71984 KB |
Output is correct |
10 |
Correct |
646 ms |
68576 KB |
Output is correct |
11 |
Correct |
598 ms |
63684 KB |
Output is correct |
12 |
Correct |
818 ms |
113704 KB |
Output is correct |
13 |
Correct |
721 ms |
96012 KB |
Output is correct |
14 |
Correct |
796 ms |
96312 KB |
Output is correct |
15 |
Correct |
451 ms |
64108 KB |
Output is correct |
16 |
Correct |
648 ms |
105052 KB |
Output is correct |
17 |
Correct |
699 ms |
98020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
9 ms |
14852 KB |
Output is correct |
3 |
Correct |
10 ms |
14956 KB |
Output is correct |
4 |
Correct |
10 ms |
15188 KB |
Output is correct |
5 |
Correct |
10 ms |
14880 KB |
Output is correct |
6 |
Correct |
12 ms |
15208 KB |
Output is correct |
7 |
Correct |
11 ms |
15060 KB |
Output is correct |
8 |
Correct |
11 ms |
14828 KB |
Output is correct |
9 |
Correct |
10 ms |
14932 KB |
Output is correct |
10 |
Correct |
10 ms |
14932 KB |
Output is correct |
11 |
Correct |
10 ms |
14932 KB |
Output is correct |
12 |
Correct |
10 ms |
14936 KB |
Output is correct |
13 |
Correct |
9 ms |
15208 KB |
Output is correct |
14 |
Correct |
11 ms |
15012 KB |
Output is correct |
15 |
Correct |
10 ms |
14972 KB |
Output is correct |
16 |
Correct |
10 ms |
14872 KB |
Output is correct |
17 |
Correct |
11 ms |
15188 KB |
Output is correct |
18 |
Correct |
9 ms |
15060 KB |
Output is correct |
19 |
Correct |
10 ms |
14884 KB |
Output is correct |
20 |
Correct |
10 ms |
15464 KB |
Output is correct |
21 |
Correct |
10 ms |
15340 KB |
Output is correct |
22 |
Correct |
9 ms |
14828 KB |
Output is correct |
23 |
Correct |
10 ms |
15096 KB |
Output is correct |
24 |
Correct |
11 ms |
14932 KB |
Output is correct |
25 |
Correct |
10 ms |
14932 KB |
Output is correct |
26 |
Correct |
9 ms |
14932 KB |
Output is correct |
27 |
Correct |
12 ms |
15472 KB |
Output is correct |
28 |
Correct |
10 ms |
15336 KB |
Output is correct |
29 |
Correct |
10 ms |
15240 KB |
Output is correct |
30 |
Correct |
11 ms |
14920 KB |
Output is correct |
31 |
Correct |
10 ms |
15188 KB |
Output is correct |
32 |
Correct |
10 ms |
15276 KB |
Output is correct |
33 |
Correct |
268 ms |
41752 KB |
Output is correct |
34 |
Correct |
344 ms |
64960 KB |
Output is correct |
35 |
Correct |
58 ms |
24008 KB |
Output is correct |
36 |
Correct |
516 ms |
61984 KB |
Output is correct |
37 |
Correct |
640 ms |
102444 KB |
Output is correct |
38 |
Correct |
724 ms |
82200 KB |
Output is correct |
39 |
Correct |
562 ms |
61860 KB |
Output is correct |
40 |
Correct |
594 ms |
66732 KB |
Output is correct |
41 |
Correct |
683 ms |
65096 KB |
Output is correct |
42 |
Correct |
676 ms |
64996 KB |
Output is correct |
43 |
Correct |
481 ms |
62424 KB |
Output is correct |
44 |
Correct |
725 ms |
88744 KB |
Output is correct |
45 |
Correct |
623 ms |
82584 KB |
Output is correct |
46 |
Correct |
683 ms |
80904 KB |
Output is correct |
47 |
Correct |
456 ms |
62168 KB |
Output is correct |
48 |
Correct |
564 ms |
89304 KB |
Output is correct |
49 |
Correct |
713 ms |
82280 KB |
Output is correct |
50 |
Correct |
522 ms |
53264 KB |
Output is correct |
51 |
Correct |
796 ms |
130744 KB |
Output is correct |
52 |
Correct |
86 ms |
27112 KB |
Output is correct |
53 |
Correct |
614 ms |
63680 KB |
Output is correct |
54 |
Correct |
798 ms |
132800 KB |
Output is correct |
55 |
Correct |
891 ms |
97704 KB |
Output is correct |
56 |
Correct |
573 ms |
63520 KB |
Output is correct |
57 |
Correct |
665 ms |
78704 KB |
Output is correct |
58 |
Correct |
704 ms |
71984 KB |
Output is correct |
59 |
Correct |
646 ms |
68576 KB |
Output is correct |
60 |
Correct |
598 ms |
63684 KB |
Output is correct |
61 |
Correct |
818 ms |
113704 KB |
Output is correct |
62 |
Correct |
721 ms |
96012 KB |
Output is correct |
63 |
Correct |
796 ms |
96312 KB |
Output is correct |
64 |
Correct |
451 ms |
64108 KB |
Output is correct |
65 |
Correct |
648 ms |
105052 KB |
Output is correct |
66 |
Correct |
699 ms |
98020 KB |
Output is correct |
67 |
Correct |
58 ms |
20932 KB |
Output is correct |
68 |
Correct |
254 ms |
56316 KB |
Output is correct |
69 |
Correct |
395 ms |
60208 KB |
Output is correct |
70 |
Correct |
563 ms |
62028 KB |
Output is correct |
71 |
Correct |
665 ms |
102456 KB |
Output is correct |
72 |
Correct |
843 ms |
82076 KB |
Output is correct |
73 |
Correct |
559 ms |
61908 KB |
Output is correct |
74 |
Correct |
666 ms |
70708 KB |
Output is correct |
75 |
Correct |
666 ms |
66028 KB |
Output is correct |
76 |
Correct |
638 ms |
65456 KB |
Output is correct |
77 |
Correct |
514 ms |
61964 KB |
Output is correct |
78 |
Correct |
675 ms |
92864 KB |
Output is correct |
79 |
Correct |
623 ms |
86908 KB |
Output is correct |
80 |
Correct |
699 ms |
78896 KB |
Output is correct |
81 |
Correct |
439 ms |
62060 KB |
Output is correct |
82 |
Correct |
591 ms |
89136 KB |
Output is correct |
83 |
Correct |
668 ms |
82228 KB |
Output is correct |
84 |
Correct |
564 ms |
62284 KB |
Output is correct |
85 |
Correct |
674 ms |
103052 KB |
Output is correct |
86 |
Correct |
735 ms |
82744 KB |
Output is correct |
87 |
Correct |
542 ms |
62276 KB |
Output is correct |
88 |
Correct |
633 ms |
71656 KB |
Output is correct |
89 |
Correct |
625 ms |
68052 KB |
Output is correct |
90 |
Correct |
618 ms |
66716 KB |
Output is correct |
91 |
Correct |
487 ms |
62368 KB |
Output is correct |
92 |
Correct |
683 ms |
102208 KB |
Output is correct |
93 |
Correct |
641 ms |
77248 KB |
Output is correct |
94 |
Correct |
666 ms |
73508 KB |
Output is correct |
95 |
Correct |
449 ms |
62700 KB |
Output is correct |
96 |
Correct |
580 ms |
89716 KB |
Output is correct |
97 |
Correct |
671 ms |
82920 KB |
Output is correct |
98 |
Correct |
559 ms |
63700 KB |
Output is correct |
99 |
Correct |
681 ms |
104012 KB |
Output is correct |
100 |
Correct |
765 ms |
92080 KB |
Output is correct |
101 |
Correct |
532 ms |
62692 KB |
Output is correct |
102 |
Correct |
631 ms |
72884 KB |
Output is correct |
103 |
Correct |
619 ms |
70204 KB |
Output is correct |
104 |
Correct |
636 ms |
67892 KB |
Output is correct |
105 |
Correct |
478 ms |
62856 KB |
Output is correct |
106 |
Correct |
706 ms |
99264 KB |
Output is correct |
107 |
Correct |
603 ms |
88820 KB |
Output is correct |
108 |
Correct |
698 ms |
82104 KB |
Output is correct |
109 |
Correct |
440 ms |
63620 KB |
Output is correct |
110 |
Correct |
642 ms |
93528 KB |
Output is correct |
111 |
Correct |
696 ms |
91272 KB |
Output is correct |