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 <iostream>
#include <map>
#include <vector>
#include <queue>
#define ll long long
using namespace std;
map <ll, ll> mp;
bool ok;
ll visited[1000000], cur;
vector <ll> adj[1000000], V, U;
queue <ll> Q;
ll n, x, y, z, tot, P[1000000], cyc[1000000], deg[1000000];
ll dsu(ll u) {
if (P[u] == u) return u;
else return P[u] = dsu(P[u]);
}
void Init(int N_) {
ok = 0;
n = N_;
tot = 0;
mp.clear();
for (int i=0; i<n; ++i) {
adj[i].clear();
P[i] = i;
++mp[i];
}
}
void dfs(ll u, ll p) {
visited[u] = cur;
V.push_back(u);
for (auto v : adj[u]) {
if (visited[v] != cur) dfs(v, u);
else if (v != p) {
U.clear();
for (int i=V.size()-1; i>=0; --i) {
//cout << V[i] << "*";
if (mp.count(V[i])) U.push_back(V[i]);
if (V[i] == v) break;
}
//cout << endl;
mp.clear();
for (auto u : U) ++mp[u];
}
}
visited[u] = -1;
V.pop_back();
}
void relabel(ll u, ll p, ll w) {
P[u] = w;
visited[u] = cur;
for (auto v : adj[u]) {
if (visited[v] != cur) {
relabel(v, u, w);
}
}
}
void Link(int A, int B) {
if (mp.empty()) return;
else if (ok) {
if (A == z || B == z) return;
if (deg[A] == 2 || deg[B] == 2) {
mp.clear();
return;
}
++deg[A], ++deg[B];
x = dsu(A), y = dsu(B);
if (x == y) mp.clear();
P[x] = y;
return;
}
++deg[A], ++deg[B];
adj[A].push_back(B);
adj[B].push_back(A);
x = dsu(A), y = dsu(B);
if (x == y) {
++cyc[x];
if (cyc[x] <= 2) {
V.clear();
++cur;
dfs(A, -1);
}
else ok = 1;
}
else {
P[x] = y;
if (cyc[x] && cyc[y]) {
mp.clear();
return;
}
cyc[y] += cyc[x];
}
//cout << A << " " << B << " " << tot << endl;
if (deg[A] > 3) {
if (mp.count(A)) {
mp.clear();
++mp[A];
}
else mp.clear();
}
else if (deg[A] == 3) {
V.clear();
if (mp.count(A)) V.push_back(A);
for (auto v : adj[A]) {
if (mp.count(v)) V.push_back(v);
}
mp.clear();
for (auto u : V) ++mp[u];
}
if (deg[B] > 3) {
if (mp.count(B)) {
mp.clear();
++mp[B];
}
else mp.clear();
}
else if (deg[B] == 3) {
V.clear();
if (mp.count(B)) V.push_back(B);
for (auto v : adj[B]) {
if (mp.count(v)) V.push_back(v);
}
mp.clear();
for (auto u : V) ++mp[u];
}
if (ok) {
if (mp.size() > 1) {
mp.clear();
return;
}
for (auto [x, y] : mp) {
z = x;
for (auto v : adj[x]) {
--deg[v];
}
}
++cur;
tot = 0;
for (int i=0; i<n; ++i) {
if (i == z) continue;
if (deg[i] <= 1) {
Q.push(i);
visited[i] = cur;
}
else if (deg[i] > 2) {
mp.clear();
return;
}
}
while (!Q.empty()) {
auto u = Q.front();
Q.pop();
++tot;
for (auto v : adj[u]) {
if (v == z) continue;
if (visited[v] != cur) {
visited[v] = cur;
Q.push(v);
}
}
}
if (tot != n-1) mp.clear();
for (int i=0; i<n; ++i) {
if (i == z) continue;
if (deg[i] <= 1) {
++cur;
relabel(i, -1, i);
}
}
}
}
int CountCritical() {
return mp.size();
}
# | 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... |