QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#472488#6414. Classical Maximization ProblemUESTC_xxx#TL 0ms28168kbC++143.9kb2024-07-11 16:44:362024-07-11 16:44:37

Judging History

你现在查看的是最新测评结果

  • [2024-07-11 16:44:37]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:28168kb
  • [2024-07-11 16:44:36]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<set>
#include<vector>
#include<map>
#define ll long long
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=2e5+5;
struct d{
	int x,y,id;
}a[maxn];
struct edge{
	int to,next;
}e[maxn*10];
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;}
set<d,cmp1>s1[maxn];
set<d,cmp2>s2[maxn];
set<d,cmp1>::iterator it,it3;
set<d,cmp2>::iterator it2,it4;
int Ans,flag[maxn],n,du[maxn],b[maxn],c[maxn];
int pr[maxn][2],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;
	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].insert(a[i]);
		s2[a[i].y].insert(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){
		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){
		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=s1[a[To].x].lower_bound(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];
		}
		s1[a[To].x].erase(a[To]);
		it2=s2[a[To].y].lower_bound(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];
		}
		s2[a[To].y].erase(a[To]);
		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<=n*2;++i){
		if(!flag[i]){
			dfs(i,0,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: 0ms
memory: 28168kb

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 3
4 2
2
1 2
4 3
0
1 2
3 4

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
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: