#include "split.h"
#include <bits/stdc++.h>
using namespace std;
const int SZ = 2e5 + 5;
vector <int> v[SZ];
int n;
int d[SZ], Max[SZ], TT[SZ];
bool viz[SZ];
void dfs(int nod){
viz[nod] = 1; d[nod] = 1;
for(auto it : v[nod]){
if(viz[it]) continue ;
TT[it] = nod;
dfs(it);
d[nod] += d[it];
Max[nod] = max(Max[nod], d[it]);
}
viz[nod] = 0;
}
int find_centroid(int nod){
viz[nod] = 1;
if(max(Max[nod], n - d[nod]) <= n / 2) return nod;
for(auto it : v[nod]){
if(viz[it]) continue ;
int x = find_centroid(it);
if(x){
viz[nod] = 0;
return x;
}
}
viz[nod] = 0;
return 0;
}
vector <int> comp[SZ];
int id[SZ], RG[SZ];
inline int find(int x){
int R = x;
while(R != id[R]) R = id[R];
while(id[x] != R){
int aux = id[x];
id[x] = R;
x = aux;
}
return R;
}
inline void unite(int x, int y){
if(x == y) return ;
if(RG[x] >= RG[y]) id[y] = x, RG[x] += RG[y];
else id[x] = y, RG[y] += RG[x];
}
void make_comp(int nod, int NR){
comp[NR].push_back(nod);
viz[nod] = 1;
for(auto it : v[nod]){
if(viz[it]) continue ;
unite(find(it), find(nod));
make_comp(it, NR);
}
viz[nod] = 0;
}
bool merge_comp(int nod, int &am, int a){
viz[nod] = 1;
for(auto it : v[nod]){
if(viz[it]) continue ;
if(find(it) == find(nod)) merge_comp(nod, am, a);
else{
unite(find(nod), find(it));
am = RG[find(nod)];
if(am >= a) break ;
merge_comp(nod, am, a);
}
}
viz[nod] = 0;
return 0;
}
int cul[SZ];
void paint_comp(int nod, int &rem, int c){
if(rem == 0) return ;
viz[nod] = 1;
--rem;
cul[nod] = c;
for(auto it : v[nod]){
if(viz[it] || cul[it] || find(it) != find(nod)) continue ;
if(rem) paint_comp(it, rem, c);
}
viz[nod] = 0;
}
vector<int> find_split(int N, int a, int b, int c, vector<int> p, vector<int> q) {
n = N;
vector<int> res(n);
int m = p.size();
for(int i = 0; i < m ; ++i){
v[p[i] + 1].push_back(q[i] + 1);
v[q[i] + 1].push_back(p[i] + 1);
}
int ca = 1, cb = 2, cc = 3;
if(a > b) swap(a, b), swap(ca, cb);
if(a > c) swap(a, c), swap(ca, cc);
if(b > c) swap(b, c), swap(cb, cc);
for(int i = 1; i <= n ; ++i) id[i] = i, RG[i] = 1;
dfs(1);
int nod = find_centroid(1);
dfs(nod);
viz[nod] = 1;
int NR = 0;
for(auto it : v[nod]){
make_comp(it, NR);
NR++;
}
bool found = false;
int wh = 0;
for(int i = 0; i < NR ; ++i){
if(comp[i].size() >= a){
found = true;
wh = comp[i][0];
break ;
}
}
//cerr << found << endl;
if(!found){
for(int i = 0; i < NR ; ++i){
int x = comp[i].size();
bool ok = merge_comp(comp[i][0], x, a);
if(ok){
wh = comp[i][0];
break ;
}
}
}
if(wh == 0) return res;
paint_comp(wh, a, ca);
wh = find(wh);
for(int i = 1; i <= n ; ++i) unite(find(i), wh);
paint_comp(nod, b, cb);
for(int i = 0; i < n ; ++i){
if(cul[i + 1] == 0) cul[i + 1] = cc;
res[i] = cul[i + 1];
}
return res;
}
Compilation message
split.cpp: In function 'std::vector<int> find_split(int, int, int, int, std::vector<int>, std::vector<int>)':
split.cpp:137:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(comp[i].size() >= a){
~~~~~~~~~~~~~~~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
9720 KB |
ok, correct split |
2 |
Correct |
12 ms |
9720 KB |
ok, correct split |
3 |
Correct |
11 ms |
9720 KB |
ok, correct split |
4 |
Correct |
14 ms |
9720 KB |
ok, correct split |
5 |
Correct |
11 ms |
9720 KB |
ok, correct split |
6 |
Correct |
12 ms |
9720 KB |
ok, correct split |
7 |
Correct |
188 ms |
23460 KB |
ok, correct split |
8 |
Correct |
187 ms |
22108 KB |
ok, correct split |
9 |
Execution timed out |
2088 ms |
21344 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
9720 KB |
ok, correct split |
2 |
Correct |
13 ms |
9720 KB |
ok, correct split |
3 |
Correct |
13 ms |
9720 KB |
ok, correct split |
4 |
Execution timed out |
2074 ms |
21972 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
12 ms |
9720 KB |
invalid split: #1=1, #2=1, #3=3 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
9720 KB |
ok, correct split |
2 |
Correct |
12 ms |
9720 KB |
ok, no valid answer |
3 |
Correct |
12 ms |
9720 KB |
ok, correct split |
4 |
Correct |
12 ms |
9720 KB |
ok, correct split |
5 |
Execution timed out |
2083 ms |
9720 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
9720 KB |
ok, correct split |
2 |
Correct |
12 ms |
9720 KB |
ok, correct split |
3 |
Correct |
11 ms |
9720 KB |
ok, correct split |
4 |
Correct |
14 ms |
9720 KB |
ok, correct split |
5 |
Correct |
11 ms |
9720 KB |
ok, correct split |
6 |
Correct |
12 ms |
9720 KB |
ok, correct split |
7 |
Correct |
188 ms |
23460 KB |
ok, correct split |
8 |
Correct |
187 ms |
22108 KB |
ok, correct split |
9 |
Execution timed out |
2088 ms |
21344 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |