답안 #519085

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
519085 2022-01-25T15:44:28 Z Koosha_mv Unique Cities (JOI19_ho_t5) C++14
100 / 100
717 ms 55732 KB
#include <bits/stdc++.h>
using namespace std;
#define dbgv(v) cout<<#v<<" = "; f(i,0,v.size()) cout<<v[i]<<" "; cout<<endl
#define dbga(a,x,y) cout<<#a<<" = "; f(i,x,y) cout<<a[i]<<" "; cout<<endl
#define erorp(x) cout<<#x<<"={"<<(x.F)<<" , "<<x.S<<"}"<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define all(v) v.begin(),v.end()
#define bit(n,k) (((n)>>(k))&1)
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first

const int N=2e5+99;

int n,m,d,res,cent,s1,s2,a[N],ans[N],dp[N],pd[N],mark[N],fenwik[N];
vector<int> v,g[N];

void add(int x, int val){ for(x++;x<n;x+=x&-x) fenwik[x]+=val; }
int get(int x) { int res=0; for (x++;x>0;x-=x&-x) res+=fenwik[x]; return res; }

void Add(int x){
	if(++mark[x]==1) res++;
}
void del(int x){
	if(--mark[x]==0) res--;
}
void dfs1(int u,int p,int dist){
	if(dist>=d){
		d=dist;
		cent=u;
	}
	for(auto v : g[u]){
		if(v==p) continue ;
		dfs1(v,u,dist+1);
	}
}
void find_cent(){
	cent=d=0;
	dfs1(1,0,0);
	s1=cent;
	cent=d=0;
	dfs1(s1,0,0);
	s2=cent;
}
void dfs2(int u,int p){
	dp[u]=0;
	for(auto v : g[u]){
		if(v==p) continue ;
		dfs2(v,u);
		maxm(dp[u],dp[v]+1);
	}
}
void dfs3(int u,int p){
	set<pair<int,int> > s;
	for(auto v : g[u]){
		if(v==p) continue ;
		s.insert(mp(dp[v],v));
	}
	for(auto v : g[u]){
		if(v==p) continue ;
		s.erase(mp(dp[v],v));
		pd[v]=0;
		if(s.size()){
			maxm(pd[v],(*s.rbegin()).F+1);
		}
		dfs3(v,u);
		s.insert(mp(dp[v],v));
	}
}
void dfs4(int u,int p,int d){
	int x=-1,y=-1;
	v.pb(u);
	if(pd[u]>0){
		add(max(0,d-pd[u]-1),1);
		add(d-1,-1);
	}
	if(d-dp[u]-2>=0 && get(d-dp[u]-2)==0){
		x=a[v[d-dp[u]-2]];
		Add(x);
	}
	if(d-dp[u]-1>=0 && get(d-dp[u]-1)==0){
		y=a[v[d-dp[u]-1]];
		Add(y);
	}
	maxm(ans[u],res);
	for(auto v : g[u]){
		if(v==p) continue ;
		dfs4(v,u,d+1);
	}
	if(x!=-1){
		del(x);
	}
	if(y!=-1){
		del(y);
	}
	if(pd[u]>0){
		add(max(0,d-pd[u]-1),-1);
		add(d-1,1);
	}
	v.pop_back();
}
void solve(int u){
	fill(mark,mark+N,0);
	fill(fenwik,fenwik+N,0);
	v.clear();
	pd[u]=res=0;
	
	dfs2(u,0);
	dfs3(u,0);
	/*f(i,1,n+1){
		cout<<i<<" : "<<dp[i]<<" "<<pd[i]<<endl;
	}*/
	dfs4(u,0,0);
	
}
	
int main(){
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin>>n>>m;
	f(i,1,n){
		int u,v;
		cin>>u>>v;
		g[u].pb(v);
		g[v].pb(u);
	}
	f(i,1,n+1) cin>>a[i];
	find_cent();
	//cout<<s1<<" "<<s2<<endl;
	solve(s1);
	solve(s2);
	f(i,1,n+1) cout<<ans[i]<<" ";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6480 KB Output is correct
2 Correct 8 ms 6604 KB Output is correct
3 Correct 4 ms 6732 KB Output is correct
4 Correct 5 ms 6780 KB Output is correct
5 Correct 7 ms 6604 KB Output is correct
6 Correct 6 ms 6988 KB Output is correct
7 Correct 5 ms 6860 KB Output is correct
8 Correct 6 ms 6604 KB Output is correct
9 Correct 6 ms 6732 KB Output is correct
10 Correct 7 ms 6732 KB Output is correct
11 Correct 5 ms 6732 KB Output is correct
12 Correct 7 ms 6732 KB Output is correct
13 Correct 5 ms 6988 KB Output is correct
14 Correct 6 ms 6860 KB Output is correct
15 Correct 6 ms 6860 KB Output is correct
16 Correct 6 ms 6732 KB Output is correct
17 Correct 6 ms 6988 KB Output is correct
18 Correct 5 ms 6912 KB Output is correct
19 Correct 5 ms 6704 KB Output is correct
20 Correct 5 ms 6988 KB Output is correct
21 Correct 5 ms 6860 KB Output is correct
22 Correct 6 ms 6604 KB Output is correct
23 Correct 6 ms 6732 KB Output is correct
24 Correct 6 ms 6668 KB Output is correct
25 Correct 6 ms 6616 KB Output is correct
26 Correct 6 ms 6788 KB Output is correct
27 Correct 5 ms 6988 KB Output is correct
28 Correct 6 ms 6860 KB Output is correct
29 Correct 6 ms 6844 KB Output is correct
30 Correct 7 ms 6732 KB Output is correct
31 Correct 6 ms 6988 KB Output is correct
32 Correct 5 ms 6860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 206 ms 12336 KB Output is correct
2 Correct 225 ms 34280 KB Output is correct
3 Correct 32 ms 10744 KB Output is correct
4 Correct 383 ms 16476 KB Output is correct
5 Correct 488 ms 54848 KB Output is correct
6 Correct 436 ms 35760 KB Output is correct
7 Correct 412 ms 18368 KB Output is correct
8 Correct 434 ms 21788 KB Output is correct
9 Correct 462 ms 20376 KB Output is correct
10 Correct 418 ms 19992 KB Output is correct
11 Correct 708 ms 25936 KB Output is correct
12 Correct 506 ms 42816 KB Output is correct
13 Correct 520 ms 38784 KB Output is correct
14 Correct 505 ms 36740 KB Output is correct
15 Correct 717 ms 26536 KB Output is correct
16 Correct 500 ms 45716 KB Output is correct
17 Correct 494 ms 38176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 301 ms 14436 KB Output is correct
2 Correct 451 ms 53516 KB Output is correct
3 Correct 37 ms 11232 KB Output is correct
4 Correct 414 ms 16556 KB Output is correct
5 Correct 505 ms 55732 KB Output is correct
6 Correct 460 ms 35952 KB Output is correct
7 Correct 432 ms 17988 KB Output is correct
8 Correct 433 ms 26384 KB Output is correct
9 Correct 440 ms 23668 KB Output is correct
10 Correct 443 ms 20556 KB Output is correct
11 Correct 542 ms 21440 KB Output is correct
12 Correct 497 ms 49500 KB Output is correct
13 Correct 559 ms 36872 KB Output is correct
14 Correct 500 ms 36260 KB Output is correct
15 Correct 697 ms 26552 KB Output is correct
16 Correct 492 ms 45884 KB Output is correct
17 Correct 530 ms 38380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6480 KB Output is correct
2 Correct 8 ms 6604 KB Output is correct
3 Correct 4 ms 6732 KB Output is correct
4 Correct 5 ms 6780 KB Output is correct
5 Correct 7 ms 6604 KB Output is correct
6 Correct 6 ms 6988 KB Output is correct
7 Correct 5 ms 6860 KB Output is correct
8 Correct 6 ms 6604 KB Output is correct
9 Correct 6 ms 6732 KB Output is correct
10 Correct 7 ms 6732 KB Output is correct
11 Correct 5 ms 6732 KB Output is correct
12 Correct 7 ms 6732 KB Output is correct
13 Correct 5 ms 6988 KB Output is correct
14 Correct 6 ms 6860 KB Output is correct
15 Correct 6 ms 6860 KB Output is correct
16 Correct 6 ms 6732 KB Output is correct
17 Correct 6 ms 6988 KB Output is correct
18 Correct 5 ms 6912 KB Output is correct
19 Correct 5 ms 6704 KB Output is correct
20 Correct 5 ms 6988 KB Output is correct
21 Correct 5 ms 6860 KB Output is correct
22 Correct 6 ms 6604 KB Output is correct
23 Correct 6 ms 6732 KB Output is correct
24 Correct 6 ms 6668 KB Output is correct
25 Correct 6 ms 6616 KB Output is correct
26 Correct 6 ms 6788 KB Output is correct
27 Correct 5 ms 6988 KB Output is correct
28 Correct 6 ms 6860 KB Output is correct
29 Correct 6 ms 6844 KB Output is correct
30 Correct 7 ms 6732 KB Output is correct
31 Correct 6 ms 6988 KB Output is correct
32 Correct 5 ms 6860 KB Output is correct
33 Correct 206 ms 12336 KB Output is correct
34 Correct 225 ms 34280 KB Output is correct
35 Correct 32 ms 10744 KB Output is correct
36 Correct 383 ms 16476 KB Output is correct
37 Correct 488 ms 54848 KB Output is correct
38 Correct 436 ms 35760 KB Output is correct
39 Correct 412 ms 18368 KB Output is correct
40 Correct 434 ms 21788 KB Output is correct
41 Correct 462 ms 20376 KB Output is correct
42 Correct 418 ms 19992 KB Output is correct
43 Correct 708 ms 25936 KB Output is correct
44 Correct 506 ms 42816 KB Output is correct
45 Correct 520 ms 38784 KB Output is correct
46 Correct 505 ms 36740 KB Output is correct
47 Correct 717 ms 26536 KB Output is correct
48 Correct 500 ms 45716 KB Output is correct
49 Correct 494 ms 38176 KB Output is correct
50 Correct 301 ms 14436 KB Output is correct
51 Correct 451 ms 53516 KB Output is correct
52 Correct 37 ms 11232 KB Output is correct
53 Correct 414 ms 16556 KB Output is correct
54 Correct 505 ms 55732 KB Output is correct
55 Correct 460 ms 35952 KB Output is correct
56 Correct 432 ms 17988 KB Output is correct
57 Correct 433 ms 26384 KB Output is correct
58 Correct 440 ms 23668 KB Output is correct
59 Correct 443 ms 20556 KB Output is correct
60 Correct 542 ms 21440 KB Output is correct
61 Correct 497 ms 49500 KB Output is correct
62 Correct 559 ms 36872 KB Output is correct
63 Correct 500 ms 36260 KB Output is correct
64 Correct 697 ms 26552 KB Output is correct
65 Correct 492 ms 45884 KB Output is correct
66 Correct 530 ms 38380 KB Output is correct
67 Correct 38 ms 7940 KB Output is correct
68 Correct 140 ms 27836 KB Output is correct
69 Correct 220 ms 24832 KB Output is correct
70 Correct 459 ms 16744 KB Output is correct
71 Correct 465 ms 54736 KB Output is correct
72 Correct 448 ms 35684 KB Output is correct
73 Correct 465 ms 17864 KB Output is correct
74 Correct 513 ms 25984 KB Output is correct
75 Correct 435 ms 21532 KB Output is correct
76 Correct 452 ms 20540 KB Output is correct
77 Correct 555 ms 22276 KB Output is correct
78 Correct 482 ms 46440 KB Output is correct
79 Correct 493 ms 43136 KB Output is correct
80 Correct 519 ms 34780 KB Output is correct
81 Correct 682 ms 26532 KB Output is correct
82 Correct 492 ms 45304 KB Output is correct
83 Correct 476 ms 38160 KB Output is correct
84 Correct 397 ms 16576 KB Output is correct
85 Correct 494 ms 55320 KB Output is correct
86 Correct 481 ms 35804 KB Output is correct
87 Correct 454 ms 17728 KB Output is correct
88 Correct 488 ms 26684 KB Output is correct
89 Correct 452 ms 21984 KB Output is correct
90 Correct 446 ms 20832 KB Output is correct
91 Correct 658 ms 22168 KB Output is correct
92 Correct 464 ms 53992 KB Output is correct
93 Correct 515 ms 33216 KB Output is correct
94 Correct 478 ms 28928 KB Output is correct
95 Correct 683 ms 26544 KB Output is correct
96 Correct 564 ms 46044 KB Output is correct
97 Correct 488 ms 38456 KB Output is correct
98 Correct 404 ms 16584 KB Output is correct
99 Correct 480 ms 55368 KB Output is correct
100 Correct 448 ms 35932 KB Output is correct
101 Correct 436 ms 18408 KB Output is correct
102 Correct 453 ms 24448 KB Output is correct
103 Correct 425 ms 21828 KB Output is correct
104 Correct 457 ms 20796 KB Output is correct
105 Correct 654 ms 22780 KB Output is correct
106 Correct 497 ms 41040 KB Output is correct
107 Correct 557 ms 42776 KB Output is correct
108 Correct 486 ms 30720 KB Output is correct
109 Correct 693 ms 26604 KB Output is correct
110 Correct 512 ms 46396 KB Output is correct
111 Correct 484 ms 38408 KB Output is correct