This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
//#define endl '\n'
#define st first
#define nd second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ld long double
ll mod=1000000007;
int inf=1000000007;
ll infl=1000000000000000007;
const int N=200007;
int a[2*N];
set<int>id[2*N];
vector<pair<int,int>>ans;
set<int>fre;
set<int>one;
void check(int x) ;
void mov(int x,int y)
{
    fre.erase(y);
    ans.pb({x,y});
    if(a[2*x+1]>0)
    {
        one.insert(x);
        id[a[2*x+1]].erase(2*x+1);
        if(a[2*y]==0)
        {
            one.insert(y);
            a[2*y]=a[2*x+1];
            id[a[2*x+1]].insert(2*y);
        }
        else
        {
            one.erase(y);
            a[2*y+1]=a[2*x+1];
            id[a[2*x+1]].insert(2*y+1);
        }
        int t=a[2*x+1];
        a[2*x+1]=0;
        check(a[2*x]);
        check(t);
    }
    else
    {
        one.erase(x);
        fre.insert(x);
        id[a[2*x]].erase(2*x);
        if(a[2*y]==0)
        {
            one.insert(y);
            a[2*y]=a[2*x];
            id[a[2*x]].insert(2*y);
        }
        else
        {
            one.erase(y);
            a[2*y+1]=a[2*x];
            id[a[2*x]].insert(2*y+1);
        }
        int t=a[2*x];
        a[2*x]=0;
        check(t);
    }
}
void check(int x)
{
    int i=*id[x].begin(),j=*++id[x].begin();
    if(i%2==1&&j%2==0&&a[j+1]==0) mov(i/2,j/2);
    if(i%2==0&&j%2==1&&a[i+1]==0) mov(j/2,i/2);
    if(i%2==0&&j%2==0&&a[i+1]==0&&a[j+1]==0) mov(i/2,j/2);
}
void gg()
{
    cout<<-1;
    exit(0);
}
void check_one()
{
    while(sz(one)>0)
    {
        int x=*one.begin(),y;
        x*=2;
        bool top=0;
        while(true)
        {
            y=a[x];
            if(x%2==0)
            {
                if(top)
                {
                    if(sz(fre)==0) gg();
                    mov(x/2,*fre.begin());
                    break;
                }
                top=0;
            }
            else top=1;
            if(*id[y].begin()==x) x=*++id[y].begin();
            else x=*id[y].begin();
            x^=1;
        }
    }
}
int main()
{
  //  freopen("parking.in.3a","r",stdin);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=m;i++)
    {
        cin>>a[2*i]>>a[2*i+1];
        if(a[2*i]==0&&a[2*i+1]==0) fre.insert(i);
        else if(a[2*i+1]==0) one.insert(i);
        id[a[2*i]].insert(2*i);
        id[a[2*i+1]].insert(2*i+1);
    }
    //kasuje to co mo¿na od razu
    for(int i=1;i<=n;i++) check(i);
    //kasuje wszytsko co zaczyna siê od pojedynczego
    check_one();
    //teraz cykle z 2 gornymi elementami
   for(int i=1;i<=n;i++)
    {
        if(*id[i].begin()%2==1&&*++id[i].begin()%2==1)
        {
            if(sz(fre)==0) gg();
            mov(*id[i].begin()/2,*fre.begin());
            check_one();
        }
    }
    //teraz zwykle cykle
    for(int i=1;i<=m;i++)
    {
        if(a[2*i]>0&&a[2*i]!=a[2*i+1])
        {
            if(sz(fre)==0) gg();
            mov(i,*fre.begin());
        }
    }
     for(int i=1;i<=m;i++) if(a[2*i]!=a[2*i+1]) return 2137;
    cout<<sz(ans)<<endl;
    for(auto x:ans) cout<<x.st<<" "<<x.nd<<endl;
    return 0;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |