QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#407981 | #6414. Classical Maximization Problem | grass8cow# | TL | 0ms | 13196kb | C++17 | 1.6kb | 2024-05-09 15:36:45 | 2024-05-09 15:36:46 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
vector<int>g[401000];int m;
map<int,int>fx,fy;
#define pb push_back
int X[400100],Y[401000],dfn[400100];
int U[401000],V[400100];bool vis[401000];
int ans,A[400100],B[401000],n1,n2;
void lj(int x,int y){
ans++,A[ans]=x,B[ans]=y,vis[x]=vis[y]=1;
}
#define pb push_back
void ad(int u,int v,int w){U[w]=u,V[w]=v,g[u].pb(w),g[v].pb(w);}
void dfs(int x,int o){
dfn[x]=++dfn[0];
vector<int>Z;
for(int i:g[x]){
int v=U[i]^V[i]^x;
if(!dfn[v]){
dfs(v,i);
if(!vis[i])Z.pb(i);
}
else if(dfn[x]<dfn[v])Z.pb(i);
}
while(Z.size()>=2){
int x=Z.back();Z.pop_back();
int y=Z.back();Z.pop_back();
lj(x,y);
}
if(o&&!Z.empty())lj(o,Z.back());
}
void sol(){
scanf("%d",&m);m*=2;
fx.clear(),fy.clear();
for(int i=1;i<=m;i++){
vis[i]=0;
scanf("%d%d",&X[i],&Y[i]);
if(!fx[X[i]])fx[X[i]]=++n1;
X[i]=fx[X[i]];
if(!fy[Y[i]])fy[Y[i]]=++n2;
Y[i]=fy[Y[i]];
}
dfn[0]=0;
for(int i=1;i<=n1+n2;i++)g[i].clear(),dfn[i]=0;
for(int i=1;i<=m;i++)ad(X[i],n1+Y[i],i);
ans=0;for(int i=1;i<=n1+n2;i++)if(!dfn[i])dfs(i,0);
printf("%d\n",ans);for(int i=1;i<=ans;i++)printf("%d %d\n",A[i],B[i]);
vector<int>Z;
for(int i=1;i<=m;i++)if(!vis[i])Z.pb(i);
while(Z.size()>=2){
int x=Z.back();Z.pop_back();
int y=Z.back();Z.pop_back();
printf("%d %d\n",x,y);
}
}
int main(){
int T;scanf("%d",&T);
while(T--)sol();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 13196kb
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 3 4 2 1 2 4 3 2 1 0 4 3 2 1
result:
ok ok (3 test cases)
Test #2:
score: -100
Time Limit Exceeded
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 4 3 2 1 0 6 5 4 3 2 1 0 2 1 0 12 11 10 9 8 7 6 5 4 3 2 1 0 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 2 1 0 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 6 5 4 3...