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 <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vpi;
#define mp make_pair
#define f first
#define s second
#define pb push_back
#define sz(x) (int)(x).size()
int& ckmax(int& a, int b){
a = max(a, b);
return a;
}
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int mx = 1000005;
int N;
vpi eds;
vi cands;
int max_deg;
int deg[mx];
pi beg_other_end[mx]; //other end, cycle size
int cyc_num = 0;
int cyc_size = 0;
void addToBegGraph(int A, int B){
if(beg_other_end[A].f == B){
assert(beg_other_end[B].f == A);
cyc_num++;
cyc_size = beg_other_end[A].s;
}
else{
int tot_size = beg_other_end[A].s+beg_other_end[B].s;
int a = beg_other_end[A].f;
int b = beg_other_end[B].f;
beg_other_end[a] = mp(b, tot_size);
beg_other_end[b] = mp(a, tot_size);
}
}
void Init(int _N) {
N = _N;
for(int i = 0; i < N; i++){
beg_other_end[i] = mp(i, 1);
}
}
bool cand_bad[4];
int cand_other_end[4][mx];
void addToCandGraph(int cand_num, int A, int B){ //update cand_bad
if(cand_bad[cand_num]) return;
if(cand_other_end[cand_num][A] == -1 || cand_other_end[cand_num][B] == -1){
cand_bad[cand_num] = 1;
return;
}
if(cand_other_end[cand_num][A] == B){
assert(cand_other_end[cand_num][B] == A);
cand_bad[cand_num] = 1;
return;
}
int a = cand_other_end[cand_num][A];
int b = cand_other_end[cand_num][B];
cand_other_end[cand_num][A] = cand_other_end[cand_num][B] = -1;
cand_other_end[cand_num][a] = b;
cand_other_end[cand_num][b] = a;
}
void Link(int A, int B) {
eds.pb(mp(A, B));
if(max_deg >= 3){
for(int i = 0; i < sz(cands); i++){
if(A != cands[i] && B != cands[i]){
addToCandGraph(i, A, B);
}
}
}
else{
deg[A]++;
deg[B]++;
ckmax(max_deg, max(deg[A], deg[B]));
if(deg[B] == 3){
swap(A, B);
}
if(deg[A] == 3){
//start phase 2
cands.pb(A);
for(auto u: eds){
if(u.s == A) swap(u.f, u.s);
if(u.f == A){
cands.pb(u.s);
}
}
assert(sz(cands) == 4);
for(int i = 0; i < 4; i++){
for(int j = 0; j < N; j++){
cand_other_end[i][j] = j;
}
for(auto u: eds){
if(u.f != cands[i] && u.s != cands[i]){
addToCandGraph(i, u.f, u.s);
}
}
}
}
else{
//continue phase 1
addToBegGraph(A, B);
}
}
}
int CountCritical() {
if(max_deg >= 3){ //phase 2
int ans = 0;
for(int i = 0; i < 4; i++){
if(!cand_bad[i]){
ans++;
}
}
return ans;
}
//phase 1
if(cyc_num == 0){
return N;
}
if(cyc_num == 1){
return cyc_size;
}
return 0;
}
# | 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... |