Submission #970797

# Submission time Handle Problem Language Result Execution time Memory
970797 2024-04-27T09:23:42 Z Tozzyyyy Meetings 2 (JOI21_meetings2) C++17
100 / 100
3624 ms 41408 KB
#include<bits/stdc++.h>
#define pll pair<long long, long long>
#define fi first
#define se second
#define bit(i,j) ((j >> i) & 1)

using namespace std;
const long long inf = 1e9+1;
const int mod = 1e9+7;
const int MAXN = 2e5+100;

vector<int> adj[MAXN];
int sz[MAXN] , del[MAXN];

int get_size(int u , int p){
	sz[u] = 1;
	for(auto v : adj[u]) if(v!=p and !del[v]) sz[u] += get_size(v , u);
	return sz[u];
}
int get_cen(int u , int p , int n){
	for(auto v : adj[u]) if(v!=p and !del[v] and sz[v] > n/2) return get_cen(v , u , n);
	return u;
}

struct segment_tree{
	int t[4 * MAXN];
	int d[4 * MAXN];

	void push(int id , int l , int r){
		if(l == r) return;
		if(d[id] == -inf) return;
		d[id<<1] = max(d[id<<1] , d[id]) ; d[id<<1|1] = max(d[id<<1|1] , d[id]);
		t[id<<1] = max(t[id<<1] , d[id]) ; t[id<<1|1] = max(t[id<<1|1] , d[id]);
		d[id] = -inf;
	}
	void update(int id , int l, int r , int x , int y , int val){
		push(id , l , r);
		if(r < x or l > y) return ;
		if(l >= x and r <= y){
			t[id] = max(t[id] , val) ; d[id] = max(d[id] , val) ; return;
		}
		int mid = l + r >> 1;
		update(id<<1 , l , mid , x , y , val); update(id<<1|1 , mid+1 , r , x , y , val);
		t[id] = max(t[id<<1] , t[id<<1|1]);
	}

	int get(int id , int l , int r , int x , int y){
		push(id, l , r);
		if(r < x or l > y) return -inf;
		if(l >= x and r <= y) return t[id];
		int mid = l + r >>1;
		return max(get(id<<1 , l , mid , x , y) , get(id<<1|1 , mid+1 , r , x , y));
	}
} ans;

struct segment_tree_2{
	int t[4 * MAXN];

	void update(int id , int l, int r , int x , int y , int val){
		if(r < x or l > y) return ;
		if(l >= x and r <= y){
			t[id] = max(t[id] , val); return;
		}
		int mid = l + r >> 1;
		update(id<<1 , l , mid , x , y , val); update(id<<1|1 , mid+1 , r , x , y , val);
		t[id] = max(t[id<<1] , t[id<<1|1]);
	}
	void reset(int id , int l, int r , int x , int y , int val){
		if(r < x or l > y) return ;
		if(l >= x and r <= y){
			t[id] = val; return;
		}
		int mid = l + r >> 1;
		reset(id<<1 , l , mid , x , y , val); reset(id<<1|1 , mid+1 , r , x , y , val);
		t[id] = max(t[id<<1] , t[id<<1|1]);
	}

	int get(int id , int l , int r , int x , int y){
		if(r < x or l > y) return -inf;
		if(l >= x and r <= y) return t[id];
		int mid = l + r >>1;
		return max(get(id<<1 , l , mid , x , y) , get(id<<1|1 , mid+1 , r , x , y));
	}

} T;

int mp[MAXN];
vector<pll> s; int n;
vector<int> vt;
void dfs(int u , int p , int d){
	int mx = T.get(1 , 1 , n , sz[u] , n);
	if(mx != -inf) ans.update(1 , 1 , n , 2 , sz[u] * 2 , mx + d + 1);
	for(auto v : adj[u]){
		if(del[v] == 0 and v != p) dfs(v , u , d+1);
	}
	s.push_back({sz[u] , d});
	vt.push_back(sz[u]);
}
void solve(int u){
	u = get_cen(u , 0 , get_size(u , 0));
	del[u] = 1;
	get_size(u , u);
	for(auto v : adj[u]){
		if(del[v] == 1) continue;
		T.update(1 , 1 , n , sz[u] - sz[v] , sz[u]-sz[v] , 0) ; vt.push_back(sz[u] - sz[v]) , dfs(v , u , 1);
		for(auto x : s) T.update(1 , 1 , n , x.fi , x.fi , x.se);
		s.clear();
	}
	for(auto x : vt) T.reset(1 , 1 , n , x , x , -inf);
	vt.clear();

	for(int v , j = adj[u].size() - 1 ; j >= 0 ; j--){
		v = adj[u][j];
		if(del[v] == 1) continue;
		T.update(1 , 1 , n , sz[u] - sz[v] , sz[u]-sz[v] , 0) ; vt.push_back(sz[u] - sz[v]) , dfs(v , u , 1);
		for(auto x : s) T.update(1 , 1 , n , x.fi , x.fi , x.se);
		s.clear();
	}
	for(auto x : vt) T.reset(1 , 1 , n , x , x , -inf);
	vt.clear();

	for(auto v : adj[u]){
		if(del[v] == 1) continue;
		solve(v);
	}
}
int32_t main(){
   ios_base::sync_with_stdio(0);
	cin.tie(0);
	
	for(int i = 0 ; i < 4 * MAXN;  i++) T.t[i] = -inf, ans.t[i] = 1 , ans.d[i] = -inf;
	cin >> n;
	for(int i = 1 ; i < n ; i++){
		int x , y; cin >> x>> y;
		adj[x].push_back(y) ; adj[y].push_back(x);
	}
		
	solve(1);
	for(int i = 1 ; i <= n ; i++){
		if(i & 1) cout << 1 << "\n";
		else cout << ans.get(1 , 1 , n , i , i) << "\n";
	}
	return 0;
}

Compilation message

meetings2.cpp: In member function 'void segment_tree::update(int, int, int, int, int, int)':
meetings2.cpp:42:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   42 |   int mid = l + r >> 1;
      |             ~~^~~
meetings2.cpp: In member function 'int segment_tree::get(int, int, int, int, int)':
meetings2.cpp:51:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   51 |   int mid = l + r >>1;
      |             ~~^~~
meetings2.cpp: In member function 'void segment_tree_2::update(int, int, int, int, int, int)':
meetings2.cpp:64:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   64 |   int mid = l + r >> 1;
      |             ~~^~~
meetings2.cpp: In member function 'void segment_tree_2::reset(int, int, int, int, int, int)':
meetings2.cpp:73:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   73 |   int mid = l + r >> 1;
      |             ~~^~~
meetings2.cpp: In member function 'int segment_tree_2::get(int, int, int, int, int)':
meetings2.cpp:81:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   81 |   int mid = l + r >>1;
      |             ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 16728 KB Output is correct
2 Correct 4 ms 16732 KB Output is correct
3 Correct 4 ms 16556 KB Output is correct
4 Correct 4 ms 16732 KB Output is correct
5 Correct 4 ms 16984 KB Output is correct
6 Correct 4 ms 16732 KB Output is correct
7 Correct 4 ms 16732 KB Output is correct
8 Correct 4 ms 16568 KB Output is correct
9 Correct 4 ms 16732 KB Output is correct
10 Correct 4 ms 16732 KB Output is correct
11 Correct 4 ms 16728 KB Output is correct
12 Correct 4 ms 16732 KB Output is correct
13 Correct 4 ms 16732 KB Output is correct
14 Correct 4 ms 16736 KB Output is correct
15 Correct 4 ms 16732 KB Output is correct
16 Correct 4 ms 16732 KB Output is correct
17 Correct 4 ms 16732 KB Output is correct
18 Correct 4 ms 16732 KB Output is correct
19 Correct 4 ms 16732 KB Output is correct
20 Correct 4 ms 16728 KB Output is correct
21 Correct 4 ms 16732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 16728 KB Output is correct
2 Correct 4 ms 16732 KB Output is correct
3 Correct 4 ms 16556 KB Output is correct
4 Correct 4 ms 16732 KB Output is correct
5 Correct 4 ms 16984 KB Output is correct
6 Correct 4 ms 16732 KB Output is correct
7 Correct 4 ms 16732 KB Output is correct
8 Correct 4 ms 16568 KB Output is correct
9 Correct 4 ms 16732 KB Output is correct
10 Correct 4 ms 16732 KB Output is correct
11 Correct 4 ms 16728 KB Output is correct
12 Correct 4 ms 16732 KB Output is correct
13 Correct 4 ms 16732 KB Output is correct
14 Correct 4 ms 16736 KB Output is correct
15 Correct 4 ms 16732 KB Output is correct
16 Correct 4 ms 16732 KB Output is correct
17 Correct 4 ms 16732 KB Output is correct
18 Correct 4 ms 16732 KB Output is correct
19 Correct 4 ms 16732 KB Output is correct
20 Correct 4 ms 16728 KB Output is correct
21 Correct 4 ms 16732 KB Output is correct
22 Correct 18 ms 16948 KB Output is correct
23 Correct 17 ms 16732 KB Output is correct
24 Correct 19 ms 16948 KB Output is correct
25 Correct 19 ms 16732 KB Output is correct
26 Correct 18 ms 16984 KB Output is correct
27 Correct 17 ms 16732 KB Output is correct
28 Correct 19 ms 16744 KB Output is correct
29 Correct 17 ms 16732 KB Output is correct
30 Correct 20 ms 16728 KB Output is correct
31 Correct 17 ms 16732 KB Output is correct
32 Correct 29 ms 17092 KB Output is correct
33 Correct 40 ms 17192 KB Output is correct
34 Correct 8 ms 16732 KB Output is correct
35 Correct 8 ms 16988 KB Output is correct
36 Correct 21 ms 16972 KB Output is correct
37 Correct 10 ms 16984 KB Output is correct
38 Correct 20 ms 17112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 16728 KB Output is correct
2 Correct 4 ms 16732 KB Output is correct
3 Correct 4 ms 16556 KB Output is correct
4 Correct 4 ms 16732 KB Output is correct
5 Correct 4 ms 16984 KB Output is correct
6 Correct 4 ms 16732 KB Output is correct
7 Correct 4 ms 16732 KB Output is correct
8 Correct 4 ms 16568 KB Output is correct
9 Correct 4 ms 16732 KB Output is correct
10 Correct 4 ms 16732 KB Output is correct
11 Correct 4 ms 16728 KB Output is correct
12 Correct 4 ms 16732 KB Output is correct
13 Correct 4 ms 16732 KB Output is correct
14 Correct 4 ms 16736 KB Output is correct
15 Correct 4 ms 16732 KB Output is correct
16 Correct 4 ms 16732 KB Output is correct
17 Correct 4 ms 16732 KB Output is correct
18 Correct 4 ms 16732 KB Output is correct
19 Correct 4 ms 16732 KB Output is correct
20 Correct 4 ms 16728 KB Output is correct
21 Correct 4 ms 16732 KB Output is correct
22 Correct 18 ms 16948 KB Output is correct
23 Correct 17 ms 16732 KB Output is correct
24 Correct 19 ms 16948 KB Output is correct
25 Correct 19 ms 16732 KB Output is correct
26 Correct 18 ms 16984 KB Output is correct
27 Correct 17 ms 16732 KB Output is correct
28 Correct 19 ms 16744 KB Output is correct
29 Correct 17 ms 16732 KB Output is correct
30 Correct 20 ms 16728 KB Output is correct
31 Correct 17 ms 16732 KB Output is correct
32 Correct 29 ms 17092 KB Output is correct
33 Correct 40 ms 17192 KB Output is correct
34 Correct 8 ms 16732 KB Output is correct
35 Correct 8 ms 16988 KB Output is correct
36 Correct 21 ms 16972 KB Output is correct
37 Correct 10 ms 16984 KB Output is correct
38 Correct 20 ms 17112 KB Output is correct
39 Correct 1622 ms 26596 KB Output is correct
40 Correct 1542 ms 26400 KB Output is correct
41 Correct 1565 ms 26512 KB Output is correct
42 Correct 1566 ms 26864 KB Output is correct
43 Correct 1576 ms 25808 KB Output is correct
44 Correct 1567 ms 26692 KB Output is correct
45 Correct 3159 ms 34288 KB Output is correct
46 Correct 3624 ms 41408 KB Output is correct
47 Correct 373 ms 25672 KB Output is correct
48 Correct 289 ms 25900 KB Output is correct
49 Correct 2077 ms 27628 KB Output is correct
50 Correct 521 ms 27580 KB Output is correct
51 Correct 1949 ms 34092 KB Output is correct