/*
author: honglan0301
Sexy_goodier _ xiaoqing
*/
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cctype>
#include <queue>
#include <map>
#include <unordered_map>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <cmath>
#include <random>
#include <set>
#include <bitset>
#include <assert.h>
using namespace std;
//namespace Fread{const int SIZE=1<<20;char buf[SIZE],*S,*T;inline char getchar(){if(S==T){T=(S=buf)+fread(buf,1,SIZE,stdin);if(S==T)return'\n';}return*S++;}}using namespace Fread;namespace Fwrite{const int SIZE=1<<20;char buf[SIZE],*S=buf,*T=buf+SIZE;inline void flush(){fwrite(buf,1,S-buf,stdout);S=buf;}inline void putchar(char c){*S++=c;if(S==T)flush();}struct NTR{~NTR(){flush();}}ztr;}using namespace Fwrite;
//#define getchar Fread::getchar
//#define putchar Fwrite::putchar
namespace Fastio{struct Reader{template<typename T>Reader&operator>>(T&x){x=0;short f=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+(c^48),c=getchar();x*=f;return*this;}Reader&operator>>(double&x){x=0;double t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();else{x*=f;return*this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return*this;}Reader&operator>>(long double&x){x=0;long double t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();else{x*=f;return*this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return*this;}Reader&operator>>(__float128&x){x=0;__float128 t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();else{x*=f;return*this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return*this;}Reader&operator>>(char&c){c=getchar();while(c==' '||c=='\n'||c=='\r')c=getchar();return*this;}Reader&operator>>(char*str){int len=0;char c=getchar();while(c==' '||c=='\n'||c=='\r')c=getchar();while(c!=' '&&c!='\n'&&c!='\r')str[len++]=c,c=getchar();str[len]='\0';return*this;}Reader&operator>>(string&str){str.clear();char c=getchar();while(c==' '||c=='\n'||c=='\r')c=getchar();while(c!=' '&&c!='\n'&&c!='\r')str.push_back(c),c=getchar();return*this;}Reader(){}}cin;const char endl='\n';struct Writer{const int Setprecision=6;typedef int mxdouble;template<typename T>Writer&operator<<(T x){if(x==0){putchar('0');return*this;}if(x<0)putchar('-'),x=-x;static short sta[40];short top=0;while(x>0)sta[++top]=x%10,x/=10;while(top>0)putchar(sta[top]+'0'),top--;return*this;}Writer&operator<<(double x){if(x<0)putchar('-'),x=-x;mxdouble _=x;x-=(double)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<Setprecision;i++)x*=10;_=x;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<Setprecision-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return*this;}Writer&operator<<(long double x){if(x<0)putchar('-'),x=-x;mxdouble _=x;x-=(long double)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<Setprecision;i++)x*=10;_=x;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<Setprecision-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return*this;}Writer&operator<<(__float128 x){if(x<0)putchar('-'),x=-x;mxdouble _=x;x-=(__float128)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<Setprecision;i++)x*=10;_=x;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<Setprecision-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return*this;}Writer&operator<<(char c){putchar(c);return*this;}Writer&operator<<(char*str){int cur=0;while(str[cur])putchar(str[cur++]);return*this;}Writer&operator<<(const char*str){int cur=0;while(str[cur])putchar(str[cur++]);return*this;}Writer&operator<<(string str){int st=0,ed=str.size();while(st<ed)putchar(str[st++]);return*this;}Writer(){}}cout;}using namespace Fastio;
#define cin Fastio::cin
#define cout Fastio::cout
#define endl Fastio::endl//;fflush(stdout)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define int long long
#define ll long long
#define ull unsigned long long
#define mod 998244353
#define G 3
#define Gi 332748118
mt19937 rnd(time(0));
mt19937_64 rndl(time(0));
int n,m,a[200005][3],ans,cnt[200005],tot[200005];
pair<int,int> wz[200005][2];
vector <pair<int,int>> ansc;
set <int> kk;
int geto(int k,int x)
{
if(wz[k][0].fi==x) return 0; else return 1;
}
void cz(int x,int y)
{
ans++;
ansc.pb(mp(x,y));
int ys=a[x][tot[x]-1],op=geto(ys,x);
a[x][tot[x]-1]=0;
wz[ys][op]=mp(y,tot[y]);
a[x][tot[x]-1]=0;
tot[x]--; tot[y]++;
a[y][tot[y]-1]=ys;
if(tot[x]==0) kk.insert(x);
if(tot[y]==1) kk.erase(y);
}
void solve00(int x)
{
if(a[x][0]==a[x][1]) return;
if(!a[x][0]) return;
pair<int,int> ano=wz[a[x][0]][geto(a[x][0],x)^1];
if(!a[ano.fi][ano.se+1])
{
cz(ano.fi,x); solve00(ano.fi);
}
}
void solve0()
{
for(int i=1;i<=m;i++)
{
if(a[i][0]==a[i][1]) continue;
if(a[i][0]&&!a[i][1])
{
pair<int,int> ano=wz[a[i][0]][geto(a[i][0],i)^1];
//cout<<"F "<<a[i][0]<<" "<<ano.fi<<" "<<ano.se<<endl;
if(!a[ano.fi][ano.se+1])
{
solve00(i);
}
}
}
}
int ck()
{
for(int i=1;i<=m;i++) if(a[i][0]!=a[i][1]) return 0; return 1;
}
int solve10(int x,int op)
{
if(!a[x][op]) return 0;
//if(x==0) return 0;
//cout<<x<<" "<<op<<endl;
if(op==1)
{
pair<int,int> ano=wz[a[x][1]][geto(a[x][1],x)^1];
if(ano.se==0)
{
// if(!a[ano.fi][1]) exi
int na=solve10(ano.fi,1); cz(x,ano.fi); return na;
}
else
{
if(!kk.size()) exit(1);
int nr=(*kk.begin());
cz(x,nr); cz(ano.fi,nr); return ano.fi; //solve10(ano.fi,0);
}
}
else
{
pair<int,int> ano=wz[a[x][0]][geto(a[x][0],x)^1];
if(ano.se==0)
{
int na=0; if(tot[ano.fi]>1) na=solve10(ano.fi,1); cz(ano.fi,x); return na;
}
else
{
cz(ano.fi,x); return ano.fi;//solve10(ano.fi,0);
}
}
}
void solve1()
{
for(int i=1;i<=m;i++)
{
if(a[i][0]&&!a[i][1])
{
//cout<<i<<endl;
int nr=i;
while(1)
{
nr=solve10(nr,0);
if(!nr) break;
}
//solve10(i,0);
}
}
}
int fa[200005],val[200005],sz[200005];
int getfa(int x) {return fa[x]==x?x:fa[x]=getfa(fa[x]);}
void merge(int x,int y)
{
if((x=getfa(x))==(y=getfa(y))) return;
val[y]+=val[x]; sz[y]+=sz[x]; fa[x]=y;
}
void solve30(int x)
{
//cout<<"G "<<x<<endl; return;
int nr=(*kk.begin());
cz(x,nr);
nr=x;
while(1)
{
nr=solve10(nr,0);
if(!nr) break;
}
}
void solve31(int x)
{
//ans+=sz[x]+max(val[x],1ll);
// return;
//if(val[x]==1) return;
while(1)
{
pair<int,int> ano=wz[a[x][1]][geto(a[x][1],x)^1];
if(ano.se==1) break;
x=ano.fi;
}
pair<int,int> ano=wz[a[x][1]][geto(a[x][1],x)^1];
pair<int,int> tano=wz[a[x][0]][geto(a[x][0],x)^1];
if(tano.se==0) x=ano.fi;
ano=wz[a[x][1]][geto(a[x][1],x)^1];
int nr=(*kk.begin());
//cout<<kk.size()<<" "<<x<<" "<<ano.fi<<endl;
cz(x,nr); cz(ano.fi,nr);
//cout<<a[x][0]<<" "<<a[ano.fi][0]<<" "<<kk.size()<<endl;
//solve1(); exit(0);
nr=x;
while(1)
{
nr=solve10(nr,0);
//cout<<"G "<<nr<<" "<<kk.size()<<" "<<a[nr][0]<<" "<<a[nr][1]<<endl;
if(!nr) break;
}
}
signed main()
{
//freopen("parking.in","r",stdin);
cin>>n>>m;
for(int i=1;i<=m;i++)
{
cin>>a[i][0]>>a[i][1];
if(a[i][0]) wz[a[i][0]][cnt[a[i][0]]++]=mp(i,0),tot[i]++;
if(a[i][1]) wz[a[i][1]][cnt[a[i][1]]++]=mp(i,1),tot[i]++;
if(!a[i][0]&&!a[i][1]) kk.insert(i);
}
solve0();
if(ck())
{
cout<<ans<<endl;
for(auto i:ansc) cout<<i.fi<<" "<<i.se<<endl; return 0;
}
else if(!kk.size())
{
cout<<"-1"<<endl; return 0;
}
solve1();
if(ck())
{
cout<<ans<<endl;
for(auto i:ansc) cout<<i.fi<<" "<<i.se<<endl; return 0;
}
/*cout<<ans<<endl;
for(auto i:ansc) cout<<i.fi<<" "<<i.se<<endl;
for(int i=1;i<=m;i++)
{
cout<<"G "<<i<<" "<<a[i][0]<<" "<<a[i][1]<<endl;
}*/
int mx=1;
for(int i=1;i<=m;i++) fa[i]=i,sz[i]=1;
for(int i=1;i<=n;i++)
{
if(wz[i][0].fi==wz[i][1].fi) continue;
merge(wz[i][0].fi,wz[i][1].fi);
if(wz[i][0].se==1&&wz[i][1].se==1) val[getfa(wz[i][0].fi)]++;
}
for(int i=1;i<=m;i++)
{
if(getfa(i)!=i) continue;
if(sz[i]==1) continue;
mx=max(mx,min(val[i],2ll));
//ans+=sz[i]+max(val[i],1ll);
}
if(n+mx>m)
{
cout<<"-1"<<endl; return 0;
}
for(int i=1;i<=m;i++)
{
if(getfa(i)!=i) continue;
if(sz[i]==1) continue;
mx=max(mx,min(val[i],2ll));
if(!val[i]) solve30(i);
else solve31(i);
}
cout<<ans<<endl;
for(auto i:ansc) cout<<i.fi<<" "<<i.se<<endl; return 0;
/*for(int i=1;i<=m;i++)
{
cout<<"G "<<i<<" "<<a[i][0]<<" "<<a[i][1]<<endl;
}*/
}剪贴板
剪贴板 86o92yfj
剪贴板信息
剪贴板信息
- 收录
- 更新