/************************************************ * * * La battaglia del convoglio VR370108 * * * ************************************************/ #include #include #include #include #define maxN 100000 #define maxM 200000 using namespace std; vector graph[maxN]; int t[maxN]; bool msg [maxN]; int navi [maxN]; int N, M; bool dfs(int x, int y); int main(){ FILE* in; in = fopen("input.txt", "r"); assert(in); fscanf(in, "%d %d", &N, &M); for(int i = 0; i < N; i++){ int m, n; fscanf(in, "%d %d", &m, &n); graph[m].push_back(n); t[m] = n; } for(int i = 0; i < M-N; i++){ int o, p; fscanf(in, "%d %d", &o, &p); graph[o].push_back(p); } fclose(in); FILE* output; output = fopen("output.txt", "w"); if(dfs(0,0)){ int m, n; for(int i = 0; i < N; i++){ m = i; n = navi[m]; fprintf(output, "%d %d\n", m, n); } } else { fprintf(output, "-1\n"); } fclose(output); return 0; } bool dfs(int x, int y){ if(y == N){ return false; } if(x == N){ return true; } for(vector::iterator i = graph[x].begin(); i!= graph[x].end(); i++){ if(!msg[*i]){ msg[*i] = true; navi[x] = *i; if(t[x] == *i){ y++; } if(dfs(x+1, y)){ return true; } else { msg[*i] = false; navi[x] = -1; } } if(i == graph[x].end()){ return false; } } }