# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
406148 | ngrace | Split the Attractions (IOI19_split) | C++14 | 195 ms | 14212 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "split.h"
#include <bits/stdc++.h>
using namespace std;
vector<vector<pair<int,int>>> adj;
vector<bool> vis;
int dfs(int cur){//on tree
if(vis[cur]){
return 0;
}
vis[cur]=true;
int ret=0;
for(pair<int,int>& it:adj[cur]){
int sub=dfs(it.first);
ret+=sub;
it.second=sub;
}
return ret+1;
}
vector<int> find_split(int n, int a, int b, int c, vector<int> p, vector<int> q) {
adj=vector<vector<pair<int,int>>>(n);
for(int i=0;i<p.size();i++){
adj[p[i]].push_back({q[i],-1});
adj[q[i]].push_back({p[i],-1});
}
vis=vector<bool>(n);
dfs(0);
vector<int> answer(n,0);
bool done=false;
for(int i=0;i<p.size();i++){
for(pair<int,int> it:adj[i]){
int prim=-1,sec=-1,spare=-1;
if(it.second>=a){
if(n-it.second>=b){
prim=1;
sec=2;
spare=3;
}
else if(n-it.second>=c){
prim=1;
sec=3;
spare=2;
}
}
else if(it.second>=b){
if(n-it.second>=a){
prim=2;
sec=1;
spare=3;
}
else if(n-it.second>=c){
prim=2;
sec=3;
spare=1;
}
}
else if(it.second>=c){
if(n-it.second>=a){
prim=3;
sec=1;
spare=2;
}
else if(n-it.second>=b){
prim=3;
sec=2;
spare=1;
}
}
if(prim!=-1){
done=true;
int primC=(prim==1)?a:((prim==2)?b:c);
int secC=(sec==1)?a:((sec==2)?b:c);
vis=vector<bool>(n,false);
stack<int> s;
vis[i]=true;
s.push(it.first);
while(s.size()>0){
int cur=s.top();
s.pop();
if(!vis[cur]){
vis[cur]=true;
if(primC>0){
primC--;
answer[cur]=prim;
}
else{
answer[cur]=spare;
}
for(pair<int,int> pt:adj[cur]){
s.push(pt.first);
}
}
}
vis=vector<bool>(n,false);
vis[it.first]=true;
s.push(i);
while(s.size()>0){
int cur=s.top();
s.pop();
if(!vis[cur]){
vis[cur]=true;
if(secC>0){
secC--;
answer[cur]=sec;
}
else{
answer[cur]=spare;
}
for(pair<int,int> pt:adj[cur]){
s.push(pt.first);
}
}
}
}
if(done){
break;
}
}
if(done){
break;
}
}
return answer;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |