QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#624909 | #9412. Stop the Castle | gaofeng | AC ✓ | 18ms | 16584kb | C++23 | 5.0kb | 2024-10-09 16:52:26 | 2024-10-09 16:52:26 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>PII;
typedef pair<PII,PII>PIII;
const int N=2e5+10, M = N;
const int mod=998244353;
const int INF = 0x3f3f3f3f;
const ll INFll = 0x3f3f3f3f3f3f3f3f;
#define endl "\n"
#define x first
#define y second
PIII L[N], R[N];
int lcnt, rcnt;
int S, T;
int h[N], ne[M], e[M], f[M], idx;
int d[N],cur[N];
bool st[N];
PII res[N];
void clear() {
// cout << S << " " << T << endl;
for(int i = 0; i < N; i ++) {
h[i] = -1;
ne[i] = e[i] = f[i] = d[i] = cur[i] = 0;
st[i] = false;
R[i] = L[i] = {{0,0},{0,0}};
res[i] = {0,0};
}
S = 0, T = 0;
lcnt = rcnt = idx = 0;
}
void print(PIII t) {
cout << t.x.x << " " << t.x.y << " | " << t.y.x << " " << t.y.y << endl;
}
PII pd2(PIII a, PIII b) {
int resx = 0, resy = 0;
if(b.first.x < a.first.x && a.first.x < b.second.x) resx = a.first.x;
if(a.first.y < b.first.y && b.first.y < a.second.y) resy = b.first.y;
return {resx, resy};
}
void add(int a, int b, int c) {
e[idx] = b, f[idx] = c, ne[idx] = h[a], h[a] = idx ++;
e[idx] = a, f[idx] = 0, ne[idx] = h[b], h[b] = idx ++;
}
bool bfs() {
// memset(d, -1, sizeof(int) * (T + 10));
memset(d, -1, sizeof d);
queue<int> q;
q.push(S), d[S] = 0, cur[S] = h[S];
while(q.size()) {
int u = q.front(); q.pop();
for(int i = h[u]; ~i; i = ne[i]) {
int v = e[i];
if(d[v] == -1 && f[i]) {
d[v] = d[u] + 1;
cur[v] = h[v];
if(v == T) return true;
q.push(v);
}
}
}
return false;
}
int find(int u, int limit) {
if(u == T) return limit;
int flow = 0;
for(int i = cur[u]; ~i && flow < limit; i = ne[i]) {
int v = e[i];
cur[u] = i;
if(d[v] == d[u] + 1 && f[i]) {
int t = find(v, min(f[i], limit - flow));
if(!t) cur[u] = -1;
f[i] -= t, f[i ^ 1] += t, flow += t;
}
}
return flow;
}
ll dinic() {
ll ans = 0, flow;
while(bfs())
if(flow = find(S, INF))
ans += flow;
return ans;
}
void solve()
{
unordered_map<int,vector<PII>> row, col;
row.clear();
col.clear();
int n; cin >> n;
for(int i = 1; i <= n; i ++) {
int x, y; cin >> x >> y;
row[x].push_back({y,1});
col[y].push_back({x,1});
}
int m; cin >> m;
for(int i = 1; i <= m; i ++) {
int x, y; cin >> x >> y;
row[x].push_back({y,0});
col[y].push_back({x,0});
}
bool ky = true;
for(auto &[i,t] : row) {
sort(t.begin(), t.end());
for(int j = 1; j < t.size(); j ++) {
if(t[j].second == 0 || t[j - 1].second == 0) continue;
if(t[j].first == t[j - 1].first + 1) ky = false;
L[++lcnt] = {{i, t[j - 1].first}, {i, t[j].first}};
}
}
for(auto &[i,t] : col) {
sort(t.begin(), t.end());
for(int j = 1; j < t.size(); j ++) {
if(t[j].second == 0 || t[j - 1].second == 0) continue;
if(t[j].first == t[j - 1].first + 1) ky = false;
R[++rcnt] = {{t[j - 1].first, i}, {t[j].first, i}};
}
}
if(!ky) {
cout << "-1\n";
return;
}
S = 0, T = lcnt + rcnt + 1;;
for(int i = 1; i <= lcnt; i ++) {
// print(L[i]);
add(S, i, 1);
}
for(int i = 1; i <= rcnt; i ++) {
// print(R[i]);
add(i + lcnt, T, 1);
}
for(int i = 1; i <= lcnt; i ++) {
for(int j = 1; j <= rcnt; j ++) {
PII T = pd2(L[i], R[j]);
if(T.x == 0 || T.y == 0) continue;
res[idx] = T;
add(i, j + lcnt, 1);
}
}
dinic();
vector<PII> ans;
// cout << lcnt << " " << T << endl;
for(int i = 0; i < idx; i += 2) {
int v = e[i], u = e[i ^ 1];
if(0 < u && u <= lcnt && lcnt < v && v < T && f[i ^ 1]) {
// cout << u << " " << v << " " << f[i ^ 1] << " " << endl;
// cout << res[i].x << " " << res[i].y << endl;
ans.push_back(res[i]);
st[u] = st[v] = true;
}
}
for(int i = 1; i <= lcnt; i ++) {
if(!st[i]) {
ans.push_back({L[i].x.x, L[i].x.y + 1});
// cout << L[i].x.x << " " << L[i].x.y + 1 << endl;
}
}
for(int i = 1; i <= rcnt; i ++) {
if(!st[i + lcnt]) {
ans.push_back({R[i].x.x + 1, R[i].x.y});
// cout << R[i].x.x + 1 << " " << R[i].x.y << endl;
}
}
cout << ans.size() << endl;
for(auto [x, y] : ans) cout << x << " " << y << endl;
}
signed main()
{
memset(h, -1, sizeof h);
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cout << setprecision(11) << fixed;
int t;t=1;
cin>>t;
for(int i=1;i<=t;i++){
solve();
clear();
}
}
这程序好像有点Bug,我给组数据试试?
詳細信息
Test #1:
score: 100
Accepted
time: 6ms
memory: 16256kb
input:
4 7 1 3 6 6 4 7 2 1 6 3 4 1 2 6 3 3 4 6 4 3 1 2 1 1 2 2 0 3 1 1 1 3 3 3 1 1 2 3 1 1 1 3 2 3 0
output:
2 2 3 4 6 0 1 2 3 -1
result:
ok ok 4 cases (4 test cases)
Test #2:
score: 0
Accepted
time: 15ms
memory: 16360kb
input:
21 11 3 5 18 6 19 12 27 48 28 38 30 12 33 18 42 18 43 38 45 46 48 34 3 2 6 24 4 41 45 15 11 6 15 27 16 9 16 14 16 48 19 26 25 12 27 26 28 4 31 40 32 6 33 50 37 50 46 11 50 29 17 1 49 4 9 9 15 9 22 11 31 11 46 14 28 17 5 17 49 18 43 20 31 30 46 33 11 33 33 34 15 36 6 47 10 2 16 46 36 30 11 7 34 7 41 ...
output:
3 29 38 20 12 34 18 5 16 10 16 15 12 6 20 26 34 50 0 1 16 10 0 1 43 22 5 1 3 1 13 33 10 44 45 42 44 0 5 29 26 27 41 44 4 8 1 21 15 1 32 9 0 0 0 0 6 23 10 35 34 23 44 12 11 29 21 24 46 0 3 20 30 43 25 31 17 0 -1 3 16 36 25 7 17 39 6 8 9 5 5 7 3 6 4 2 11 3 10
result:
ok ok 21 cases (21 test cases)
Test #3:
score: 0
Accepted
time: 0ms
memory: 16324kb
input:
2 200 7 52 9 160 10 4 12 61 18 436 19 430 20 499 24 139 25 416 29 53 33 153 35 275 35 310 37 25 38 477 39 349 42 120 44 158 45 330 49 486 50 204 51 67 52 138 52 305 56 139 63 132 66 4 67 327 70 484 71 67 72 308 74 218 76 479 81 139 82 90 86 201 87 335 91 35 91 220 92 496 94 29 94 436 96 359 97 299 1...
output:
46 393 186 352 61 132 153 277 305 94 160 91 67 224 35 126 61 52 139 306 374 199 432 154 138 154 496 185 147 187 467 349 112 260 275 270 356 311 177 334 367 453 251 440 179 390 308 261 468 277 189 274 67 35 276 224 393 126 214 187 433 311 455 391 307 380 385 271 186 138 429 19 436 21 499 57 139 11 4 ...
result:
ok ok 2 cases (2 test cases)
Test #4:
score: 0
Accepted
time: 11ms
memory: 16556kb
input:
20 13 89287395 441913071 89287395 590314987 142917372 582720391 142917372 598813561 232930851 582720391 263974835 468188689 263974835 490702144 543529670 152471388 543529670 219371706 647691663 598813561 772865038 598813561 773363571 482933265 773363571 561453301 8 128947560 120560639 264980960 4742...
output:
8 773363571 482933266 543529670 152471389 263974835 468188690 142917372 582720392 89287395 441913072 142917373 598813561 647691664 598813561 142917373 582720391 7 808969359 818037531 808969359 386523246 808969359 891229782 59832704 871951216 92745430 358274214 469117951 762966373 808969360 354711322...
result:
ok ok 20 cases (20 test cases)
Test #5:
score: 0
Accepted
time: 6ms
memory: 16540kb
input:
2 184 35763790 435860426 152681166 443483111 261932524 745277126 261932524 787982418 314305867 342102981 314305867 522593781 314305867 747023891 314305867 811404535 314305867 816914009 314305867 857896659 319901983 797458033 321347896 342102981 332550928 902179526 343207116 914408792 350635050 15515...
output:
160 814024114 802057256 813653133 815487777 812959730 526518374 812959730 787982418 812959730 815487777 469496529 159320601 469496529 438907614 469496529 732085440 469496529 816914009 469496529 837963933 496813081 342102981 496813081 787982418 496813081 815487777 496813081 857896659 495537855 811404...
result:
ok ok 2 cases (2 test cases)
Test #6:
score: 0
Accepted
time: 0ms
memory: 16520kb
input:
2 193 78368109 329329995 87932514 657474465 87932514 903492853 94608574 845872655 103602204 88649154 124431127 405860533 145262472 108198774 145262472 907127202 154400439 136367504 165270984 773451933 184157521 409828915 197728547 291882089 197728547 609623645 211750768 843867422 246178069 108396139...
output:
145 843018587 657489855 798222630 650775261 385064292 480642952 382558552 984789430 459002742 726497712 291242118 564718673 946078033 789467242 391226954 779506686 416747507 855156543 365329221 136367504 365329221 211655411 365329221 405860533 365329221 657489855 365329221 773451933 365329221 843867...
result:
ok ok 2 cases (2 test cases)
Test #7:
score: 0
Accepted
time: 0ms
memory: 16296kb
input:
2 192 1075648 579875717 1075648 937648715 1393060 191616432 3957616 541362608 3957616 971879257 5415801 541362608 5887448 541362608 5887448 624842533 25767120 610618164 26193206 214214028 26193206 231445627 26727662 374314271 29550509 755417826 29550509 917592925 30033126 610618164 31134588 58188305...
output:
131 959874149 755417826 954450160 231445627 867509363 541362608 861709091 541362608 757703054 231445627 757703054 541362608 757703054 755417826 746663436 213352628 679286177 322666885 673815006 231445627 622701955 231445627 958316958 231445627 649040970 541362608 261816954 381919591 865222469 755417...
result:
ok ok 2 cases (2 test cases)
Test #8:
score: 0
Accepted
time: 18ms
memory: 16364kb
input:
20 14 378192988 147499872 378192988 837331700 449924898 147499872 449924898 837331700 592684636 147499872 592684636 837331700 783332532 147499872 783332532 837331700 378192988 197547597 783332532 197547597 378192988 343857831 783332532 343857831 378192988 576579017 783332532 576579017 2 449924898 19...
output:
15 783332532 147499873 783332532 197547598 783332532 343857832 783332532 576579018 378192988 147499873 378192988 197547598 378192988 343857832 378192988 576579018 378192989 576579017 378192989 837331700 449924899 837331700 592684637 837331700 378192989 147499872 449924899 147499872 592684637 1474998...
result:
ok ok 20 cases (20 test cases)
Test #9:
score: 0
Accepted
time: 0ms
memory: 16584kb
input:
2 200 8623893 3649468 8623893 989134714 24820918 3649468 24820918 989134714 43705451 3649468 43705451 989134714 45500146 3649468 45500146 989134714 58265855 3649468 58265855 989134714 112263159 3649468 112263159 989134714 126496650 3649468 126496650 989134714 137574156 3649468 137574156 989134714 14...
output:
249 940751563 987186358 837279973 942850783 875171159 887715271 836310042 868785127 824608515 790057179 824235612 662669999 806391318 949978130 756411639 624732370 688184299 556768926 673135534 551971959 687041819 535654097 670510137 522387718 668886829 488548916 807375696 457148969 607899528 438907...
result:
ok ok 2 cases (2 test cases)
Test #10:
score: 0
Accepted
time: 2ms
memory: 16232kb
input:
2 200 2691939 27674656 2691939 984614319 30225807 27674656 30225807 984614319 39388076 27674656 39388076 984614319 55883389 27674656 55883389 984614319 77258752 27674656 77258752 984614319 106874917 27674656 106874917 984614319 144383292 27674656 144383292 984614319 145360776 27674656 145360776 9846...
output:
216 643299496 243902652 631858791 490065400 630132600 293316179 562810007 383623456 347252805 390555088 754008138 540189713 446662965 376231503 538628510 206375958 496057717 257900116 523731402 441004197 279200011 545422987 514655989 565468975 782710197 363531956 2691939 27674657 2691939 69029841 26...
result:
ok ok 2 cases (2 test cases)
Test #11:
score: 0
Accepted
time: 4ms
memory: 16296kb
input:
2 200 1005937 7 3412691 7 5764908 7 7897785 7 8061992 7 12801501 7 18529696 7 22574605 7 25203351 7 34574958 7 34998216 7 40928451 7 49792193 7 55962320 7 59632864 7 62353373 7 62611196 7 65346591 7 71459320 7 71550121 7 72215989 7 82584263 7 86119549 7 96204862 7 96940713 7 97693112 7 102067050 7 1...
output:
199 1005938 7 3412692 7 5764909 7 7897786 7 8061993 7 12801502 7 18529697 7 22574606 7 25203352 7 34574959 7 34998217 7 40928452 7 49792194 7 55962321 7 59632865 7 62353374 7 62611197 7 65346592 7 71459321 7 71550122 7 72215990 7 82584264 7 86119550 7 96204863 7 96940714 7 97693113 7 102067051 7 105...
result:
ok ok 2 cases (2 test cases)
Extra Test:
score: 0
Extra Test Passed