#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
using namespace std;
#define ll int
#define ull unsigned long long
#define ld long double
#define ff first
#define ss second
#define ln "\n"
#define mp make_pair
#define pb push_back
#define INF (ll)1e18
ll MOD = (ll)(1e9+7);
vector<pair<ll, ll>> ops;
vector<vector<ll>> s;
vector<pair<ll, ll>> ans;
ll n, m;
void search(ll opcnt){
bool pos=1;
for (ll i=0; i<m; i++){
if (s[i].size()==1 or (s[i].size()==2 and s[i][0]!=s[i][1])) pos=0;
}
if (pos){
if (ans.size()==0 or ans.size()>ops.size()){
ans=ops;
}
}else{
for (ll i=0; i<m; i++){
for (ll j=0; j<m; j++){
if (i!=j and !s[i].empty() and !(s[i].size()==1 and s[j].empty())){
if((s[j].empty() or s[j].back()==s[i].back()) and s[j].size()<2){
s[j].push_back(s[i].back()); s[i].pop_back();
ops.push_back({i+1, j+1});
search(opcnt+1);
s[i].push_back(s[j].back()); s[j].pop_back();
}
}
}
}
}
}
void solve(){
cin >> n >> m; s.resize(m);
for (ll i=0; i<m; i++){
ll b, t; cin >> b >> t;
if (b!=0) s[i].push_back(b);
if (t!=0) s[i].push_back(t);
}
search(0);
if (ans.empty()){
cout << -1 << ln; return;
}
cout << ans.size() << ln;
for (auto ch:ans) cout << ch.ff << " " << ch.ss << ln;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(nullptr);
#ifdef LOCAL
auto start = chrono::high_resolution_clock::now();
#endif
ll testc=1;
// cin >> testc;
for (ll __c=1; __c<=testc; __c++) solve();
#ifdef LOCAL
auto duration = chrono::duration_cast<chrono::microseconds>(chrono::high_resolution_clock::now() - start);
cout << setprecision(0) << fixed << "time: " << (double)duration.count()/1000.0 << " milliseconds" << endl;
#endif
}
# | 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... |