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 <algorithm>
using namespace std;
const int mxn = 1e5+5;
bool visited[mxn];
int sz[mxn];
vector<int> color, adj[mxn];
vector<pair<int, int>> new_edges;
void dfs(int u) {
visited[u] = true;
sz[u] = 1;
for(int v : adj[u]) if(!visited[v]) {
new_edges.push_back({u, v});
dfs(v); sz[u] += sz[v];
}
}
int find_centroid(int u, int n) {
visited[u] = true;
for(int v : adj[u]) if(!visited[v] && 2*sz[v]>n) {
return find_centroid(v, n);
}
return u;
}
void mark(int u, int m, int &cnt) {
if(cnt <= 0) return;
visited[u] = true;
color[u] = m;
cnt--;
for(int v : adj[u]) if(!visited[v]) {
mark(v, m, cnt);
}
}
vector<int> find_split(int n, int a, int b, int c, vector<int> p, vector<int> q) {
for(int i = 0; i < p.size(); i++) {
int u = p[i], v = q[i];
adj[u].push_back(v), adj[v].push_back(u);
}
dfs(0);
for(int i = 0; i < n; i++) {
adj[i].clear();
}
for(auto e : new_edges) {
adj[e.first].push_back(e.second);
adj[e.second].push_back(e.first);
}
fill(visited, visited+n, false);
int centroid = find_centroid(0, n);
fill(visited, visited+n, false);
vector<pair<int, int>> groups = {pair<int, int>{a, 1}, pair<int, int>{b, 2}, pair<int, int>{c, 3}};
sort(groups.begin(), groups.end());
color.resize(n, groups[2].second);
int small_group = -1;
int cnt;
for(int u : adj[centroid]) {
if(sz[u] >= groups[0].first) {
visited[centroid] = true;
cnt = groups[centroid].first;
mark(u, groups[centroid].second, cnt);
small_group = u;
}
break;
}
if(small_group == -1) return vector<int>(n, 0);
fill(visited, visited+n, false);
visited[small_group] = true;
cnt = groups[1].first;
mark(centroid, groups[1].second, cnt);
return color;
}
Compilation message (stderr)
split.cpp: In function 'std::vector<int> find_split(int, int, int, int, std::vector<int>, std::vector<int>)':
split.cpp:41:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for(int i = 0; i < p.size(); i++) {
| ~~^~~~~~~~~~
# | 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... |