#include<bits/stdc++.h>
#define st first
#define nd second
#define pb push_back
#define ppb pop_back
#define ii pair<int,int>
#define ll long long
#define umin(x,y) x=min(x,y)
#define umax(x,y) x=max(x,y)
#define orta ((bas+son)>>1)
#define sz(x) ((int)x.size())
#define all(x) x.begin(),x.end()
#define inf 2000000000
#define N 200005
#define MOD 998244353
using namespace std;
int n,m;
int mxd[N][2],der[N];
int ans[N],c[N],deep[N];
multiset<int> s;
int tot_op;
int cntd;
int has[N];
int cnt[N];
vector<int> v[N];
struct diameter_finder {
int l,r;
int mx;
void dfs(int node,int ata,int len) {
if(len>mx) {
mx=len;
l=node;
}
for(int i:v[node]) {
if(i==ata) continue ;
dfs(i,node,len+1);
}
}
void find() {
mx=0;
dfs(1,0,0);
r=l;
mx=0;
dfs(r,0,0);
}
} dia;
void rollback(vector<int>& roll) {
while(!roll.empty()) {
int x=roll.back();
roll.ppb();
s.insert(x);
cntd+=(cnt[c[has[x]]]==0);
cnt[c[has[x]]]++;
}
}
void add(int node) {
has[der[node]]=node;
s.insert(der[node]);
cntd+=(cnt[c[node]]==0);
cnt[c[node]]++;
}
void del(vector<int>& roll,int cur) {
auto pos=s.lower_bound(cur);
while(pos!=s.end()) {
++tot_op;
roll.pb(*pos);
cnt[c[has[*pos]]]--;
cntd-=(cnt[c[has[*pos]]]==0);
s.erase(pos++);
}
}
void upd(int node,int nd) {
if(mxd[node][1]<nd) mxd[node][1]=nd;
if(mxd[node][1]>mxd[node][0]) swap(mxd[node][1],mxd[node][0]);
}
vector<int> temp;
void dfs(int node,int ata) {
// cerr << "node is : " << node <<'\n';
// for(auto x:s) cerr << x.st << ' ' << x.nd << '\n';
vector<int> roll;
// cerr << "roll upto : " << 2*der[node]-mxd[node][0] << "\n";
del(roll,2*der[node]-mxd[node][1]);
add(node);
for(int i:v[node]) {
if(i!=deep[node]) continue ;
dfs(i,node);
}
del(roll,2*der[node]-mxd[node][0]);
roll.ppb();
add(node);
for(int i:v[node]) {
if(i==ata || i==deep[node]) continue ;
dfs(i,node);
}
del(temp,der[node]);
umax(ans[node],cntd);
rollback(roll);
}
void dfs1(int node,int ata) {
der[node]=der[ata]+1;
mxd[node][0]=mxd[node][1]=der[node];
deep[node]=node;
for(int i:v[node]) {
if(i==ata) continue ;
dfs1(i,node);
upd(node,mxd[i][0]);
if(mxd[node][0]==mxd[i][0]) {
deep[node]=i;
}
}
}
int main() {
scanf("%d %d",&n,&m);
for(int i=1;i<n;i++) {
int x,y;
scanf("%d %d",&x,&y);
v[x].pb(y);
v[y].pb(x);
}
for(int i=1;i<=n;i++) scanf("%d",c+i);
dia.find();
// cerr << "root is : " << dia.l << '\n';
dfs1(dia.l,0);
dfs(dia.l,0);
// cerr << "root is : " << dia.r << '\n';
dfs1(dia.r,0);
dfs(dia.r,0);
for(int i=1;i<=n;i++) printf("%d\n",ans[i]);
// cerr << tot_op << '\n';
}
Compilation message
joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:199:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&n,&m);
~~~~~^~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:205:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&x,&y);
~~~~~^~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:212:29: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=1;i<=n;i++) scanf("%d",c+i);
~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
9 ms |
5240 KB |
Output is correct |
3 |
Correct |
74 ms |
5240 KB |
Output is correct |
4 |
Correct |
65 ms |
5240 KB |
Output is correct |
5 |
Correct |
9 ms |
5240 KB |
Output is correct |
6 |
Correct |
309 ms |
5540 KB |
Output is correct |
7 |
Correct |
76 ms |
5240 KB |
Output is correct |
8 |
Correct |
9 ms |
5112 KB |
Output is correct |
9 |
Correct |
9 ms |
5240 KB |
Output is correct |
10 |
Correct |
10 ms |
5240 KB |
Output is correct |
11 |
Correct |
9 ms |
5240 KB |
Output is correct |
12 |
Correct |
8 ms |
5240 KB |
Output is correct |
13 |
Correct |
273 ms |
5496 KB |
Output is correct |
14 |
Correct |
27 ms |
5240 KB |
Output is correct |
15 |
Correct |
27 ms |
5368 KB |
Output is correct |
16 |
Correct |
8 ms |
5240 KB |
Output is correct |
17 |
Correct |
141 ms |
5540 KB |
Output is correct |
18 |
Correct |
76 ms |
5368 KB |
Output is correct |
19 |
Correct |
9 ms |
5240 KB |
Output is correct |
20 |
Correct |
309 ms |
5556 KB |
Output is correct |
21 |
Correct |
76 ms |
5368 KB |
Output is correct |
22 |
Correct |
9 ms |
5240 KB |
Output is correct |
23 |
Correct |
9 ms |
5240 KB |
Output is correct |
24 |
Correct |
9 ms |
5240 KB |
Output is correct |
25 |
Correct |
10 ms |
5240 KB |
Output is correct |
26 |
Correct |
8 ms |
5240 KB |
Output is correct |
27 |
Correct |
140 ms |
5468 KB |
Output is correct |
28 |
Correct |
111 ms |
5496 KB |
Output is correct |
29 |
Correct |
45 ms |
5240 KB |
Output is correct |
30 |
Correct |
8 ms |
5240 KB |
Output is correct |
31 |
Correct |
143 ms |
5516 KB |
Output is correct |
32 |
Correct |
76 ms |
5368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
340 ms |
12724 KB |
Output is correct |
2 |
Execution timed out |
2027 ms |
29836 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
470 ms |
16748 KB |
Output is correct |
2 |
Execution timed out |
2072 ms |
47224 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
9 ms |
5240 KB |
Output is correct |
3 |
Correct |
74 ms |
5240 KB |
Output is correct |
4 |
Correct |
65 ms |
5240 KB |
Output is correct |
5 |
Correct |
9 ms |
5240 KB |
Output is correct |
6 |
Correct |
309 ms |
5540 KB |
Output is correct |
7 |
Correct |
76 ms |
5240 KB |
Output is correct |
8 |
Correct |
9 ms |
5112 KB |
Output is correct |
9 |
Correct |
9 ms |
5240 KB |
Output is correct |
10 |
Correct |
10 ms |
5240 KB |
Output is correct |
11 |
Correct |
9 ms |
5240 KB |
Output is correct |
12 |
Correct |
8 ms |
5240 KB |
Output is correct |
13 |
Correct |
273 ms |
5496 KB |
Output is correct |
14 |
Correct |
27 ms |
5240 KB |
Output is correct |
15 |
Correct |
27 ms |
5368 KB |
Output is correct |
16 |
Correct |
8 ms |
5240 KB |
Output is correct |
17 |
Correct |
141 ms |
5540 KB |
Output is correct |
18 |
Correct |
76 ms |
5368 KB |
Output is correct |
19 |
Correct |
9 ms |
5240 KB |
Output is correct |
20 |
Correct |
309 ms |
5556 KB |
Output is correct |
21 |
Correct |
76 ms |
5368 KB |
Output is correct |
22 |
Correct |
9 ms |
5240 KB |
Output is correct |
23 |
Correct |
9 ms |
5240 KB |
Output is correct |
24 |
Correct |
9 ms |
5240 KB |
Output is correct |
25 |
Correct |
10 ms |
5240 KB |
Output is correct |
26 |
Correct |
8 ms |
5240 KB |
Output is correct |
27 |
Correct |
140 ms |
5468 KB |
Output is correct |
28 |
Correct |
111 ms |
5496 KB |
Output is correct |
29 |
Correct |
45 ms |
5240 KB |
Output is correct |
30 |
Correct |
8 ms |
5240 KB |
Output is correct |
31 |
Correct |
143 ms |
5516 KB |
Output is correct |
32 |
Correct |
76 ms |
5368 KB |
Output is correct |
33 |
Correct |
340 ms |
12724 KB |
Output is correct |
34 |
Execution timed out |
2027 ms |
29836 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |