#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
#define pb push_back
#define f first
#define s second
const int MAXN = 1e6 + 10;
struct Graph {
bool pos = false;
int crit;
int opp[MAXN];
};
Graph cand[4];
vi aL[MAXN];
int opp[MAXN], sz[MAXN];
int nLoop = 0;
int numCrit = 0;
int N;
int phase = 2;
namespace debug {
const int DEBUG = true;
template<class T1, class T2>
void pr(const pair<T1, T2> &x);
template<class T, size_t SZ>
void pr(const array<T, SZ> &x);
template<class T>
void pr(const vector<T> &x);
template<class T>
void pr(const set<T> &x);
template<class T1, class T2>
void pr(const map<T1, T2> &x);
template<class T>
void pr(const T &x) { if (DEBUG) cout << x; }
template<class T, class... Ts>
void pr(const T &first, const Ts &... rest) { pr(first), pr(rest...); }
template<class T1, class T2>
void pr(const pair<T1, T2> &x) { pr("{", x.f, ", ", x.s, "}"); }
template<class T>
void prIn(const T &x) {
pr("{");
bool fst = 1;
for (auto &a : x) {
pr(fst ? "" : ", ", a), fst = 0;
}
pr("}");
}
template<class T, size_t SZ>
void pr(const array<T, SZ> &x) { prIn(x); }
template<class T>
void pr(const vector<T> &x) { prIn(x); }
template<class T>
void pr(const set<T> &x) { prIn(x); }
template<class T1, class T2>
void pr(const map<T1, T2> &x) { prIn(x); }
void ps() { pr("\n"), cout << flush; }
template<class Arg, class... Args>
void ps(const Arg &first, const Args &... rest) {
pr(first, " ");
ps(rest...);
}
}
using namespace debug;
void Init(int iN) {
N = iN;
for (int i = 0; i < N; i++) {
opp[i] = i;
sz[i] = 1;
}
numCrit = N;
}
vector<pi> edges;
inline void addEdge(Graph &cG, int u, int v) {
// ps("edge:", u, v);
if (u == cG.crit || v == cG.crit) {
return;
}
if (cG.opp[u] == -1 || cG.opp[v] == -1 || cG.opp[u] == v) {
// ps(u, v);
// ps(cG.opp[u], cG.opp[v]);
cG.pos = false;
return;
}
int e1 = cG.opp[u];
int e2 = cG.opp[v];
cG.opp[u] = cG.opp[v] = -1;
cG.opp[e1] = e2;
cG.opp[e2] = e1;
}
void build(Graph &cG) {
// ps("crit:", cG.crit);
cG.pos = true;
for (int i = 0; i < N; i++) {
cG.opp[i] = i;
}
for (pi cE : edges) {
addEdge(cG, cE.f, cE.s);
if (!cG.pos) {
return;
}
}
// ps("fin build");
}
void Link(int u, int v) {
edges.pb({u, v});
if (phase == 2) {
aL[u].pb(v);
aL[v].pb(u);
if (aL[u].size() == 3) {
cand[0].crit = u;
build(cand[0]);
for (int i = 0; i < aL[u].size(); i++) {
cand[i + 1].crit = aL[u][i];
build(cand[i + 1]);
}
phase = 3;
return;
} else if (aL[v].size() == 3) {
cand[0].crit = v;
build(cand[0]);
for (int i = 0; i < aL[v].size(); i++) {
cand[i + 1].crit = aL[v][i];
build(cand[i + 1]);
}
phase = 3;
return;
}
assert(opp[u] != -1 && opp[v] != -1);
if (opp[u] == v) {
nLoop++;
if (nLoop == 1) {
numCrit = sz[u];
} else {
numCrit = 0;
}
opp[u] = opp[v] = -1;
return;
}
int e1 = opp[u];
int e2 = opp[v];
int nS = sz[u] + sz[v];
opp[u] = opp[v] = -1;
opp[e1] = e2;
opp[e2] = e1;
sz[e1] = nS;
sz[e2] = nS;
} else {
for (Graph &cG : cand) {
if (!cG.pos) {
continue;
}
addEdge(cG, u, v);
}
}
}
int CountCritical() {
if (phase == 2) {
return numCrit;
} else {
int ans = 0;
for (Graph &cG : cand) {
if (cG.pos) {
ans++;
}
}
return ans;
}
}
Compilation message
rings.cpp: In function 'void Link(int, int)':
rings.cpp:156:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < aL[u].size(); i++) {
~~^~~~~~~~~~~~~~
rings.cpp:166:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < aL[v].size(); i++) {
~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23928 KB |
Output is correct |
2 |
Correct |
24 ms |
24184 KB |
Output is correct |
3 |
Correct |
25 ms |
24184 KB |
Output is correct |
4 |
Correct |
23 ms |
23928 KB |
Output is correct |
5 |
Correct |
25 ms |
24056 KB |
Output is correct |
6 |
Correct |
25 ms |
24184 KB |
Output is correct |
7 |
Correct |
24 ms |
23972 KB |
Output is correct |
8 |
Correct |
25 ms |
24056 KB |
Output is correct |
9 |
Correct |
25 ms |
24312 KB |
Output is correct |
10 |
Correct |
26 ms |
24184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
456 ms |
48308 KB |
Output is correct |
2 |
Correct |
563 ms |
64884 KB |
Output is correct |
3 |
Correct |
334 ms |
56028 KB |
Output is correct |
4 |
Correct |
1197 ms |
70864 KB |
Output is correct |
5 |
Correct |
1239 ms |
84304 KB |
Output is correct |
6 |
Correct |
1219 ms |
82716 KB |
Output is correct |
7 |
Correct |
333 ms |
67300 KB |
Output is correct |
8 |
Correct |
1074 ms |
92148 KB |
Output is correct |
9 |
Correct |
1255 ms |
98880 KB |
Output is correct |
10 |
Correct |
814 ms |
81252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23928 KB |
Output is correct |
2 |
Correct |
24 ms |
24184 KB |
Output is correct |
3 |
Correct |
25 ms |
24184 KB |
Output is correct |
4 |
Correct |
23 ms |
23928 KB |
Output is correct |
5 |
Correct |
25 ms |
24056 KB |
Output is correct |
6 |
Correct |
25 ms |
24184 KB |
Output is correct |
7 |
Correct |
24 ms |
23972 KB |
Output is correct |
8 |
Correct |
25 ms |
24056 KB |
Output is correct |
9 |
Correct |
25 ms |
24312 KB |
Output is correct |
10 |
Correct |
26 ms |
24184 KB |
Output is correct |
11 |
Correct |
25 ms |
24184 KB |
Output is correct |
12 |
Correct |
29 ms |
24604 KB |
Output is correct |
13 |
Correct |
28 ms |
24568 KB |
Output is correct |
14 |
Correct |
26 ms |
24312 KB |
Output is correct |
15 |
Correct |
26 ms |
24568 KB |
Output is correct |
16 |
Correct |
28 ms |
24448 KB |
Output is correct |
17 |
Correct |
27 ms |
24472 KB |
Output is correct |
18 |
Correct |
27 ms |
24668 KB |
Output is correct |
19 |
Correct |
28 ms |
24568 KB |
Output is correct |
20 |
Correct |
28 ms |
24568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23928 KB |
Output is correct |
2 |
Correct |
24 ms |
24184 KB |
Output is correct |
3 |
Correct |
25 ms |
24184 KB |
Output is correct |
4 |
Correct |
23 ms |
23928 KB |
Output is correct |
5 |
Correct |
25 ms |
24056 KB |
Output is correct |
6 |
Correct |
25 ms |
24184 KB |
Output is correct |
7 |
Correct |
24 ms |
23972 KB |
Output is correct |
8 |
Correct |
25 ms |
24056 KB |
Output is correct |
9 |
Correct |
25 ms |
24312 KB |
Output is correct |
10 |
Correct |
26 ms |
24184 KB |
Output is correct |
11 |
Correct |
25 ms |
24184 KB |
Output is correct |
12 |
Correct |
29 ms |
24604 KB |
Output is correct |
13 |
Correct |
28 ms |
24568 KB |
Output is correct |
14 |
Correct |
26 ms |
24312 KB |
Output is correct |
15 |
Correct |
26 ms |
24568 KB |
Output is correct |
16 |
Correct |
28 ms |
24448 KB |
Output is correct |
17 |
Correct |
27 ms |
24472 KB |
Output is correct |
18 |
Correct |
27 ms |
24668 KB |
Output is correct |
19 |
Correct |
28 ms |
24568 KB |
Output is correct |
20 |
Correct |
28 ms |
24568 KB |
Output is correct |
21 |
Correct |
45 ms |
25908 KB |
Output is correct |
22 |
Correct |
59 ms |
27016 KB |
Output is correct |
23 |
Correct |
69 ms |
27888 KB |
Output is correct |
24 |
Correct |
52 ms |
27640 KB |
Output is correct |
25 |
Correct |
36 ms |
26744 KB |
Output is correct |
26 |
Correct |
53 ms |
27892 KB |
Output is correct |
27 |
Correct |
78 ms |
29024 KB |
Output is correct |
28 |
Correct |
54 ms |
28144 KB |
Output is correct |
29 |
Correct |
50 ms |
27508 KB |
Output is correct |
30 |
Correct |
85 ms |
29672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23928 KB |
Output is correct |
2 |
Correct |
24 ms |
24184 KB |
Output is correct |
3 |
Correct |
25 ms |
24184 KB |
Output is correct |
4 |
Correct |
23 ms |
23928 KB |
Output is correct |
5 |
Correct |
25 ms |
24056 KB |
Output is correct |
6 |
Correct |
25 ms |
24184 KB |
Output is correct |
7 |
Correct |
24 ms |
23972 KB |
Output is correct |
8 |
Correct |
25 ms |
24056 KB |
Output is correct |
9 |
Correct |
25 ms |
24312 KB |
Output is correct |
10 |
Correct |
26 ms |
24184 KB |
Output is correct |
11 |
Correct |
456 ms |
48308 KB |
Output is correct |
12 |
Correct |
563 ms |
64884 KB |
Output is correct |
13 |
Correct |
334 ms |
56028 KB |
Output is correct |
14 |
Correct |
1197 ms |
70864 KB |
Output is correct |
15 |
Correct |
1239 ms |
84304 KB |
Output is correct |
16 |
Correct |
1219 ms |
82716 KB |
Output is correct |
17 |
Correct |
333 ms |
67300 KB |
Output is correct |
18 |
Correct |
1074 ms |
92148 KB |
Output is correct |
19 |
Correct |
1255 ms |
98880 KB |
Output is correct |
20 |
Correct |
814 ms |
81252 KB |
Output is correct |
21 |
Correct |
25 ms |
24184 KB |
Output is correct |
22 |
Correct |
29 ms |
24604 KB |
Output is correct |
23 |
Correct |
28 ms |
24568 KB |
Output is correct |
24 |
Correct |
26 ms |
24312 KB |
Output is correct |
25 |
Correct |
26 ms |
24568 KB |
Output is correct |
26 |
Correct |
28 ms |
24448 KB |
Output is correct |
27 |
Correct |
27 ms |
24472 KB |
Output is correct |
28 |
Correct |
27 ms |
24668 KB |
Output is correct |
29 |
Correct |
28 ms |
24568 KB |
Output is correct |
30 |
Correct |
28 ms |
24568 KB |
Output is correct |
31 |
Correct |
45 ms |
25908 KB |
Output is correct |
32 |
Correct |
59 ms |
27016 KB |
Output is correct |
33 |
Correct |
69 ms |
27888 KB |
Output is correct |
34 |
Correct |
52 ms |
27640 KB |
Output is correct |
35 |
Correct |
36 ms |
26744 KB |
Output is correct |
36 |
Correct |
53 ms |
27892 KB |
Output is correct |
37 |
Correct |
78 ms |
29024 KB |
Output is correct |
38 |
Correct |
54 ms |
28144 KB |
Output is correct |
39 |
Correct |
50 ms |
27508 KB |
Output is correct |
40 |
Correct |
85 ms |
29672 KB |
Output is correct |
41 |
Correct |
246 ms |
41588 KB |
Output is correct |
42 |
Correct |
564 ms |
66400 KB |
Output is correct |
43 |
Correct |
241 ms |
51440 KB |
Output is correct |
44 |
Correct |
331 ms |
65160 KB |
Output is correct |
45 |
Correct |
377 ms |
65756 KB |
Output is correct |
46 |
Correct |
779 ms |
74812 KB |
Output is correct |
47 |
Correct |
1055 ms |
75992 KB |
Output is correct |
48 |
Correct |
303 ms |
58712 KB |
Output is correct |
49 |
Correct |
762 ms |
73296 KB |
Output is correct |
50 |
Correct |
833 ms |
72880 KB |
Output is correct |
51 |
Correct |
235 ms |
48788 KB |
Output is correct |
52 |
Correct |
316 ms |
58036 KB |
Output is correct |
53 |
Correct |
262 ms |
57688 KB |
Output is correct |
54 |
Correct |
806 ms |
79736 KB |
Output is correct |
55 |
Correct |
445 ms |
64360 KB |
Output is correct |