QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#472557 | #6414. Classical Maximization Problem | UESTC_xxx# | RE | 42ms | 17980kb | C++20 | 4.7kb | 2024-07-11 17:09:01 | 2024-07-11 17:09:02 |
Judging History
answer
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<set>
#include<vector>
#include<map>
#define ll long long
#pragma GCC optimize(2)
using namespace std;
//const long long inf=1e18;
inline int read(){
char ch=getchar();
int k=0,ty=1;
while(ch<'0'||ch>'9'){
if(ch=='-')ty=-1;
ch=getchar();
}
while(ch<='9'&&ch>='0'){
k=k*10+ch-'0';
ch=getchar();
}
return k*ty;
}
const int maxn=4e5+5;
struct d{
int x,y,id;
bool operator == (const d& A1){
return x==A1.x&&y==A1.y&&id==A1.id;
}
}a[maxn];
struct edge{
int to,next;
}e[maxn*20];
int cnt,h[maxn];
inline void add(int x,int y){
e[++cnt]=(edge){y,h[x]};
h[x]=cnt;
}
//struct cmp1 {
// bool operator() (const d& A1, const d& A2) const {
// return A1.y<A2.y;
// }
//};
//struct cmp2 {
// bool operator() (const d& A1, const d& A2) const {
// return A1.x<A2.x;
// }
//};
bool cmp1(d& A1,d& A2){return A1.y<A2.y;}
bool cmp2(d A1,d& A2){return A1.x<A2.x;}
vector<d>s1[maxn];
vector<d>s2[maxn];
vector<d>::iterator it,it2,it3,it4;
int Ans,flag[maxn],n,du[maxn],b[maxn],c[maxn];
int pr[maxn][4],flag2[maxn],ans;
queue<int>q;
void dfs(int x,int fa,int dep){
flag[x]=1;
if(dep==1){
++ans;
pr[ans][0]=fa;
pr[ans][1]=x;
flag2[x]=1;
flag2[fa]=1;
}
for(int i=h[x];i;i=e[i].next){
int y=e[i].to;
if(flag[y])continue;
dfs(y,x,dep^1);
}
}
void Solve(){
n=read();
cnt=0;b[0]=0;c[0]=0;
for(int i=1;i<=n*2;++i){
h[i]=0;du[i]=0;flag[i]=0;flag2[i]=0;
a[i].x=read();a[i].y=read();
a[i].id=i;
b[++b[0]]=a[i].x;
c[++c[0]]=a[i].y;
}
sort(b+1,b+1+b[0]);
b[0]=unique(b+1,b+1+b[0])-b-1;
sort(c+1,c+1+c[0]);
c[0]=unique(c+1,c+1+c[0])-c-1;
for(int i=1;i<=b[0];++i)s1[i].clear();
for(int i=1;i<=c[0];++i)s2[i].clear();
for(int i=1;i<=n*2;++i){
a[i].x=lower_bound(b+1,b+1+b[0],a[i].x)-b;
a[i].y=lower_bound(c+1,c+1+c[0],a[i].y)-c;
s1[a[i].x].push_back(a[i]);
s2[a[i].y].push_back(a[i]);
}
// for(int i=1;i<=n*2;++i){
// cout<<a[i].x<<' '<<a[i].y<<'\n';
// }
for(int i=1;i<=b[0];++i){
sort(s1[i].begin(),s1[i].end(),cmp1);
int last=0,now=0;
if(s1[i].size()>1){
it=s1[i].begin();
last=(*it).id;
++it;
while(it!=s1[i].end()){
now=(*it).id;
add(last,now);add(now,last);
++du[last];++du[now];
last=now;++it;
}
}
}
for(int i=1;i<=c[0];++i){
sort(s2[i].begin(),s2[i].end(),cmp2);
int last=0,now=0;
if(s2[i].size()>1){
it2=s2[i].begin();
last=(*it2).id;
++it2;
while(it2!=s2[i].end()){
now=(*it2).id;
add(last,now);add(now,last);
++du[last];++du[now];
last=now;++it2;
}
}
}
// cout<<"FAF\n";
// cout<<"FAF\n";
for(int i=1;i<=n*2;++i){
if(du[i]==1)q.push(i);
}
ans=0;
while(!q.empty()){
int x=q.front();
q.pop();
if(flag[x])continue;
flag[x]=1;
int To=0;
for(int i=h[x];i;i=e[i].next){
int y=e[i].to;
if(flag[y])continue;
To=y;
}
flag[To]=1;++ans;
pr[ans][0]=x;pr[ans][1]=To;
flag2[x]=1;flag2[To]=1;
it=find(s1[a[To].x].begin(),s1[a[To].x].end(),a[To]);
it3=it;++it;
if(it3!=s1[a[To].x].begin()&&it!=s1[a[To].x].end()){
--it3;
add((*it3).id,(*it).id);
add((*it).id,(*it3).id);
++du[(*it3).id];++du[(*it).id];
}
--it;
s1[a[To].x].erase(it);
it2=find(s2[a[To].y].begin(),s2[a[To].y].end(),a[To]);
it4=it2;++it2;
if(it4!=s2[a[To].y].begin()&&it2!=s2[a[To].y].end()){
--it4;
add((*it4).id,(*it2).id);
add((*it2).id,(*it4).id);
++du[(*it4).id];++du[(*it2).id];
}
--it2;
s2[a[To].y].erase(it2);
for(int i=h[To];i;i=e[i].next){
int y=e[i].to;
if(flag[y])continue;
--du[y];
if(du[y]==1)q.push(y);
}
}
for(int i=1;i<=b[0];++i){
it=s1[i].begin();
int last=0,id=0;
while(it!=s1[i].end()){
id=(*it).id;++it;
if(flag[id])continue;
if(last==0)last=id;
else {
++ans;
pr[ans][0]=last;pr[ans][1]=id;
flag[last]=1;flag[id]=1;
flag2[last]=1;flag2[id]=1;
last=0;
}
}
}
for(int i=1;i<=c[0];++i){
it2=s2[i].begin();
int last=0,id=0;
while(it2!=s2[i].end()){
id=(*it2).id;++it2;
if(flag[id])continue;
if(last==0)last=id;
else {
++ans;
pr[ans][0]=last;pr[ans][1]=id;
flag[last]=1;flag[id]=1;
flag2[last]=1;flag2[id]=1;
last=0;
}
}
}
cout<<ans<<'\n';
for(int i=1;i<=ans;++i)cout<<pr[i][0]<<' '<<pr[i][1]<<'\n';
int now=0;
for(int i=1;i<=n*2;++i){
if(!flag2[i]){
if(now==0)cout<<i<<' ';
else cout<<i<<'\n';
now^=1;
}
}
}
int main(){
int T=read();
while(T--)Solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 17824kb
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 1 2 3 4 2 1 2 4 3 0 1 2 3 4
result:
ok ok (3 test cases)
Test #2:
score: 0
Accepted
time: 40ms
memory: 15924kb
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: 42ms
memory: 15896kb
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: 35ms
memory: 17980kb
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 76 111 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 ...