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<cstdio>
#include<vector>
using namespace std;
const int SZ = 1000010;
int min(int a, int b) { return a < b ? a : b; }
int n;
int cycleNum;
vector<int>l[SZ];
bool p = true;
typedef struct Graph {
int st[4], ts[3];
int p[SZ], sz[SZ], ln[SZ];
bool is3E[SZ], isCX[SZ];
int x;
void init() {
x = n;
st[0] = ts[0] = n;
st[1] = st[2] = st[3] = ts[1] = ts[2] = 0;
for (int i = 0; i < n; i++) {
p[i] = i;
sz[i] = 1;
ln[i] = 0;
is3E[i] = isCX[i] = false;
}
}
void init(int X) {
init();
x = X;
int i, a, b, c;
for (i = 0; i < n; i++) {
for (int e : l[i]) {
if (e > i) {
if (e == x || i == x) {
isCX[e + i - x] = true;
continue;
}
a = f(e), b = f(i);
if (a != b) {
p[b] = a;
ln[a] += ln[b];
sz[a] += sz[b];
}
ln[a]++;
}
}
c = gC(i);
st[min(c, 3)]++;
if (c > 2)is3E[f(i)] = true;
}
for (i = 0; i < n; i++) {
if (i == p[i])ts[gT(i)]++;
}
}
int f(int a) {
return a == p[a] ? a : p[a] = f(p[a]);
}
int gC(int A) {
if (A == x)return 0;
return l[A].size() - (isCX[A] ? 1 : 0);
}
void u(int A, int B) {
int a = f(A), b = f(B);
if (a != b) {
p[b] = a;
ts[gT(a)]--;
ts[gT(b)]--;
ln[a] += ln[b];
sz[a] += sz[b];
is3E[a] |= is3E[b];
}
else {
ts[gT(a)]--;
}
}
void Con(int A, int B) {
int a = f(A), c;
if (x == n)l[A].push_back(B);
c = gC(A);
st[min(c - 1, 3)]--;
st[min(c, 3)]++;
if (c >= 3)is3E[a] = true;
}
int gT(int a) {
if (is3E[a]) {
return 2;
}
else if (sz[a] == ln[a]) {
return 1;
}
else {
return 0;
}
}
void Link(int A, int B) {
if (A == x || B == x) {
isCX[A + B - x] = true;
return;
}
u(A, B);
Con(A, B);
Con(B, A);
int a = f(A);
ln[a]++;
ts[gT(a)]++;
}
bool isChain() {
return (ts[1] + ts[2] == 0);
}
}Graph;
Graph MG, GV[4];
int GS;
void Init(int N) {
cycleNum = n = N;
MG.init();
}
void Link(int A, int B) {
if (p) {
MG.Link(A, B);
}
else {
l[A].push_back(B);
l[B].push_back(A);
}
for (int i = 0; i < GS; i++) {
if (GV[i].isChain())GV[i].Link(A, B);
}
if (MG.gT(MG.f(A)) == 1)cycleNum = MG.f(A);
if (MG.gT(MG.f(B)) == 1)cycleNum = MG.f(B);
if (p && MG.st[3]) {
p = false;
int a = A;
if (l[A].size() < 3)a = B;
GS = l[a].size() + 1;
GV[l[a].size()].init(a);
for (int i = 0; i < l[a].size(); i++) {
GV[i].init(l[a][i]);
}
}
}
int CountCritical() {
if (MG.ts[2]) {
int r = 0;
for (int i = 0; i < GS; i++) {
if (GV[i].isChain())r++;
}
return r;
}
else if (MG.ts[1]) {
if (MG.ts[1] > 1)return 0;
return MG.sz[cycleNum];
}
else {
return n;
}
}
Compilation message (stderr)
rings.cpp: In function 'void Link(int, int)':
rings.cpp:135:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
135 | for (int i = 0; i < l[a].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... |