This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* 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;
vector<int> E;
void move(int i, int j){
if(i==j) return;
int x = v[i].back();
ans.pb({i, j});
v[j].pb(x);
v[i].pop_back();
if(v[i].empty()) E.pb(i);
}
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;
for(int i = 0; i < m; ++i) if(v[i].empty()) E.pb(i);
while(ok){
ok = 0;
vector<int> one(n+1, -1), up(n+1, -1);
for(int i = 0; i < m; ++i){
if(v[i].empty()) continue;
else if(v[i].size() == 1){
if(one[v[i][0]] != -1){
ok = 1;
move(i, one[v[i][0]]);
continue;
}
if(up[v[i][0]] != -1){
ok = 1;
move(up[v[i][0]], i);
continue;
}
one[v[i][0]] = i;
}else{
if(v[i][0] == v[i][1]) continue;
if(up[v[i][1]] != -1 && (v[up[v[i][1]]][0] == v[i][0] || E.size() > 1)){
if(!E.empty()){
int pos = E.back();
E.pop_back();
move(up[v[i][1]], pos);
move(i, pos);
move(up[v[i][1]], i);
ok = 1;
}
continue;
}
if(one[v[i][1]] != -1){
ok = 1;
move(i, one[v[i][1]]);
continue;
}
up[v[i][1]] = i;
}
}
if(ok) continue;
if(E.empty()) break;
// if(ok) continue;
for(int i = 0; i < m; ++i){
if(v[i].size() == 2){
if(v[i][0] == v[i][1]) continue;
move(i, E.back());
E.pop_back();
ok = 1;
break;
}
}
if(ok) continue;
for(int i = 0; i < m; ++i){
if(v[i].size() == 2){
if(v[i][0] == v[i][1]) continue;
if(up[v[i][1]] != i){
move(i, E.back());
E.pop_back();
ok = 1;
break;
}
up[v[i][1]] = i;
}
}
}
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 (stderr)
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 |
---|
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... |