#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <queue>
#include "split.h"
using namespace std;
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
#define ll long long
#define ld long double
#define ui uint64_t
#define cont(set, element) ((set).find(element) != (set).end())
/********* DEBUG *********/
template <typename T>
void outvec(const vector<T>& Z){
for (const T& x : Z)
cout << x << ' ';
cout << "\n";
}
void printVariable(const any& var) {
if (!var.has_value()) {
cout << "null";
return;
}
if (var.type() == typeid(int)) {
cout << any_cast<int>(var);
} else if (var.type() == typeid(double)) {
cout << any_cast<double>(var);
} else if (var.type() == typeid(float)) {
cout << any_cast<float>(var);
} else if (var.type() == typeid(char)) {
cout << any_cast<char>(var);
} else if (var.type() == typeid(bool)) {
cout << (any_cast<bool>(var) ? "true" : "false");
} else if (var.type() == typeid(string)) {
cout << any_cast<string>(var);
} else if (var.type() == typeid(const char*)) {
cout << any_cast<const char*>(var);
} else if (var.type() == typeid(long long)) {
cout << any_cast<long long>(var);
} else {
cout << "[unknown type]";
}
}
template<typename... Args>
void outval(Args... args) {
vector<any> variables = {args...};
for (size_t i = 0; i < variables.size(); ++i) {
printVariable(variables[i]);
if (i != variables.size() - 1) {
cout << " ";
}
}
cout << "\n";
}
#define sp << " " <<
/********* DEBUG *********/
const ll MOD = 1e9+7;
const ll MOD2 = 998244353;
const ll inf = 1e18;
const ll mxN = 2000005;
ll lo, hi, lo_nd, hi_nd, N;
ll dfs(ll curr, vector<vector<ll>> &adj, ll par = -1, ll sum = 1){
if (lo_nd != -1)
return 0;
for (auto &x : adj[curr]){
if (x == par)
continue;
ll c = dfs(x, adj, curr);
if (lo_nd != -1)
return 0;
if (c >= lo && N-c >= hi){
hi_nd = curr;
lo_nd = x;
return 0;
}
if (c >= hi && N-c >= lo){
hi_nd = x;
lo_nd = curr;
return 0;
}
sum += c;
}
return sum;
}
vector<int> find_split(int n, int a, int b, int c, vector<int> p, vector<int> v) {
vector<vector<ll>> adj(n);
for (int i = 0; i < p.size(); i++){
adj[p[i]].push_back(v[i]);
adj[v[i]].push_back(p[i]);
}
vector<pair<ll,ll>> labels = {{a, 1}, {b, 2}, {c, 3}};
sort(all(labels));
lo = labels[0].first;
hi = labels[1].first;
lo_nd = hi_nd = -1;
N = n;
dfs(0, adj);
if (lo_nd == -1){
return vector<int>(n, 0);
}
vector<int> ans(n, labels[2].second);
vector<bool> vis(n);
queue<int> q;
vis[hi_nd] = true;
q.push(lo_nd);
// do lo
while (q.size() && labels[0].first){
int nd = q.front(); q.pop();
if (vis[nd])
continue;
vis[nd] = true;
ans[nd] = labels[0].second;
labels[0].first--;
for (auto &x : adj[nd]){
if (vis[x])
continue;
q.push(x);
}
}
while (q.size())
q.pop();
vis[hi_nd] = false;
q.push(hi_nd);
while (q.size() && labels[1].first){
int nd = q.front(); q.pop();
if (vis[nd])
continue;
vis[nd] = true;
ans[nd] = labels[1].second;
labels[1].first--;
for (auto &x : adj[nd]){
if (vis[x])
continue;
q.push(x);
}
}
return ans;
}
# | 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... |