/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
const int N = 1e6+100, M = 1e5+10, K = 52, MX = 30;
int n, m, A[N], B[N], AA = MOD;
vector<pair<int, int>> ans;
vector<vector<int>> v;
array<int, 2> col[N][2];
void solve(){
cin >> n >> m;
v.resize(m);
for(int i = 0; i < m; ++i){
cin >> B[i] >> A[i];
if(B[i] > 0) v[i].pb(B[i]);
if(A[i] > 0) v[i].pb(A[i]);
}
bool ok = 1;
while(ok){
ok = 0;
vector<int> E, one(n+1, -1), up(n+1, -1);
for(int i = 0; i < m; ++i){
if(v[i].empty()) E.pb(i);
else if(v[i].size() == 1){
if(one[v[i][0]] != -1){
ok = 1;
v[one[v[i][0]]].pb(v[i][0]);
ans.pb({i, one[v[i][0]]});
v[i].clear();
break;
}
if(up[v[i][0]] != -1){
ok = 1;
v[up[v[i][0]]].pop_back();
v[i].pb(v[i][0]);
ans.pb({up[v[i][0]], i});
break;
}
one[v[i][0]] = i;
}else{
if(v[i][0] == v[i][1]) continue;
if(up[v[i][1]] != -1){
if(!E.empty()){
int pos = E.back();
E.pop_back();
ans.pb({i, pos});
ans.pb({up[v[i][1]], pos});
v[pos].pb(v[i][1]);
v[pos].pb(v[i][1]);
v[up[v[i][1]]].pop_back();
v[i].pop_back();
ok = 1;
break;
}
}
if(one[v[i][1]] != -1){
ok = 1;
v[one[v[i][1]]].pb(v[i][1]);
ans.pb({i, one[v[i][1]]});
v[i].pop_back();
break;
}
up[v[i][1]] = i;
}
}
if(ok) continue;
if(E.empty()) break;
for(int i = 0; i < m; ++i){
if(v[i].size() == 2){
if(up[v[i][1]] != i){
int pos = E.back();
E.pop_back();
ans.pb({i, pos});
ans.pb({up[v[i][1]], pos});
v[pos].pb(v[i][1]);
v[pos].pb(v[i][1]);
v[up[v[i][1]]].pop_back();
v[i].pop_back();
ok = 1;
break;
}
}
}
for(int i = 0; i < m; ++i){
if(v[i].size() == 2){
if(v[i][0] == v[i][1]) continue;
ans.pb({i, E[0]});
v[E[0]].pb(v[i][1]);
v[i].pop_back();
ok = 1;
break;
}
}
}
ok = 1;
for(int i = 0; i < m; ++i){
if(v[i].empty()) continue;
if(v[i].size() == 1) ok = 0;
else{
ok &= (v[i][0] == v[i][1]);
}
}
if(!ok){
cout << -1;
return;
}
cout << ans.size() << '\n';
for(auto x: ans) cout << x.first + 1 << ' ' << x.second + 1 << '\n';
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(tt--){
solve();
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:123:15: warning: unused variable 'aa' [-Wunused-variable]
123 | int tt = 1, aa;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
4440 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2054 ms |
14312 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
4440 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
4440 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
4440 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
4440 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |