#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=b;i>=a;--i)
using namespace std;
inline ll read(){
ll x=0,f=1;
char c=getchar();
while (c<'0' || c>'9'){
if (c=='-') f=-1;
c=getchar();
}
while (c>='0' && c<='9'){
x=x*10+c-'0';
c=getchar();
}
return x*f;
}
inline void print(ll x){
if(x < 0) putchar('-'), x = -x;
if(x > 9) print(x / 10);
putchar(x % 10 + '0');
return ;
}
inline void pprint(ll x){
print(x); puts("");
}
inline void aprint(ll x, char c){
print(x); putchar(c);
}
const int N = 1000;
const int M = 1000;
ll d[N], q[N * M], hd, tl;
ll nxt[M << 1], to[M << 1], head[N], cnt = 1, c[M << 1], pre[N], cur[N];
void add(ll x, ll y, ll z)
{
nxt[++cnt] = head[x];
to[cnt] = y;
c[cnt] = z;
head[x] = cnt;
}
const ll inf = 1145141919810;
bool bfs(int st, int ed)
{
memset(d, 0, sizeof(d));
hd = tl = 0;
q[++tl] = st;
d[st] = 1;
while(hd < tl)
{
int x = q[++hd];
for(int i = head[x]; i; i = nxt[i])
{
int y = to[i];
if(d[y] == 0 && c[i])
{
d[y] = d[x] + 1;
q[++tl] = y;
if(y == ed) return 1;
}
}
}
return 0;
}
ll dfs(int x, int ed, ll mf)// mf表示剩余的流量
{
if(x == ed) return mf;
ll sum = 0;
for(int i = cur[x]; i; i = nxt[i])
{
cur[x] = i;
int y = to[i];
if(d[y] == d[x] + 1 && c[i])
{
ll f = dfs(y, ed, min(c[i], mf));
c[i] -= f;
c[i ^ 1] += f;
sum += f;
mf -= f;
if(mf == 0) break;
}
}
if(sum == 0) d[x] = 0;//剪枝
return sum;//这个就是这个点的ans
}
ll dinic(int st, int ed)
{
ll flow = 0;
while(bfs(st, ed))
{
memcpy(cur, head, sizeof(head));
flow += dfs(st, ed, inf);
}
return flow;
}
int vis[N];
void check(int st, int ed){
queue<int> qu;
qu.push(ed);
while(qu.size()){
int x = qu.front(); qu.pop();
// cout << x << endl;
if(vis[x]) continue;
vis[x] = 1;
for(int i = head[x]; i; i = nxt[i]){
int y = to[i];
if(c[i] > 0 && (i & 1)){
if(x != ed && y != st)
cout << y << " " << x << endl;
qu.push(y);
}
}
}
}
int main(){
ll n, m; cin >> m >> n;
ll x, y;
while(cin >> x >> y && x != -1){
add(x, y, inf);
add(y, x, 0);
}
for(int i = 1; i <= m; ++i){
add(n + 2, i, 1);
add(i, n + 2, 0);
}
for(int i = m + 1; i <= n; ++i){
add(i, n + 3, 1);
add(n + 3, i, 0);
}
cout << dinic(n + 2, n + 3) << endl;
// check(n + 2, n + 3);
for(int i = 1; i <= cnt; ++i){
if(i % 2 == 0 && to[i ^ 1] != n + 2 && to[i] != n + 3 && c[i ^ 1] > 0){
cout << to[i ^ 1] << " " << to[i] << endl;
}
}
return 0;
}
一直在WA第五个点