Submission #1206332

#TimeUsernameProblemLanguageResultExecution timeMemory
1206332thelegendary08Split the Attractions (IOI19_split)C++17
0 / 100
2095 ms27460 KiB
#include "split.h"
#include<bits/stdc++.h>
#define f0r(i,n) for(int i = 0; i<n; i++)
#define FOR(i, k, n) for(int i = k; i<n; i++)
#define pb push_back
#define vi vector<int>
#define vout(v) for(auto u : v)cout<<u<<' '; cout<<'\n';
#define dout(x) cout<<x<<' '<<#x<<'\n';
#define pii pair<int,int>
#define vpii vector<pii>
#define vb vector<bool>
#define mp make_pair
using namespace std;
const int mxn = 2e5 + 5;
vector<vi> edj(mxn);
vector<vi> adj(mxn);
int m;
vi par(mxn);
vi sts(mxn);
int dfs(int node, int from){
	int cur = 1;
	par[node] = from;
	for(auto u : adj[node]){
		if(u == from)continue;
		cur += dfs(u,node);
	}
	sts[node] = cur;
	return cur;
}
vi p(mxn);
vi sz(mxn);
int find(int x){
	while(x != p[x])x = p[x];
	return x;
}
void unite(int a, int b){
	a = find(a);
	b = find(b);
	if(a == b)return;
	if(sz[a] < sz[b])swap(a,b);
	sz[a] += sz[b];
	p[b] = a;
}
vector<int> find_split(int n, int a, int b, int c, vector<int> P, vector<int> q) {
	m = P.size();
	vi deg(n);
	f0r(i,m){
		int x = P[i]; int y = q[i];
		edj[x].pb(y);
		edj[y].pb(x);
		deg[x]++; deg[y]++;
	}
	vi ans(n, 3);
	if(a == 1){
		queue<int>q;
		q.push(0);
		int cnt = 0;
		vb vis(n);
		vis[0] = 1;
		while(!q.empty() && cnt < b){
			int node = q.front(); q.pop();
			cnt++;
			ans[node] = 2;
			for(auto u : adj[node]){
				if(vis[u])continue;
				vis[u] = 1;
				q.push(u);
			}
		}
		f0r(i,n){
			if(ans[i] == 3){
				ans[i] = 1;
				break;
			}
		}
	}
	else{
		//subtask 1
		/*
		int st = -1;
		f0r(i, n){
			if(deg[i] == 1)st = i;
		}
		if(st == -1)st = 0;
		queue<int>q;
		vb vis(n);
		vis[st] = 1;
		q.push(st);
		int cnt = 1;
		ans[st] = 1;
		while(!q.empty() && cnt < a){
			int node = q.front(); q.pop();
			for(auto u : adj[node]){
				if(vis[u])continue;
				vis[u] = 1;
				cnt++;
				ans[u] = 1;
				q.push(u);
				break;
			}
		}
		cnt = 0;
		while(!q.empty() && cnt < b){
			int node = q.front(); q.pop();
			for(auto u : adj[node]){
				if(vis[u])continue;
				vis[u] = 1;
				cnt++;
				ans[u] = 2;
				q.push(u);
			}
		}
		*/
		bool okk = 0;
		f0r(tt, min(m, 1000)){
			f0r(i,n){
				p[i] = i;
				sz[i] = 0;
				adj[i].clear();
			}
			
			for(int i = tt; i<m; i++){
				if(find(P[i]) != find(q[i])){
					unite(P[i], q[i]);
					adj[P[i]].pb(q[i]);
					adj[q[i]].pb(P[i]);
				}
			}
			f0r(i, tt){
				if(find(P[i]) != find(q[i])){
					unite(P[i], q[i]);
					adj[P[i]].pb(q[i]);
					adj[q[i]].pb(P[i]);
				}
			}
			
			f0r(i, n)ans[i] = -1;
			vpii thing;
			thing.pb(mp(a,1));
			thing.pb(mp(b,2));
			thing.pb(mp(c,3));
			sort(thing.begin(), thing.end());
			
			
			dfs(0, -1);
			bool ok = 0;
			FOR(i, 1, n){
				int x = sts[i];
				int y = n - sts[i];
				if(thing[0].first <= x && thing[1].first <= y){
					// dout(i);
					queue<int>q;
					q.push(i);
					int cnt = 0;
					vb vis(n);
					vis[i] = 1;
					while(!q.empty() && cnt < thing[0].first){
						int node = q.front(); q.pop();
						cnt++;
						ans[node] = thing[0].second;
						for(auto u : adj[node]){
							if(vis[u])continue;
							if(u == par[node])continue;
							vis[u] = 1;
							q.push(u);
						}
					}
					
					q = queue<int>();
					q.push(par[i]);
					cnt = 0;
					vis[par[i]] = 1;
					while(!q.empty() && cnt < thing[1].first){
						int node = q.front(); q.pop();
						cnt++;
						ans[node] = thing[1].second;
						for(auto u : adj[node]){
							if(vis[u])continue;
							vis[u] = 1;
							q.push(u);
						}
					}
					ok = 1;
				}
				else if(thing[1].first <= x && thing[0].first <= y){
					// cout<<i<<'\n';
					// cout<<x<<' '<<y<<'\n';
					queue<int>q;
					q.push(i);
					int cnt = 0;
					vb vis(n);
					vis[i] = 1;
					while(!q.empty() && cnt < thing[1].first){
						int node = q.front(); q.pop();
						cnt++;
						ans[node] = thing[1].second;
						for(auto u : adj[node]){
							if(vis[u])continue;
							if(u == par[node])continue;
							vis[u] = 1;
							q.push(u);
						}
					}
					// vout(ans);
					q = queue<int>();
					q.push(par[i]);
					cnt = 0;
					vis[par[i]] = 1;
					while(!q.empty() && cnt < thing[0].first){
						int node = q.front(); q.pop();
						// dout(node);
						cnt++;
						ans[node] = thing[0].second;
						for(auto u : adj[node]){
							if(vis[u])continue;
							vis[u] = 1;
							q.push(u);
						}
					}
					ok = 1;
				}
				if(ok)break;
			}
			
			if(!ok){
				f0r(i,n)ans[i] = 0;
			}
			else{
				f0r(i,n)if(ans[i] == -1)ans[i] = thing[2].second;
				okk = 1; 
				break;
			}
			
		}
		
		
	}
	return ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...