#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef basic_string<int> BI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod=998244353;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head
const int N=200200;
int n,m,a[N],b[N],bb[N],cnt[N][2];
vector<PII> ps[N];
void add(int x,int i,int p) {
if (!x) return;
cnt[x][p]++;
ps[x].pb(mp(i,p));
}
set<int> tp,tp00,tp01;
void ins(int x) {
// printf("ins %d\n",x);
bb[x]++;
if (bb[x]<2) return;
if (ps[x][0].se==ps[x][1].se) {
tp00.insert(x);
} else {
// printf("01 ins %d\n",x);
tp01.insert(x);
}
}
int main() {
scanf("%d%d",&n,&m);
rep(i,1,m+1) {
scanf("%i%i",a+i,b+i);
if (a[i]==b[i]) continue;
add(a[i],i,0);
add(b[i],i,1);
if (b[i]==0) bb[a[i]]++;
else bb[b[i]]++;
}
set<int> fr;
rep(i,1,m+1) if (a[i]==0) fr.insert(i);
rep(i,1,n+1) {
if (bb[i]<2) continue;
if (ps[i][0].se==ps[i][1].se) {
tp00.insert(i);
} else {
tp01.insert(i);
}
}
rep(i,1,n+1) if (cnt[i][1]==2) {
tp.insert(i);
tp00.erase(i);
}
vector<PII> ans;
while (1) {
if (SZ(tp)&&SZ(fr)) {
auto it1=tp.begin();
auto it2=fr.begin();
int f1=*it1,f2=*it2;
ans.pb(mp(ps[f1][0].fi,f2));
ans.pb(mp(ps[f1][1].fi,f2));
ins(a[ps[f1][0].fi]);
ins(a[ps[f1][1].fi]);
tp.erase(it1);
fr.erase(it2);
continue;
}
if (SZ(tp00)) {
auto it=tp00.begin();
int x=*it;
ans.pb(mp(ps[x][0].fi,ps[x][1].fi));
fr.insert(ps[x][0].fi);
tp00.erase(it);
continue;
}
if (SZ(tp01)) {
auto it=tp01.begin();
int x=*it;
// printf("01 %d\n",x);
if (ps[x][0].se==0) swap(ps[x][0],ps[x][1]);
ans.pb(mp(ps[x][0].fi,ps[x][1].fi));
ins(a[ps[x][0].fi]);
tp01.erase(it);
continue;
}
break;
}
if (SZ(tp)+SZ(tp01)+SZ(tp00)>0) {
puts("-1");
return 0;
}
printf("%d\n",SZ(ans));
rep(i,0,SZ(ans)) printf("%d %d\n",ans[i].fi,ans[i].se);
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:48:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
48 | scanf("%d%d",&n,&m);
| ~~~~~^~~~~~~~~~~~~~
Main.cpp:50:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | scanf("%i%i",a+i,b+i);
| ~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5012 KB |
Output is correct |
4 |
Incorrect |
3 ms |
5020 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
138 ms |
18620 KB |
Output is correct |
2 |
Correct |
168 ms |
19176 KB |
Output is correct |
3 |
Correct |
114 ms |
17792 KB |
Output is correct |
4 |
Correct |
121 ms |
17580 KB |
Output is correct |
5 |
Correct |
162 ms |
18832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
5076 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
5076 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5016 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Incorrect |
4 ms |
5020 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5012 KB |
Output is correct |
4 |
Incorrect |
3 ms |
5020 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |