QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#605818 | #9412. Stop the Castle | zqx | AC ✓ | 95ms | 188380kb | C++23 | 5.0kb | 2024-10-02 19:49:42 | 2024-10-02 19:49:42 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
struct Dinic {
long long INF = 1e18;
int SIZ = 5e5 + 3;
int n, m;
int H[7000005], V[7000005], N[7000005], F[7000005], t = 1;
int add(int u, int v, int f) {
V[++ t] = v, N[t] = H[u], F[t] = f, H[u] = t;
V[++ t] = u, N[t] = H[v], F[t] = 0, H[v] = t;
n = max(n, u);
n = max(n, v);
return t;
}
void clear() {
for (int i = 1; i <= n; ++ i)
H[i] = 0;
n = m = 0, t = 1;
}
int D[7000005];
bool bfs(int s, int t) {
queue <int> Q;
for (int i = 1; i <= n; ++ i)
D[i] = 0;
Q.push(s), D[s] = 1;
while (!Q.empty()) {
int u = Q.front();
Q.pop();
for (int i = H[u]; i; i = N[i]) {
const int &v = V[i];
const int &f = F[i];
if (f != 0 && !D[v]) {
D[v] = D[u] + 1;
Q.push(v);
}
}
}
return D[t] != 0;
}
int C[7000005];
long long dfs(int s, int t, int u, long long maxf) {
if (u == t)
return maxf;
long long totf = 0;
for (int &i = C[u]; i; i = N[i]) {
const int &v = V[i];
const int &f = F[i];
if (D[v] == D[u] + 1) {
long long resf = dfs(s, t, v, min(maxf, 1ll * f));
totf += resf;
maxf -= resf;
F[i ] -= resf;
F[i ^ 1] += resf;
if (maxf == 0)
return totf;
}
}
return totf;
}
long long dinic(int s, int t) {
long long ans = 0;
while (bfs(s, t)) {
memcpy(C, H, sizeof(H));
ans += dfs(s, t, s, INF);
}
return ans;
}
} a;
#define all(tar) tar.begin(),tar.end()
int n,m;
const int maxx=2e3+5;
int mp[maxx][maxx];//0无 1防御塔 2障碍
vector<int>num;
int get_pos(int x){
return lower_bound(all(num),x)-num.begin()+1;
}
pair<int,int>p1[maxx],p2[maxx];
int id[maxx][maxx][2],idx;
int G1[maxx][maxx],G2[maxx][maxx];
void solve(){
a.clear();
num.clear();
idx=0;
cin>>n;
for(int i=1;i<=n;i++){
cin>>p1[i].first>>p1[i].second;
num.push_back(p1[i].first);
num.push_back(p1[i].second);
num.push_back(p1[i].first+1);
num.push_back(p1[i].second+1);
}
cin>>m;
int s=++idx,t=++idx;
for(int i=1;i<=m;i++){
cin>>p2[i].first>>p2[i].second;
num.push_back(p2[i].first);
num.push_back(p2[i].second);
num.push_back(p2[i].first+1);
num.push_back(p2[i].second+1);
}
sort(all(num));
int len=num.size();
num.erase(unique(all(num)),num.end());
for(int i=1;i<=len;i++){
for(int j=1;j<=len;j++){
G1[i][j]=G2[i][j]=mp[i][j]=0;
}
}
for(int i=1;i<=n;i++){
int x=get_pos(p1[i].first);
int y=get_pos(p1[i].second);
mp[x][y]=2;
}
for(int i=1;i<=m;i++){
int x=get_pos(p2[i].first);
int y=get_pos(p2[i].second);
mp[x][y]=1;
}
int ans=0;
for(int i=1;i<=len;i++){
for(int j=1;j<=len;j++){
id[i][j][0]=++idx;
id[i][j][1]=++idx;
a.add(s,id[i][j][0],1);
a.add(id[i][j][1],t,1);
if(mp[i][j]==2){
if(mp[i-1][j]==2||mp[i][j-1]==2){
cout<<"-1\n";
return ;
}
}
}
}
for(int i=1;i<=len;i++){
int pre=0;
for(int j=1;j<=len;j++){
if(mp[i][j]==2){
if(pre){
++ans;
for(int k=pre+1;k<j;k++){
G1[i][k]=pre;
}
}
pre=j;
}else if(mp[i][j]==1){
pre=0;
}
}
}
for(int i=1;i<=len;i++){
int pre=0;
for(int j=1;j<=len;j++){
if(mp[j][i]==2){
if(pre){
++ans;
for(int k=pre+1;k<j;k++){
G2[k][i]=pre;
}
}
pre=j;
}else if(mp[j][i]==1){
pre=0;
}
}
}
vector<array<int,3>>e;
int c=0;
for(int i=1;i<=len;i++){
for(int j=1;j<=len;j++){
if(G1[i][j]&&G2[i][j]){
e.push_back({i,j,a.t+1});
++c;
a.add(id[i][G1[i][j]][0],id[G2[i][j]][j][1],1);
}
}
}
int p=a.dinic(s,t);
//if(n==200)cout<<ans<<" "<<p<<" "<<c<<'\n';
cout<<ans-p<<'\n';
for(auto [x,y,z]:e){
if(a.F[z]==0)cout<<num[x-1]<<" "<<num[y-1]<<'\n',mp[x][y]=1;
}
for(int i=1;i<=len;i++){
int pre=0;
for(int j=1;j<=len;j++){
if(mp[i][j]==2){
if(pre){
cout<<num[i-1]<<" "<<num[pre]<<'\n';
}
pre=j;
}else if(mp[i][j]==1){
pre=0;
}
}
}
for(int i=1;i<=len;i++){
int pre=0;
for(int j=1;j<=len;j++){
if(mp[j][i]==2){
if(pre){
cout<<num[pre]<<" "<<num[i-1]<<'\n';
}
pre=j;
}else if(mp[j][i]==1){
pre=0;
}
}
}
}
int main() {
ios :: sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T;
cin>>T;
while(T--){
solve();
}
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 50256kb
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: 11ms
memory: 56264kb
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 20 12 34 18 29 38 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 27 41 29 26 44 4 8 1 21 15 1 32 9 0 0 0 0 6 23 10 35 34 12 11 23 44 29 21 24 46 0 3 20 30 43 25 31 17 0 -1 3 16 36 25 7 17 39 6 7 5 8 11 5 5 6 4 4 9 3 10
result:
ok ok 21 cases (21 test cases)
Test #3:
score: 0
Accepted
time: 95ms
memory: 136120kb
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 52 139 91 160 94 35 126 61 132 67 154 138 154 496 185 147 187 467 199 432 224 153 260 275 270 305 277 356 306 374 311 186 334 367 349 61 352 112 393 307 35 276 126 214 187 433 224 393 261 468 274 67 277 189 311 455 390 308 440 179 453 251 11 4 38 25 240 35 52 67 57 139 292 177 271 186 278 272 415...
result:
ok ok 2 cases (2 test cases)
Test #4:
score: 0
Accepted
time: 16ms
memory: 52832kb
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 89287395 441913072 142917372 582720392 263974835 468188690 543529670 152471389 773363571 482933266 142917373 582720391 142917373 598813561 647691664 598813561 7 808969359 818037531 808969359 386523246 808969359 891229782 808969360 354711322 92745430 358274214 469117951 762966373 59832704 871951216...
result:
ok ok 20 cases (20 test cases)
Test #5:
score: 0
Accepted
time: 71ms
memory: 124196kb
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 314305867 787982418 350635050 435860426 350635050 811404535 407380694 797458033 469496529 342102981 469496529 438907614 469496529 732085440 469496529 816914009 469496529 837963933 495537855 461654555 496813081 699021890 496813081 787982418 496813081 815487777 496813081 857896659 520115644 751818...
result:
ok ok 2 cases (2 test cases)
Test #6:
score: 0
Accepted
time: 91ms
memory: 124428kb
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 145262472 903492853 197728547 409828915 246178069 291882089 291242118 564718673 365329221 136367504 365329221 211655411 365329221 405860533 365329221 657489855 365329221 773451933 365329221 845872655 382558552 984789430 385064292 502303728 391226954 843867422 410833945 564718673 412106895 135814...
result:
ok ok 2 cases (2 test cases)
Test #7:
score: 0
Accepted
time: 75ms
memory: 128476kb
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 31134588 231445627 33207011 541362608 46425938 755417826 250896470 264208611 295641139 541362608 333807226 762642201 340743563 288390629 341366249 231445627 360971009 755417826 417168695 231445627 417966308 381919591 418600362 755417826 429088204 541362608 509798823 541362608 509798823 741119775...
result:
ok ok 2 cases (2 test cases)
Test #8:
score: 0
Accepted
time: 15ms
memory: 50044kb
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 378192988 147499873 378192988 197547598 378192988 343857832 378192988 576579018 783332532 147499873 783332532 197547598 783332532 343857832 783332532 576579018 378192989 147499872 449924899 147499872 592684637 147499872 378192989 576579017 378192989 837331700 449924899 837331700 592684637 8373317...
result:
ok ok 20 cases (20 test cases)
Test #9:
score: 0
Accepted
time: 61ms
memory: 133736kb
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 24820918 6255307 43705451 11610914 45500146 23366504 58265855 28938721 112263159 31914492 126496650 36029445 137574156 66226935 140089445 84152868 156036789 132710904 170794245 149083019 176576709 174305881 186774359 180413073 194114590 195080121 222368048 231772824 237249112 234663336 250839457...
result:
ok ok 2 cases (2 test cases)
Test #10:
score: 0
Accepted
time: 86ms
memory: 188380kb
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 279200011 293316179 347252805 363531956 446662965 376231503 496057717 383623456 514655989 390555088 523731402 441004197 538628510 490065400 562810007 540189713 630132600 545422987 631858791 565468975 643299496 646360861 754008138 861149655 782710197 901945895 2691939 27674657 2691939 69029841 26...
result:
ok ok 2 cases (2 test cases)
Test #11:
score: 0
Accepted
time: 27ms
memory: 93692kb
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