#include <bits/stdc++.h>
using namespace std;
#define TRACE(x) cerr << #x << " = " << x << endl
#define _ << " _ " <<
#define fi first
#define se second
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int INF = 1e9;
const int OFF = 1 << 17;
struct SegTree {
set<int> t[2 * OFF];
SegTree() {
for (int i = 1; i < 2 * OFF; i++)
t[i].insert(INF);
}
void insert(int l, int r, int x, int i = 1, int lo = 0, int hi = OFF) {
if (r <= lo || hi <= l) return;
if (l <= lo && hi <= r) t[i].insert(x);
else {
int mid = (lo + hi) / 2;
insert(l, r, x, 2 * i + 0, lo, mid);
insert(l, r, x, 2 * i + 1, mid, hi);
}
}
void erase(int l, int r, int x, int i = 1, int lo = 0, int hi = OFF) {
if (r <= lo || hi <= l) return;
if (l <= lo && hi <= r) t[i].erase(x);
else {
int mid = (lo + hi) / 2;
erase(l, r, x, 2 * i + 0, lo, mid);
erase(l, r, x, 2 * i + 1, mid, hi);
}
}
int lower_bound(int i, int x) {
int lb = INF;
for (i += OFF; i > 0; i /= 2)
lb = min(lb, *t[i].lower_bound(x));
return lb;
}
} M[2], P[2];
const int MAXN = 1e5 + 10;
int x[MAXN][2];
vector<int> V[MAXN][2];
bool vis[MAXN];
int cycle_id[MAXN];
vector<vector<int>> cycle;
queue<pii> Q[2];
void no() {
cout << "NE\n";
exit(0);
}
void find_path(int i, bool is_cycle) {
if (vis[i]) return;
if (is_cycle) cycle.push_back(vector<int>());
int st_o = V[x[i][0]][0].size() != 2, o = st_o;
while (!vis[i]) {
vis[i] = true;
if (V[x[i][o]][o].size() == 1) break;
assert(V[x[i][o]][o].size() == 2);
int j = V[x[i][o]][o][0] ^ V[x[i][o]][o][1] ^ i;
assert(x[i][o] == x[j][o]);
if (is_cycle) {
cycle.back().push_back(i);
cycle_id[i] = (int)cycle.size() - 1;
int l = x[i][o ^ 1], r = x[j][o ^ 1];
if (l > r) swap(l, r);
P[o].insert(l, r + 1, x[i][o]);
} else {
if (o == st_o)
Q[o].push({i, j});
}
i = j;
o ^= 1;
}
if (is_cycle) cycle.back().push_back(i);
if (!is_cycle && o == st_o) no();
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> x[i][0] >> x[i][1];
V[x[i][0]][0].push_back(i);
V[x[i][1]][1].push_back(i);
}
for (int i = 0; i < MAXN; i++)
for (int j : {0, 1})
assert(V[i][j].size() <= 2);
for (int i = 0; i < MAXN; i++)
for (int j : {0, 1})
if (V[i][j].size() == 1)
find_path(V[i][j][0], false);
for (int i = 0; i < MAXN; i++)
for (int j : {0, 1})
if (V[i][j].size() == 2)
find_path(V[i][j][0], true);
vector<bool> matched_cycle(cycle.size(), false);
vector<pii> sol;
while (!(Q[0].empty() && Q[1].empty())) {
int o = Q[0].empty();
int i = Q[o].front().fi, j = Q[o].front().se;
Q[o].pop();
assert(x[i][o] == x[j][o]);
int l = x[i][o ^ 1], r = x[j][o ^ 1];
if (l > r) swap(l, r);
if (M[o ^ 1].lower_bound(x[i][o], l) <= r) no();
for (int y = P[o ^ 1].lower_bound(x[i][o], l); y <= r;
y = P[o ^ 1].lower_bound(x[i][o], l)) {
int c = cycle_id[V[y][o ^ 1][0]];
if (matched_cycle[c]) continue;
matched_cycle[c] = true;
for (int k = 0; k < (int)cycle[c].size() - 1; k++) {
int oc = k & 1, ic = cycle[c][k], jc = cycle[c][k + 1];
int lc = x[ic][oc ^ 1], rc = x[jc][oc ^ 1];
if (lc > rc) swap(lc, rc);
P[oc].erase(lc, rc + 1, x[ic][oc]);
if (oc == o) Q[oc].push({ic, jc});
}
}
M[o].insert(l, r, x[i][o]);
sol.push_back({i, j});
}
for (int i = 0; i < (int)cycle.size(); i++)
if (!matched_cycle[i])
for (int j = 0; j < (int)cycle[i].size() - 1; j += 2)
sol.push_back({cycle[i][j], cycle[i][j + 1]});
cout << "DA\n";
for (auto p : sol) cout << p.fi + 1 << " " << p.se + 1 << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
127 ms |
103544 KB |
Output is correct |
2 |
Correct |
110 ms |
103544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
127 ms |
103544 KB |
Output is correct |
2 |
Correct |
110 ms |
103544 KB |
Output is correct |
3 |
Correct |
122 ms |
103544 KB |
Output is correct |
4 |
Correct |
107 ms |
103544 KB |
Output is correct |
5 |
Correct |
108 ms |
103544 KB |
Output is correct |
6 |
Correct |
110 ms |
103544 KB |
Output is correct |
7 |
Correct |
107 ms |
103544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
127 ms |
103544 KB |
Output is correct |
2 |
Correct |
110 ms |
103544 KB |
Output is correct |
3 |
Correct |
122 ms |
103544 KB |
Output is correct |
4 |
Correct |
107 ms |
103544 KB |
Output is correct |
5 |
Correct |
108 ms |
103544 KB |
Output is correct |
6 |
Correct |
110 ms |
103544 KB |
Output is correct |
7 |
Correct |
107 ms |
103544 KB |
Output is correct |
8 |
Correct |
106 ms |
103544 KB |
Output is correct |
9 |
Correct |
110 ms |
103544 KB |
Output is correct |
10 |
Correct |
111 ms |
103612 KB |
Output is correct |
11 |
Correct |
108 ms |
103544 KB |
Output is correct |
12 |
Correct |
111 ms |
103544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
127 ms |
103544 KB |
Output is correct |
2 |
Correct |
110 ms |
103544 KB |
Output is correct |
3 |
Correct |
122 ms |
103544 KB |
Output is correct |
4 |
Correct |
107 ms |
103544 KB |
Output is correct |
5 |
Correct |
108 ms |
103544 KB |
Output is correct |
6 |
Correct |
110 ms |
103544 KB |
Output is correct |
7 |
Correct |
107 ms |
103544 KB |
Output is correct |
8 |
Correct |
106 ms |
103544 KB |
Output is correct |
9 |
Correct |
110 ms |
103544 KB |
Output is correct |
10 |
Correct |
111 ms |
103612 KB |
Output is correct |
11 |
Correct |
108 ms |
103544 KB |
Output is correct |
12 |
Correct |
111 ms |
103544 KB |
Output is correct |
13 |
Correct |
115 ms |
104440 KB |
Output is correct |
14 |
Correct |
115 ms |
104440 KB |
Output is correct |
15 |
Correct |
119 ms |
104312 KB |
Output is correct |
16 |
Correct |
118 ms |
104416 KB |
Output is correct |
17 |
Correct |
118 ms |
104440 KB |
Output is correct |
18 |
Correct |
119 ms |
104568 KB |
Output is correct |
19 |
Correct |
114 ms |
104312 KB |
Output is correct |
20 |
Correct |
123 ms |
104460 KB |
Output is correct |
21 |
Correct |
122 ms |
104416 KB |
Output is correct |
22 |
Correct |
110 ms |
103672 KB |
Output is correct |
23 |
Correct |
117 ms |
104568 KB |
Output is correct |
24 |
Correct |
126 ms |
104780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
127 ms |
103544 KB |
Output is correct |
2 |
Correct |
110 ms |
103544 KB |
Output is correct |
3 |
Correct |
122 ms |
103544 KB |
Output is correct |
4 |
Correct |
107 ms |
103544 KB |
Output is correct |
5 |
Correct |
108 ms |
103544 KB |
Output is correct |
6 |
Correct |
110 ms |
103544 KB |
Output is correct |
7 |
Correct |
107 ms |
103544 KB |
Output is correct |
8 |
Correct |
106 ms |
103544 KB |
Output is correct |
9 |
Correct |
110 ms |
103544 KB |
Output is correct |
10 |
Correct |
111 ms |
103612 KB |
Output is correct |
11 |
Correct |
108 ms |
103544 KB |
Output is correct |
12 |
Correct |
111 ms |
103544 KB |
Output is correct |
13 |
Correct |
115 ms |
104440 KB |
Output is correct |
14 |
Correct |
115 ms |
104440 KB |
Output is correct |
15 |
Correct |
119 ms |
104312 KB |
Output is correct |
16 |
Correct |
118 ms |
104416 KB |
Output is correct |
17 |
Correct |
118 ms |
104440 KB |
Output is correct |
18 |
Correct |
119 ms |
104568 KB |
Output is correct |
19 |
Correct |
114 ms |
104312 KB |
Output is correct |
20 |
Correct |
123 ms |
104460 KB |
Output is correct |
21 |
Correct |
122 ms |
104416 KB |
Output is correct |
22 |
Correct |
110 ms |
103672 KB |
Output is correct |
23 |
Correct |
117 ms |
104568 KB |
Output is correct |
24 |
Correct |
126 ms |
104780 KB |
Output is correct |
25 |
Correct |
405 ms |
132760 KB |
Output is correct |
26 |
Correct |
421 ms |
132668 KB |
Output is correct |
27 |
Correct |
385 ms |
132656 KB |
Output is correct |
28 |
Correct |
402 ms |
132636 KB |
Output is correct |
29 |
Correct |
347 ms |
131636 KB |
Output is correct |
30 |
Correct |
340 ms |
131636 KB |
Output is correct |
31 |
Correct |
388 ms |
131924 KB |
Output is correct |
32 |
Correct |
351 ms |
131636 KB |
Output is correct |
33 |
Correct |
317 ms |
129076 KB |
Output is correct |
34 |
Correct |
161 ms |
107128 KB |
Output is correct |
35 |
Correct |
159 ms |
107128 KB |
Output is correct |
36 |
Correct |
1191 ms |
170496 KB |
Output is correct |
37 |
Correct |
463 ms |
141748 KB |
Output is correct |
38 |
Correct |
462 ms |
141192 KB |
Output is correct |