Submission #802776

# Submission time Handle Problem Language Result Execution time Memory
802776 2023-08-02T14:18:04 Z MohamedAliSaidane Meetings 2 (JOI21_meetings2) C++14
100 / 100
1500 ms 94480 KB
#include<bits/stdc++.h>
using namespace std;
 
typedef pair<int,int> pii;
typedef vector<int> vi;
 
#define ff first
#define ss second
#define pb push_back
#define all(x) (x).begin(),(x).end()
const int nax = 2e5 + 5;
 
int n;
vi adj[nax];
int sz[20][nax], mark[nax], dis[20][nax], ans[nax];
struct node
{
	int maxi = 0;
	int maxi2 = 0;
	int sum = 0;
	int lazy1 = 0, lazy2 = 0;
};
node bss;
vector<node> st;
vi nodes[nax];
 
void mx(int p, int val)
{
	if(st[p].maxi <= val)
	{
		st[p].maxi2 = st[p].maxi;
		st[p].maxi = val;
	}
	else if(st[p].maxi2 < val)
	{
		st[p].maxi2 = val;
	}
	st[p].sum = max(st[p].sum, st[p].maxi + st[p].maxi2);
 
}
void mxlz(int p, int val)
{
	if(st[p].lazy1 <= val)
	{
		st[p].lazy2 = st[p].lazy1;
		st[p].lazy1 = val;
	}
	else if(st[p].lazy2 < val)
	{
		st[p].lazy2 = val;
	}
 
}
void propagate(int p, int l, int r)
{
	if(st[p].lazy1 != 0)
	{
		if(l != r)
		{
			mxlz(2 * p, st[p].lazy1);
			mxlz(2 * p, st[p].lazy2);
			mxlz(2 * p + 1, st[p].lazy1);
			mxlz(2 * p + 1, st[p].lazy2);
		}		
		mx(p, st[p].lazy1);
		mx(p, st[p].lazy2);
		st[p].lazy1 = st[p].lazy2 = 0;
	}
}
int query(int p, int l, int r, int i, int j)
{
	propagate(p, l, r);
	if(i > j)
		return 0;
	if(l >= i && r <= j)
	{
		//cout << p << ' ' << l << ' '<< r << ' ' << st[p].maxi << ' ' << st[p].maxi2 << '\n';
		return st[p].sum;
	}
	int m = (l + r)/2;
	return max(query(2 * p, l, m, i, min(j, m)),
				query(2 * p + 1, m + 1, r, max(i, m + 1), j));
}
void update(int p, int l, int r, int i, int j, int val)
{
	propagate(p, l, r);
	if(i > j)
		return ;
	if(l >= i && r <= j)
	{
		st[p].lazy1 = val;
		propagate(p, l, r);
		return ;
	}
	int m = (l + r)/2;
	update(2 * p, l, m, i, min(j, m), val);
	update(2 * p + 1, m + 1, r, max(i, m + 1), j, val);
	propagate(2 * p, l, m);
	propagate(2 * p + 1, m + 1, r);
	st[p].sum = max(st[p].sum, max(st[2 * p].sum, st[2 * p + 1].sum));
}
int getsz(int x, int d, int cur, int p = -1)
{
	sz[d][x] = 1;
	dis[d][x] = cur ;
	for(auto e: adj[x])
	{
		if(mark[e] || (e == p))
			continue;
		sz[d][x] += getsz(e, d, cur + 1, x);
	}
	return sz[d][x];
}
int get_centroid(int x, int d, int s, int p = -1)
{
	for(auto e: adj[x])
	{
		if(mark[e] || (e == p))
			continue;
		if(sz[d][e] > s/2)
			return get_centroid(e, d, s, x);
	}
	return x;
}
int centroid(int x, int d = 0)
{
	x = get_centroid(x, d, getsz(x, d, 0));
	getsz(x, d, 0);
	nodes[x].pb(x);
	mark[x] = 1;
	vi C;
	for(auto e: adj[x])
	{
		if(mark[e])
			continue;
		int c = centroid(e, d + 1);
		C.pb(c);
	}
	int k = 1;
	while(k < sz[d][x] + 1)
		k = (k << 1);
	st.assign(2 * k + 1, bss);
	for(auto c: C)
	{
		vector<pii> v;
		for(auto e: nodes[c])
		{
			nodes[x].pb(e);
			v.pb({dis[d][e], sz[d][e]});
			//cout << '\t' << x << ' ' << e << ' ' <<  dis[d][e] << ' ' << sz[d][e] << '\n';
		}
		nodes[c].clear();
		sort(all(v));
		reverse(all(v));
		int prev = 0;
		for(auto e: v)
		{
			if(e.ss <= prev)
				continue;
			update(1, 0, k - 1, prev + 1, e.ss, e.ff );
			prev = e.ss;
			//cout << '\t'<<  x << ' ' << e.ff << ' ' << e.ss << '\n';
		}
		v.clear();
	}
	C.clear();
	
	for(int i = 1; i <= sz[d][x]; i++)
	{
		ans[i] = max(ans[i], query(1, 0, k - 1, i, i));
		//if(x == 5)
			//cout << i << ' '<< query(1, 0, k - 1, i, i) << "\n";

	}
	//cout << '\n';
	//cout << x << endl;
	
	return x;
 
}
void solve()
{
	cin >> n;
	for(int i = 1; i < n; i ++)
	{
		int a, b;
		cin >> a >> b;
		adj[a].pb(b);
		adj[b].pb(a);
	}
	centroid(1);
	for(int i = 1; i <= n; i++)
	{
		if(i & 1)
			cout << 1 << '\n';
		else
			cout << ans[i/2]+ 1 << '\n';
	}
}
int32_t main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	/*
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	*/
	int tt = 1;
	while(tt--)
		solve();
 
}
/*
5
1 2
2 3
4 2
3 5
*/
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9732 KB Output is correct
2 Correct 4 ms 9684 KB Output is correct
3 Correct 4 ms 9684 KB Output is correct
4 Correct 4 ms 9684 KB Output is correct
5 Correct 4 ms 9684 KB Output is correct
6 Correct 4 ms 9684 KB Output is correct
7 Correct 4 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9724 KB Output is correct
10 Correct 4 ms 9720 KB Output is correct
11 Correct 4 ms 9684 KB Output is correct
12 Correct 4 ms 9684 KB Output is correct
13 Correct 4 ms 9684 KB Output is correct
14 Correct 5 ms 9728 KB Output is correct
15 Correct 5 ms 9724 KB Output is correct
16 Correct 4 ms 9684 KB Output is correct
17 Correct 4 ms 9720 KB Output is correct
18 Correct 4 ms 9684 KB Output is correct
19 Correct 4 ms 9724 KB Output is correct
20 Correct 4 ms 9724 KB Output is correct
21 Correct 5 ms 9720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9732 KB Output is correct
2 Correct 4 ms 9684 KB Output is correct
3 Correct 4 ms 9684 KB Output is correct
4 Correct 4 ms 9684 KB Output is correct
5 Correct 4 ms 9684 KB Output is correct
6 Correct 4 ms 9684 KB Output is correct
7 Correct 4 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9724 KB Output is correct
10 Correct 4 ms 9720 KB Output is correct
11 Correct 4 ms 9684 KB Output is correct
12 Correct 4 ms 9684 KB Output is correct
13 Correct 4 ms 9684 KB Output is correct
14 Correct 5 ms 9728 KB Output is correct
15 Correct 5 ms 9724 KB Output is correct
16 Correct 4 ms 9684 KB Output is correct
17 Correct 4 ms 9720 KB Output is correct
18 Correct 4 ms 9684 KB Output is correct
19 Correct 4 ms 9724 KB Output is correct
20 Correct 4 ms 9724 KB Output is correct
21 Correct 5 ms 9720 KB Output is correct
22 Correct 11 ms 10836 KB Output is correct
23 Correct 12 ms 10764 KB Output is correct
24 Correct 11 ms 10840 KB Output is correct
25 Correct 11 ms 10816 KB Output is correct
26 Correct 11 ms 10760 KB Output is correct
27 Correct 12 ms 10708 KB Output is correct
28 Correct 13 ms 10820 KB Output is correct
29 Correct 13 ms 10816 KB Output is correct
30 Correct 11 ms 10812 KB Output is correct
31 Correct 12 ms 10836 KB Output is correct
32 Correct 16 ms 11072 KB Output is correct
33 Correct 17 ms 11208 KB Output is correct
34 Correct 8 ms 10372 KB Output is correct
35 Correct 7 ms 10372 KB Output is correct
36 Correct 13 ms 10888 KB Output is correct
37 Correct 8 ms 10836 KB Output is correct
38 Correct 15 ms 11016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9732 KB Output is correct
2 Correct 4 ms 9684 KB Output is correct
3 Correct 4 ms 9684 KB Output is correct
4 Correct 4 ms 9684 KB Output is correct
5 Correct 4 ms 9684 KB Output is correct
6 Correct 4 ms 9684 KB Output is correct
7 Correct 4 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9724 KB Output is correct
10 Correct 4 ms 9720 KB Output is correct
11 Correct 4 ms 9684 KB Output is correct
12 Correct 4 ms 9684 KB Output is correct
13 Correct 4 ms 9684 KB Output is correct
14 Correct 5 ms 9728 KB Output is correct
15 Correct 5 ms 9724 KB Output is correct
16 Correct 4 ms 9684 KB Output is correct
17 Correct 4 ms 9720 KB Output is correct
18 Correct 4 ms 9684 KB Output is correct
19 Correct 4 ms 9724 KB Output is correct
20 Correct 4 ms 9724 KB Output is correct
21 Correct 5 ms 9720 KB Output is correct
22 Correct 11 ms 10836 KB Output is correct
23 Correct 12 ms 10764 KB Output is correct
24 Correct 11 ms 10840 KB Output is correct
25 Correct 11 ms 10816 KB Output is correct
26 Correct 11 ms 10760 KB Output is correct
27 Correct 12 ms 10708 KB Output is correct
28 Correct 13 ms 10820 KB Output is correct
29 Correct 13 ms 10816 KB Output is correct
30 Correct 11 ms 10812 KB Output is correct
31 Correct 12 ms 10836 KB Output is correct
32 Correct 16 ms 11072 KB Output is correct
33 Correct 17 ms 11208 KB Output is correct
34 Correct 8 ms 10372 KB Output is correct
35 Correct 7 ms 10372 KB Output is correct
36 Correct 13 ms 10888 KB Output is correct
37 Correct 8 ms 10836 KB Output is correct
38 Correct 15 ms 11016 KB Output is correct
39 Correct 677 ms 71024 KB Output is correct
40 Correct 655 ms 69696 KB Output is correct
41 Correct 670 ms 70424 KB Output is correct
42 Correct 643 ms 70304 KB Output is correct
43 Correct 652 ms 70832 KB Output is correct
44 Correct 662 ms 70344 KB Output is correct
45 Correct 1350 ms 89124 KB Output is correct
46 Correct 1500 ms 94480 KB Output is correct
47 Correct 252 ms 45204 KB Output is correct
48 Correct 212 ms 42096 KB Output is correct
49 Correct 826 ms 81272 KB Output is correct
50 Correct 296 ms 64368 KB Output is correct
51 Correct 836 ms 87104 KB Output is correct