#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19028 KB |
Output is correct |
2 |
Correct |
10 ms |
19028 KB |
Output is correct |
3 |
Correct |
9 ms |
19028 KB |
Output is correct |
4 |
Correct |
9 ms |
19028 KB |
Output is correct |
5 |
Correct |
11 ms |
19156 KB |
Output is correct |
6 |
Correct |
9 ms |
19120 KB |
Output is correct |
7 |
Correct |
9 ms |
19028 KB |
Output is correct |
8 |
Correct |
10 ms |
19028 KB |
Output is correct |
9 |
Correct |
9 ms |
19028 KB |
Output is correct |
10 |
Correct |
11 ms |
19156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
425 ms |
48720 KB |
Output is correct |
2 |
Correct |
510 ms |
48224 KB |
Output is correct |
3 |
Correct |
361 ms |
48856 KB |
Output is correct |
4 |
Correct |
341 ms |
48836 KB |
Output is correct |
5 |
Correct |
495 ms |
48988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
19156 KB |
Output is correct |
2 |
Correct |
11 ms |
19132 KB |
Output is correct |
3 |
Correct |
11 ms |
19156 KB |
Output is correct |
4 |
Correct |
12 ms |
19156 KB |
Output is correct |
5 |
Correct |
12 ms |
19156 KB |
Output is correct |
6 |
Correct |
10 ms |
19156 KB |
Output is correct |
7 |
Correct |
13 ms |
19284 KB |
Output is correct |
8 |
Correct |
11 ms |
19156 KB |
Output is correct |
9 |
Correct |
12 ms |
19156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
19156 KB |
Output is correct |
2 |
Correct |
11 ms |
19132 KB |
Output is correct |
3 |
Correct |
11 ms |
19156 KB |
Output is correct |
4 |
Correct |
12 ms |
19156 KB |
Output is correct |
5 |
Correct |
12 ms |
19156 KB |
Output is correct |
6 |
Correct |
10 ms |
19156 KB |
Output is correct |
7 |
Correct |
13 ms |
19284 KB |
Output is correct |
8 |
Correct |
11 ms |
19156 KB |
Output is correct |
9 |
Correct |
12 ms |
19156 KB |
Output is correct |
10 |
Correct |
596 ms |
44460 KB |
Output is correct |
11 |
Correct |
121 ms |
38920 KB |
Output is correct |
12 |
Correct |
160 ms |
39076 KB |
Output is correct |
13 |
Correct |
504 ms |
43424 KB |
Output is correct |
14 |
Correct |
179 ms |
39768 KB |
Output is correct |
15 |
Correct |
180 ms |
38996 KB |
Output is correct |
16 |
Correct |
638 ms |
44916 KB |
Output is correct |
17 |
Correct |
172 ms |
38876 KB |
Output is correct |
18 |
Correct |
548 ms |
44424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
19156 KB |
Output is correct |
2 |
Correct |
10 ms |
19156 KB |
Output is correct |
3 |
Correct |
12 ms |
19156 KB |
Output is correct |
4 |
Correct |
11 ms |
19156 KB |
Output is correct |
5 |
Correct |
10 ms |
19156 KB |
Output is correct |
6 |
Correct |
12 ms |
19156 KB |
Output is correct |
7 |
Correct |
11 ms |
19156 KB |
Output is correct |
8 |
Correct |
12 ms |
19156 KB |
Output is correct |
9 |
Correct |
12 ms |
19156 KB |
Output is correct |
10 |
Correct |
10 ms |
19156 KB |
Output is correct |
11 |
Correct |
12 ms |
19156 KB |
Output is correct |
12 |
Correct |
12 ms |
19156 KB |
Output is correct |
13 |
Correct |
10 ms |
19156 KB |
Output is correct |
14 |
Correct |
11 ms |
19156 KB |
Output is correct |
15 |
Correct |
10 ms |
19156 KB |
Output is correct |
16 |
Correct |
12 ms |
19156 KB |
Output is correct |
17 |
Correct |
12 ms |
19220 KB |
Output is correct |
18 |
Correct |
15 ms |
19248 KB |
Output is correct |
19 |
Correct |
12 ms |
19156 KB |
Output is correct |
20 |
Correct |
12 ms |
19156 KB |
Output is correct |
21 |
Correct |
9 ms |
19156 KB |
Output is correct |
22 |
Correct |
12 ms |
19156 KB |
Output is correct |
23 |
Correct |
11 ms |
19156 KB |
Output is correct |
24 |
Correct |
11 ms |
19156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19028 KB |
Output is correct |
2 |
Correct |
10 ms |
19028 KB |
Output is correct |
3 |
Correct |
9 ms |
19028 KB |
Output is correct |
4 |
Correct |
9 ms |
19028 KB |
Output is correct |
5 |
Correct |
11 ms |
19156 KB |
Output is correct |
6 |
Correct |
9 ms |
19120 KB |
Output is correct |
7 |
Correct |
9 ms |
19028 KB |
Output is correct |
8 |
Correct |
10 ms |
19028 KB |
Output is correct |
9 |
Correct |
9 ms |
19028 KB |
Output is correct |
10 |
Correct |
11 ms |
19156 KB |
Output is correct |
11 |
Correct |
425 ms |
48720 KB |
Output is correct |
12 |
Correct |
510 ms |
48224 KB |
Output is correct |
13 |
Correct |
361 ms |
48856 KB |
Output is correct |
14 |
Correct |
341 ms |
48836 KB |
Output is correct |
15 |
Correct |
495 ms |
48988 KB |
Output is correct |
16 |
Correct |
12 ms |
19156 KB |
Output is correct |
17 |
Correct |
11 ms |
19132 KB |
Output is correct |
18 |
Correct |
11 ms |
19156 KB |
Output is correct |
19 |
Correct |
12 ms |
19156 KB |
Output is correct |
20 |
Correct |
12 ms |
19156 KB |
Output is correct |
21 |
Correct |
10 ms |
19156 KB |
Output is correct |
22 |
Correct |
13 ms |
19284 KB |
Output is correct |
23 |
Correct |
11 ms |
19156 KB |
Output is correct |
24 |
Correct |
12 ms |
19156 KB |
Output is correct |
25 |
Correct |
596 ms |
44460 KB |
Output is correct |
26 |
Correct |
121 ms |
38920 KB |
Output is correct |
27 |
Correct |
160 ms |
39076 KB |
Output is correct |
28 |
Correct |
504 ms |
43424 KB |
Output is correct |
29 |
Correct |
179 ms |
39768 KB |
Output is correct |
30 |
Correct |
180 ms |
38996 KB |
Output is correct |
31 |
Correct |
638 ms |
44916 KB |
Output is correct |
32 |
Correct |
172 ms |
38876 KB |
Output is correct |
33 |
Correct |
548 ms |
44424 KB |
Output is correct |
34 |
Correct |
12 ms |
19156 KB |
Output is correct |
35 |
Correct |
10 ms |
19156 KB |
Output is correct |
36 |
Correct |
12 ms |
19156 KB |
Output is correct |
37 |
Correct |
11 ms |
19156 KB |
Output is correct |
38 |
Correct |
10 ms |
19156 KB |
Output is correct |
39 |
Correct |
12 ms |
19156 KB |
Output is correct |
40 |
Correct |
11 ms |
19156 KB |
Output is correct |
41 |
Correct |
12 ms |
19156 KB |
Output is correct |
42 |
Correct |
12 ms |
19156 KB |
Output is correct |
43 |
Correct |
10 ms |
19156 KB |
Output is correct |
44 |
Correct |
12 ms |
19156 KB |
Output is correct |
45 |
Correct |
12 ms |
19156 KB |
Output is correct |
46 |
Correct |
10 ms |
19156 KB |
Output is correct |
47 |
Correct |
11 ms |
19156 KB |
Output is correct |
48 |
Correct |
10 ms |
19156 KB |
Output is correct |
49 |
Correct |
12 ms |
19156 KB |
Output is correct |
50 |
Correct |
12 ms |
19220 KB |
Output is correct |
51 |
Correct |
15 ms |
19248 KB |
Output is correct |
52 |
Correct |
12 ms |
19156 KB |
Output is correct |
53 |
Correct |
12 ms |
19156 KB |
Output is correct |
54 |
Correct |
9 ms |
19156 KB |
Output is correct |
55 |
Correct |
12 ms |
19156 KB |
Output is correct |
56 |
Correct |
11 ms |
19156 KB |
Output is correct |
57 |
Correct |
11 ms |
19156 KB |
Output is correct |
58 |
Correct |
639 ms |
45372 KB |
Output is correct |
59 |
Correct |
585 ms |
44176 KB |
Output is correct |
60 |
Correct |
641 ms |
46276 KB |
Output is correct |
61 |
Correct |
577 ms |
43820 KB |
Output is correct |
62 |
Correct |
119 ms |
39624 KB |
Output is correct |
63 |
Correct |
521 ms |
43076 KB |
Output is correct |
64 |
Correct |
187 ms |
39752 KB |
Output is correct |
65 |
Correct |
591 ms |
43752 KB |
Output is correct |
66 |
Correct |
631 ms |
44868 KB |
Output is correct |
67 |
Correct |
159 ms |
38580 KB |
Output is correct |
68 |
Correct |
523 ms |
43692 KB |
Output is correct |
69 |
Correct |
503 ms |
42948 KB |
Output is correct |
70 |
Correct |
168 ms |
38872 KB |
Output is correct |
71 |
Correct |
175 ms |
39496 KB |
Output is correct |
72 |
Correct |
176 ms |
39148 KB |
Output is correct |
73 |
Correct |
575 ms |
44884 KB |
Output is correct |
74 |
Correct |
162 ms |
38820 KB |
Output is correct |
75 |
Correct |
581 ms |
44028 KB |
Output is correct |
76 |
Correct |
607 ms |
44524 KB |
Output is correct |
77 |
Correct |
553 ms |
43692 KB |
Output is correct |
78 |
Correct |
179 ms |
39176 KB |
Output is correct |
79 |
Correct |
558 ms |
43796 KB |
Output is correct |
80 |
Correct |
177 ms |
38744 KB |
Output is correct |
81 |
Correct |
541 ms |
44052 KB |
Output is correct |