Submission #386400

# Submission time Handle Problem Language Result Execution time Memory
386400 2021-04-06T13:57:20 Z MatheusLealV Meetings 2 (JOI21_meetings2) C++17
100 / 100
2467 ms 52812 KB
#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());
using namespace std;

// #define int long long
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
ll mod = (1000000007LL);
inline ll Mod(ll a, ll b){return (a%b);}
inline ll poww(ll a, ll b){ll res = 1;while (b > 0){if(b & 1) res = (res * a)%mod;a = (a * a)%mod;b >>= 1;}return res;}
ll gcd (ll a, ll b) { while (b) { a %= b,swap(a, b);}return a;}
void read(vector<int> &w, int n){w.resize(n);for(int i = 0; i < n; i++) cin>>w[i];}
void print(vector<int> &w){for(int i =0; i < sz(w); i++){if(i == sz(w) - 1) cout<<w[i]<<"\n";else cout<<w[i]<<" ";}}
int prodmod(vector<int> w);
int summod(vector<int> w);
///CUIDADO COM MAXN
#define N 600020
int n, block[N], sz[N], qtd_vertice, pai[N];
vector<int> grafo[N];
vector<int> lista;
int paist[N], szst[N];

struct Bit{
	int bit[N];
	vector<int> limpar;
	void init(){
		for(auto x: limpar) bit[x] = -1;
		limpar.clear();
	}
	void upd(int x, int v){
		for(int i = x; i < N; i += i&(-i)){
			limpar.pb(i);
			bit[i] = max(bit[i], v);
		}
	}

	int query(int x){
		int ans=-1;
		for(int i=x;i>0;i-=i&(-i)){
			ans=max(ans, bit[i]);
		}
		return ans;
	}
} T;
void dfsst(int x, int p){
	paist[x] = p;
	szst[x]=1;
	for(auto v: grafo[x]){
		if(v==p) continue;
		dfsst(v, x);
		szst[x]+=szst[v];
	}
}
void get_size(int x, int p){
	pai[x] = p;
	lista.push_back(x);
	sz[x] = 1;
	for(auto v: grafo[x]){
		if(block[v] or v == p) continue;
		get_size(v, x);
		sz[x] += sz[v];
	}
}

int Find_Centroid(int x){
	lista.clear();
	get_size(x, x);
	qtd_vertice = sz[x];
	int centro = x;
	for(auto x: lista){
		bool ok = true;
		if(qtd_vertice - sz[x] > qtd_vertice/2) ok = false;
		for(auto v: grafo[x]){
			if(v == pai[x] or block[v]) continue;
			if(sz[v] > qtd_vertice/2) ok = false;
		}
		if(ok) centro = x;
	}

	return centro;
}

map<int, int> mapa;
int curr_sz[N], reach[N];
void dfs_sz(int x, int p){
	reach[x]=1;
	curr_sz[x] = 1;
	for(auto v: grafo[x]){
		if(v==p) continue;
		if(block[v]){
			if(v != paist[x]) curr_sz[x] += szst[v];
			else curr_sz[x] += n-szst[x];
			continue;
		}
		dfs_sz(v,x);
		curr_sz[x]+=curr_sz[v];
	}
}
int ans[N];
int total = 0;
void query(int x, int p, int dist){
	int peso = curr_sz[x]; //
	//Se a > b entao 20000 - a < 2000 - b
	// cout<<"query "<<x<<" "<<curr_sz[x]<<" "<<dist<<" "<<T.query(200002-peso)<<"\n";
	if(T.query(200002-peso) > 0) ans[2*peso] = max(ans[2*peso], 1+dist + T.query(200002-peso));
	ans[2*min(total, peso)] = max(ans[2*min(total, peso)], dist + 1);
	for(auto v: grafo[x]){
		if(v == p or block[v]) continue;
		query(v,x,dist+1);
	}
}
void upd(int x, int p, int dist){
	int peso = curr_sz[x];
	T.upd(200002-peso, dist);
	// cout<<"update "<<x<<" "<<curr_sz[x]<<" "<<dist<<"\n";
	for(auto v: grafo[x]){
		if(v==p or block[v]) continue;
		upd(v,x,dist+1);
	}
}
int solve(int x, int deep){
	x = Find_Centroid(x);
	// cout<<"CENTROID = "<<x<<"\n";
	block[x] = true;
	T.init();
	for(auto v: grafo[x]){
		if(block[v])continue;
		dfs_sz(v,x);
	}
	for(auto v: grafo[x]){
		if(block[v]) continue;
		total = n - curr_sz[v];
		query(v,x,1);
		upd(v,x,1);
	}
	T.init();
	reverse(all(grafo[x]));
	for(auto v: grafo[x]){
		if(block[v]) continue;
		total = n - curr_sz[v];
		query(v,x,1);
		upd(v,x,1);
	}
	for(auto v: grafo[x]){
		if(block[v]) continue;
		solve(v, deep+1);
	}
	return x;
}

int32_t main(){
	ios::sync_with_stdio(false); cin.tie(0);
	cin>>n;
	for(int i = 1, a,b;i<n;i++){
		cin>>a>>b;
		grafo[a].pb(b);
		grafo[b].pb(a);
	}
	for(int i=1;i<=n;i++) ans[i]=1;
	dfsst(1,1);
	solve(1,1);
	for(int i =N-4;i>=1;i--)ans[i]=max(ans[i],ans[i+2]);
	for(int i =1;i<=n;i++) cout<<ans[i]<<"\n";
}
# Verdict Execution time Memory Grader output
1 Correct 13 ms 16876 KB Output is correct
2 Correct 14 ms 16876 KB Output is correct
3 Correct 13 ms 16876 KB Output is correct
4 Correct 13 ms 16876 KB Output is correct
5 Correct 13 ms 16876 KB Output is correct
6 Correct 13 ms 16876 KB Output is correct
7 Correct 13 ms 16876 KB Output is correct
8 Correct 13 ms 16876 KB Output is correct
9 Correct 13 ms 16876 KB Output is correct
10 Correct 13 ms 16876 KB Output is correct
11 Correct 14 ms 16876 KB Output is correct
12 Correct 15 ms 16876 KB Output is correct
13 Correct 15 ms 16876 KB Output is correct
14 Correct 13 ms 16876 KB Output is correct
15 Correct 28 ms 16876 KB Output is correct
16 Correct 13 ms 16876 KB Output is correct
17 Correct 13 ms 16876 KB Output is correct
18 Correct 13 ms 16876 KB Output is correct
19 Correct 13 ms 16876 KB Output is correct
20 Correct 14 ms 16876 KB Output is correct
21 Correct 13 ms 16876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 16876 KB Output is correct
2 Correct 14 ms 16876 KB Output is correct
3 Correct 13 ms 16876 KB Output is correct
4 Correct 13 ms 16876 KB Output is correct
5 Correct 13 ms 16876 KB Output is correct
6 Correct 13 ms 16876 KB Output is correct
7 Correct 13 ms 16876 KB Output is correct
8 Correct 13 ms 16876 KB Output is correct
9 Correct 13 ms 16876 KB Output is correct
10 Correct 13 ms 16876 KB Output is correct
11 Correct 14 ms 16876 KB Output is correct
12 Correct 15 ms 16876 KB Output is correct
13 Correct 15 ms 16876 KB Output is correct
14 Correct 13 ms 16876 KB Output is correct
15 Correct 28 ms 16876 KB Output is correct
16 Correct 13 ms 16876 KB Output is correct
17 Correct 13 ms 16876 KB Output is correct
18 Correct 13 ms 16876 KB Output is correct
19 Correct 13 ms 16876 KB Output is correct
20 Correct 14 ms 16876 KB Output is correct
21 Correct 13 ms 16876 KB Output is correct
22 Correct 24 ms 17532 KB Output is correct
23 Correct 24 ms 17532 KB Output is correct
24 Correct 31 ms 17532 KB Output is correct
25 Correct 25 ms 17532 KB Output is correct
26 Correct 24 ms 17532 KB Output is correct
27 Correct 29 ms 17512 KB Output is correct
28 Correct 24 ms 17532 KB Output is correct
29 Correct 24 ms 17532 KB Output is correct
30 Correct 24 ms 17532 KB Output is correct
31 Correct 25 ms 17532 KB Output is correct
32 Correct 30 ms 17532 KB Output is correct
33 Correct 30 ms 17660 KB Output is correct
34 Correct 17 ms 17532 KB Output is correct
35 Correct 21 ms 17532 KB Output is correct
36 Correct 26 ms 17480 KB Output is correct
37 Correct 21 ms 17532 KB Output is correct
38 Correct 26 ms 17532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 16876 KB Output is correct
2 Correct 14 ms 16876 KB Output is correct
3 Correct 13 ms 16876 KB Output is correct
4 Correct 13 ms 16876 KB Output is correct
5 Correct 13 ms 16876 KB Output is correct
6 Correct 13 ms 16876 KB Output is correct
7 Correct 13 ms 16876 KB Output is correct
8 Correct 13 ms 16876 KB Output is correct
9 Correct 13 ms 16876 KB Output is correct
10 Correct 13 ms 16876 KB Output is correct
11 Correct 14 ms 16876 KB Output is correct
12 Correct 15 ms 16876 KB Output is correct
13 Correct 15 ms 16876 KB Output is correct
14 Correct 13 ms 16876 KB Output is correct
15 Correct 28 ms 16876 KB Output is correct
16 Correct 13 ms 16876 KB Output is correct
17 Correct 13 ms 16876 KB Output is correct
18 Correct 13 ms 16876 KB Output is correct
19 Correct 13 ms 16876 KB Output is correct
20 Correct 14 ms 16876 KB Output is correct
21 Correct 13 ms 16876 KB Output is correct
22 Correct 24 ms 17532 KB Output is correct
23 Correct 24 ms 17532 KB Output is correct
24 Correct 31 ms 17532 KB Output is correct
25 Correct 25 ms 17532 KB Output is correct
26 Correct 24 ms 17532 KB Output is correct
27 Correct 29 ms 17512 KB Output is correct
28 Correct 24 ms 17532 KB Output is correct
29 Correct 24 ms 17532 KB Output is correct
30 Correct 24 ms 17532 KB Output is correct
31 Correct 25 ms 17532 KB Output is correct
32 Correct 30 ms 17532 KB Output is correct
33 Correct 30 ms 17660 KB Output is correct
34 Correct 17 ms 17532 KB Output is correct
35 Correct 21 ms 17532 KB Output is correct
36 Correct 26 ms 17480 KB Output is correct
37 Correct 21 ms 17532 KB Output is correct
38 Correct 26 ms 17532 KB Output is correct
39 Correct 1813 ms 43764 KB Output is correct
40 Correct 1704 ms 43452 KB Output is correct
41 Correct 1608 ms 43712 KB Output is correct
42 Correct 1589 ms 44060 KB Output is correct
43 Correct 1633 ms 44024 KB Output is correct
44 Correct 1485 ms 44112 KB Output is correct
45 Correct 2467 ms 48716 KB Output is correct
46 Correct 1990 ms 52812 KB Output is correct
47 Correct 396 ms 44112 KB Output is correct
48 Correct 272 ms 44368 KB Output is correct
49 Correct 1559 ms 44628 KB Output is correct
50 Correct 424 ms 44504 KB Output is correct
51 Correct 1182 ms 51784 KB Output is correct