Submission #1328580

#TimeUsernameProblemLanguageResultExecution timeMemory
1328580PieArmyParking (CEOI22_parking)C++20
20 / 100
164 ms19596 KiB
#include<bits/stdc++.h>
typedef long long ll;
#define pb push_back
#define fr first
#define sc second
#define endl '\n'
using namespace std;
#define mid ((left+right)>>1)

int n,m;
vector<int>park[200023];
pair<int,int>loc[200023];
set<int>st,ustte,kalan;
vector<pair<int,int>>ans;

void move(int x,int y){
	if(x!=y){
		ans.pb({x,y});
		int renk=park[x].back();
		park[x].pop_back();
		park[y].pb(renk);
		if(loc[renk].fr==x)loc[renk].fr=y;
		else loc[renk].sc=y;
		if(park[y].size()==1)st.erase(y);
		if(park[x].size()==0)st.insert(x);
		if(park[y].size()==2){
			kalan.erase(park[y].back());
		}

		if(park[y].size()!=0){
			ustte.erase(park[y][0]);
			renk=park[y].back();
			if(loc[renk].fr!=loc[renk].sc&&
				park[loc[renk].fr].back()==renk&&park[loc[renk].sc].back()==renk&&
				park[loc[renk].fr].size()==2&&park[loc[renk].sc].size()==2){
				ustte.insert(renk);
			}
		}
	}
	if(park[x].size()==1){
		int renk=park[x].back();
		if(park[loc[renk].fr].back()==renk&&park[loc[renk].sc].back()==renk){
			if(loc[renk].fr!=x){
				move(loc[renk].fr,x);
			}
			else move(loc[renk].sc,x);
		}
	}
	if(park[y].size()==1){
		int renk=park[y].back();
		if(park[loc[renk].fr].back()==renk&&park[loc[renk].sc].back()==renk){
			if(loc[renk].fr!=y){
				move(loc[renk].fr,y);
			}
			else move(loc[renk].sc,y);
		}
	}
}

signed main(){
	ios_base::sync_with_stdio(23^23);cin.tie(0);
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		int x,y;cin>>x>>y;
		if(!(x==y&&x!=0)){
			if(x)kalan.insert(x);
			if(y)kalan.insert(y);
		}
		loc[x].fr=i;
		swap(loc[x].fr,loc[x].sc);
		loc[y].fr=i;
		swap(loc[y].fr,loc[y].sc);
		if(x)park[i].pb(x);
		if(y)park[i].pb(y);
		if(x==0&&y==0)st.insert(i);
	}
	for(int i=1;i<=m;i++){
		if(!park[i].size())continue;
		int renk=park[i].back();
		if(loc[renk].fr!=loc[renk].sc&&
			park[loc[renk].fr].back()==renk&&park[loc[renk].sc].back()==renk&&
			park[loc[renk].fr].size()==2&&park[loc[renk].sc].size()==2){
			ustte.insert(renk);
		}
		move(i,i);
	}
	while(kalan.size()){
		if(ustte.size()==0){
			if(!st.size()){
				cout<<-1<<endl;
				return 0;
			}
			move(loc[*kalan.begin()].fr,*st.begin());
		}
		else{
			if(!st.size()){
				cout<<-1<<endl;
				return 0;
			}
			move(loc[*ustte.begin()].fr,*st.begin());
		}
	}
	cout<<ans.size()<<endl;
	for(int i=0;i<ans.size();i++){
		cout<<ans[i].fr<<" "<<ans[i].sc<<endl;
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...