#include #include #include #define Nmax 100000 #define Mmax 200000 using namespace std; ifstream finput; ofstream foutput; int N, M; vector adj[Mmax]; vector < vector > sort; int path[Mmax]; int checked[Nmax]; int search (int); int main () { int num, i, a, b, k, t, found; finput.open ("input.txt"); finput >> N >> M; num = 0; for (i = 0; i < M; i ++) { num ++; finput >> a >> b; b += Nmax; if (num <= N) adj[a].push_back(b); else adj[b].push_back(a); } finput.close (); num = 0; found = 0; while (num < N && !found) { found = search (num); num ++; } foutput.open ("output.txt"); if (!found) foutput << -1; else { for (i = 0; i < N; i++) if (!checked[i]) { vector row; row.push_back (i); row.push_back (adj[i][0]-Nmax); sort.push_back (row); } num = sort.size(); for (i = 0; i < num - 1; i ++) for (k = 0; k < num - 1 - i; k ++) if (sort[k][0] > sort[k+1][0]) { t = sort[k][0]; sort[k][0] = sort[k+1][0]; sort[k+1][0] = t; t = sort[k][1]; sort[k][1] = sort[k+1][1]; sort[k+1][1] = t; } for (i = 0; i < num; i ++) foutput << sort[i][0] << ' ' << sort[i][1] << '\n'; } foutput.close (); return 0; } int search (int v) { int u, found; if (path[v]) { if (v < Nmax) return 1; else return 0; } path[v] = 1; found = 0; for (u = 0; u < adj[v].size (); u++) { found = search (adj[v][u]); if (found == 1 && v >= Nmax) { vector row; row.push_back (adj[v][u]); row.push_back (v-Nmax); sort.push_back (row); checked[adj[v][u]] = 1; } if (found == 1 && v < Nmax && checked[v]) found = 2; if (found) { path[v] = 0; return found; } } path[v] = 0; return 0; }