///Max Flow Dinic's Algorithm with scaling
///O(N*N*M) on normal graphs
///O(sqrt(N)*M) on bipartite graphs
#include<bits/stdc++.h>
using namespace std;
ifstream f("harta.in");
ofstream g("harta.out");
const int NMAX=205,INF=0x3F3F3F3F;///we want double the number of nodes,because we double them to create the bipartite graph
int n,m,x,y,c,maxx;
bool SCALING=0;///you can choose between having scaling and not having scaling
///it is better without scaling on edges with lower costs, but on high cost edges, scaling helps a lot
struct edge
{
int node,flow,capacity,index;
};
vector<int>level,skip,outdegree,indegree;
vector<edge>graph[NMAX];
void add_edge(int x,int y,int c)
{
graph[x].push_back({y,0,c,0});///to node,flow,capacity,index
graph[y].push_back({x,0,0,0});///reverse edge:to node,flow,capacity,index
graph[x].back().index=graph[y].size()-1;///index from adjacency list of y
graph[y].back().index=graph[x].size()-1;///index from adjacency list of x
}
void create_graph()
{
maxx=c=1;///because we do maximum matching in a bipartite graph
for(int i=1; i<=n; i++)///add the edges between the sides, maximum number of edges - the edge from i to i+n
for(int j=1; j<=n; j++)
if(j!=i)
add_edge(i,j+n,c);///the node on the left is i and the node on the right is j+n
for(int i=1; i<=n; i++)///add the edges from the source
add_edge(2*n+1,i,outdegree[i]);///the source and the node on the left side of our graph
for(int i=1; i<=n; i++)///add the edges to the terminal
add_edge(i+n,2*n+2,indegree[i]);///the node on the right and the terminal
}
bool bfs(int s,int t,int limit)
{
level.assign(2*n+3,-1);///because we do maximum matching in a bipartite graph and we have 2*n+2 node
level[s]=0;
queue<int>q;
q.push(s);
while(!q.empty())
{
int node=q.front();
q.pop();
for(edge& link:graph[node])
if(link.capacity-link.flow>0 && level[link.node]==-1 && (!SCALING || link.capacity-link.flow>=limit))///if I can still push flow and the next node is not visited
{
level[link.node]=level[node]+1;
q.push(link.node);
}
}
return level[t]!=-1;///if t was visited it will return 1,otherwise 0
}
int dfs(int node,int t,int cur_flow)
{
if(node==t)
return cur_flow;
for(; skip[node]<(int)graph[node].size(); skip[node]++)///we start from the pointer we have for this node
{
int next=graph[node][skip[node]].node;
edge& link = graph[node][skip[node]];///for simplicity
if(link.capacity-link.flow>0 && level[node]+1==level[next])///if I can still push flow and the node is not visited and we only go forward
{
int bottleNeck=dfs(next,t,min(cur_flow,link.capacity-link.flow));
if(bottleNeck>0)
{
link.flow+=bottleNeck;///update on the normal edge
graph[next][link.index].flow-=bottleNeck;///update on the reverse edge
return bottleNeck;
}
}
}
return 0;
}
int maxflow(int s,int t)
{
int max_flow=0;
skip.resize(2*n+3);///because we do maximum matching in a bipartite graph and we have 2*n+2 node
for(int limit=SCALING ? (1<<(int(log2(maxx)))):1; limit>0; limit=limit/2)
while(bfs(s,t,limit))
{
fill(skip.begin(),skip.end(),0);///for pruning dead ends
for(int new_flow=dfs(s,t,INF); new_flow!=0; new_flow=dfs(s,t,INF))///as long as we can find a new path we increase the max_flow
max_flow+=new_flow;
}
return max_flow;
}
void solve()
{
g<<maxflow(2*n+1,2*n+2)<<'\n';///the number of edges
for(int i=1; i<=n; i++)
for(int j=0; j<(int)graph[i].size()-1; j++)///in graph[i] we have one extra edge, from the source, hence we don't take it into consideration
if(graph[i][j].flow==1)///we have used the edge from i to graph[i][j].node
g<<i<<' '<<graph[i][j].node-n<<'\n';///we subtract n because we should print the normal node
}
int main()
{
f>>n;
outdegree.resize(n+1);
indegree.resize(n+1);
for(int i=1; i<=n; i++)
f>>outdegree[i]>>indegree[i];
create_graph();
solve();
return 0;
}