QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#191326 | #6414. Classical Maximization Problem | smosp# | RE | 81ms | 3704kb | C++23 | 2.4kb | 2023-09-29 21:51:20 | 2023-09-29 21:51:21 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
int main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
int tt;
cin >> tt;
while(tt--) {
int n;
cin >> n;
n *= 2;
vector<int> xs, ys;
vector<pii> pts;
rep(i, 0, n) {
int x, y;
cin >> x >> y;
pts.push_back({x, y});
xs.push_back(x), ys.push_back(y);
}
sort(all(xs));
sort(all(ys));
xs.erase(unique(all(xs)), end(xs));
ys.erase(unique(all(ys)), end(ys));
auto xget = [&xs](int x)->int {
return lower_bound(xs.begin(), xs.end(), x) - xs.begin();
};
auto yget = [&](int y)->int {
return sz(xs) + (lower_bound(ys.begin(), ys.end(), y) - ys.begin());
};
int c = sz(xs) + sz(ys);
vector<vector<pair<int,int>>> g(c);
for (int i =0; i < sz(pts); i++) {
auto& [x, y] = pts[i];
int X = xget(x), Y = yget(y);
g[X].push_back({Y, i});
g[Y].push_back({X, i});
}
vi vis(c), taken(n);
vector<pii> ans;
for (int i =0; i < c; i++) {
if (vis[i]) continue;
auto dfs = [&](auto& self, int s)->int {
vis[s] = 1;
int ret = -1;
for (auto& [u, i] : g[s]) {
if (vis[u] == 1) continue; // back edge
int curr_edge = i;
if (!vis[i]) {
int tmp = self(self, u);
if (tmp != -1) {
taken[curr_edge] = taken[tmp] = 1;
ans.push_back({curr_edge, tmp});
curr_edge = -1;
}
}
if (curr_edge != -1) {
if (ret != -1) {
taken[ret] = taken[curr_edge] = 1;
ans.push_back({ret, curr_edge});
ret = -1;
} else ret = curr_edge;
}
}
vis[s] = 2;
return ret;
};
dfs(dfs, i);
}
int friends = ans.size();
vi not_taken;
int last = -1;
for (int i =0; i < n; i++) {
if (!taken[i]) {
if (last == -1) last= i;
else {
ans.push_back({last, i});
last = -1;
}
}
}
assert(last == -1);
cout << friends << '\n';
for (auto& [x, y] : ans) {
cout << x+1 << " " << y+1 << '\n';
}
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3628kb
input:
3 2 0 0 0 1 1 0 1 1 2 0 0 0 1 0 2 0 3 2 0 0 1 1 2 2 3 3
output:
2 2 4 3 1 2 1 2 3 4 0 1 2 3 4
result:
ok ok (3 test cases)
Test #2:
score: 0
Accepted
time: 81ms
memory: 3560kb
input:
10000 2 -107276936 -310501829 419434212 585811870 -65754386 -491212232 381152038 897148193 3 -474045168 493506332 299114415 540203303 165808153 983551 -506936261 -694189769 766718170 -725540031 975267148 -593051087 1 -818952276 -762387923 584023914 -612401389 6 -77701228 -266484128 659434465 6322062...
output:
0 1 2 3 4 0 1 2 3 4 5 6 0 1 2 0 1 2 3 4 5 6 7 8 9 10 11 12 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 0 1 2 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 0 1 2 3 4...
result:
ok ok (10000 test cases)
Test #3:
score: 0
Accepted
time: 77ms
memory: 3692kb
input:
10000 1 999855386 999580905 999342928 999615227 21 999601032 999015398 999155628 999176944 999309856 999524434 999121011 999509537 999323572 999685730 999272272 999769606 999450559 999390758 999632027 999178534 999024993 999463838 999784856 999374197 999980525 999366771 999241260 999516879 999599548...
output:
0 1 2 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 0 1 2 3 4 5 6 7...
result:
ok ok (10000 test cases)
Test #4:
score: 0
Accepted
time: 77ms
memory: 3704kb
input:
10000 5 999984799 999981445 999958394 999984217 999994978 999981258 999955539 999938710 999936554 999963561 999907222 999907508 999938166 999941959 999910567 999986887 999901446 999961092 999994730 999963038 5 999916115 999962400 999948250 999940355 999954204 999920844 999928148 999990369 999978118 ...
output:
0 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 0 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 0 1 2 3 4 5 6 7 8 9 10 1...
result:
ok ok (10000 test cases)
Test #5:
score: -100
Runtime Error
input:
10000 1 999990146 999993828 999995909 999996353 56 999999851 999991179 999997250 999997987 999990590 999997316 999997350 999996856 999997034 999996236 999999396 999996897 999991180 999993309 999991265 999995185 999993952 999994054 999990210 999994471 999993201 999995893 999997170 999998971 999998201...
output:
0 1 2 1 111 76 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 ...