#include "split.h"
#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
#define ll long long
using namespace std;
vector <ll> V;
vector <ll> adj[100000], E[200000];
bool ap[200000], ok;
vector <ll> G[200000], X[100000];
bool visited[100000];
array<ll, 2> T[3];
ll m, k, A[3], B[3], low[100000], st[100000], sz[200000], F[200000], cnt;
void tar(ll u, ll p) {
V.push_back(u);
low[u] = st[u] = ++cnt;
ll rch = 0;
for (auto v : adj[u]) {
if (!st[v]) {
++rch;
tar(v, u);
low[u] = min(low[u], low[v]);
if (low[v] >= st[u]) {
if (p != -1) ap[u] = 1;
while (!V.empty()) {
auto x = V.back();
V.pop_back();
X[x].push_back(k);
G[k].push_back(x);
if (x == v) break;
}
X[u].push_back(k);
G[k].push_back(u);
++k;
}
}
else if (v != p) {
low[u] = min(low[u], st[v]);
}
}
if (p == -1 & rch > 1) ap[u] = 1;
}
void dfs(ll u) {
visited[u] = 1;
for (auto v : adj[u]) {
if (!visited[v]) {
if (B[F[u]] < A[F[u]]) {
F[v] = F[u];
++B[F[u]];
}
else F[v] = 2;
dfs(v);
}
}
}
void solve(ll u, ll p) {
if (ap[u]) ++sz[u];
else sz[u] += G[u].size() - E[u].size();
ll mx = -1;
for (auto v : E[u]) {
if (v != p) {
solve(v, u);
sz[u] += sz[v];
mx = max(mx, sz[v]);
}
}
mx = max(mx, m - sz[u]);
if (!ok && !ap[u] && mx <= m-A[0]) {
if (mx >= A[1]) {
if (m-sz[u] == mx) {
F[p] = 1;
}
else {
for (auto v : E[u]) {
if (v != p) {
if (sz[v] == mx) {
F[v] = 1;
break;
}
}
}
}
for (auto v : G[u]) {
visited[v] = 1;
}
bool tmp = 0;
for (auto v : G[u]) {
if (F[v]) {
++B[F[v]];
dfs(v);
tmp = 1;
}
else if (tmp) {
if (B[0] < A[0]) {
++B[0];
F[v] = 0;
}
else F[v] = 2;
dfs(v);
}
}
for (auto v : G[u]) {
if (F[v]) break;
if (B[0] < A[0]) {
++B[0];
F[v] = 0;
}
else F[v] = 2;
dfs(v);
}
}
else {
ll cur = 0;
for (auto v : G[u]) {
F[v] = 1;
}
for (auto v : G[u]) {
if (v == p) cur += m-sz[u];
else if (ap[v]) cur += sz[v];
else ++cur;
F[v] = 0;
if (cur >= A[0]) {
break;
}
}
for (auto v : G[u]) {
visited[v] = 1;
}
for (auto v : G[u]) {
if (B[F[v]] < A[F[v]]) ++B[F[v]];
else F[v] = 2;
dfs(v);
}
}
ok = 1;
return;
}
}
vector<int> find_split(int n, int a, int b, int c, vector<int> p, vector<int> q) {
A[0] = T[0][0] = a, A[1] = T[1][0] = b, A[2] = T[2][0] = c;
T[1][1] = 1, T[2][1] = 2;
sort(A, A+3);
sort(T, T+3);
m = n;
k = n;
for (int i=0; i<p.size(); ++i) {
adj[p[i]].push_back(q[i]);
adj[q[i]].push_back(p[i]);
}
tar(0, -1);
for (int i=0; i<n; ++i) {
if (ap[i]) {
for (auto u : X[i]) {
E[i].push_back(u);
E[u].push_back(i);
}
}
}
solve(0, -1);
vector <int> U;
if (ok) {
U.clear();
for (int i=0; i<n; ++i) {
U.push_back(T[F[i]][1]+1);
}
}
else {
for (int i=0; i<n; ++i) {
U.push_back(F[i]);
}
}
return U;
}
Compilation message
split.cpp: In function 'void tar(long long int, long long int)':
split.cpp:45:8: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]
45 | if (p == -1 & rch > 1) ap[u] = 1;
| ~~^~~~~
split.cpp: In function 'std::vector<int> find_split(int, int, int, int, std::vector<int>, std::vector<int>)':
split.cpp:153:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
153 | for (int i=0; i<p.size(); ++i) {
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
17240 KB |
ok, correct split |
2 |
Incorrect |
7 ms |
17244 KB |
jury found a solution, contestant did not |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
17244 KB |
jury found a solution, contestant did not |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
17244 KB |
ok, correct split |
2 |
Correct |
113 ms |
37912 KB |
ok, correct split |
3 |
Incorrect |
39 ms |
24952 KB |
jury found a solution, contestant did not |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
17244 KB |
ok, correct split |
2 |
Correct |
4 ms |
17244 KB |
ok, no valid answer |
3 |
Incorrect |
4 ms |
17244 KB |
jury found a solution, contestant did not |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
17240 KB |
ok, correct split |
2 |
Incorrect |
7 ms |
17244 KB |
jury found a solution, contestant did not |
3 |
Halted |
0 ms |
0 KB |
- |