#include #include using namespace std; #define REP(i,N) for(int i=0;i<(N);++i) #define FOR(i,a,b) for(int i=(a);i<=(b);++i) #define FORD(i,a,b) for(int i=(a);i>=(b);--i) #define MAXNODES 1005 bool adjacent[MAXNODES][MAXNODES]; //Structures required for finding the maximum matching int matching[2][MAXNODES]; int path[2*MAXNODES]; bool visited[2][MAXNODES]; int L; //Length of the augmenting path found int N, K; //Number of nodes on side 0 and 1 respectively bool find_augmenting_path(int node, int side) { visited[side][node] = true; path[L++] = node; if(side == 0) { FOR(i,1,K) { if(!visited[1][i] && adjacent[node][i] && matching[0][node]!=i) if(find_augmenting_path(i,1)) return true; } } else { if(matching[1][node] == 0) return true; else { if(!visited[0][matching[1][node]]) if(find_augmenting_path(matching[1][node],0)) return true; } } --L; return false; } int main() { int C, c, e; scanf("%d %d %d", &N, &K, &C); while(C--) { scanf("%d %d", &c, &e); adjacent[c][e] = true; } //Try finding an augmenting path till none can be found while(1) { bool more_augmenting_path = false; memset(visited,0,sizeof(visited)); //Set all nodes as NOT visited L = 0; FOR(i,1,N) { if(matching[0][i] == 0) { //Find first unmatched node if(find_augmenting_path(i,0)) { more_augmenting_path = true; break; } } } if(more_augmenting_path) { for(int i = 0 ; i < L ; i+=2) { matching[0][path[i]] = path[i+1]; matching[1][path[i+1]] = path[i]; } } else { break; } } int count = 0; FOR(i,1,N) if(matching[0][i] != 0) ++count; printf("%d\n", count); FOR(i,1,N) if(matching[0][i] != 0) printf("%d %d\n", i, matching[0][i]); return 0; }