#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];
void Init(int _N) {
N = _N;
}
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;
}
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 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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
460 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
165 ms |
13564 KB |
Output is correct |
2 |
Runtime error |
43 ms |
10200 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
460 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
460 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
460 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |