CodeForces-741C.cpp 971 B

123456789101112131415161718192021222324252627282930
  1. #include <iostream>
  2. #include <queue>
  3. #include <utility>
  4. #include <vector>
  5. using namespace std;
  6. int main() {
  7. ios::sync_with_stdio(false);
  8. cin.tie(nullptr);
  9. int n; cin >> n;
  10. vector<pair<int, int>> couple(n); vector<vector<int>> g(2 * n);
  11. for (auto &[a, b] : couple) {
  12. cin >> a >> b; --a; --b; g[a].push_back(b); g[b].push_back(a);
  13. }
  14. for (int i = 0; i < 2 * n; i += 2) {
  15. g[i].push_back(i + 1); g[i + 1].push_back(i);
  16. }
  17. vector<int> color(2 * n, -1);
  18. for (int s = 0; s < 2 * n; ++s) if (color[s] == -1) {
  19. queue<int> q; q.push(s); color[s] = 0;
  20. while (!q.empty()) {
  21. int u = q.front(); q.pop();
  22. for (int v : g[u]) {
  23. if (color[v] == -1) color[v] = color[u] ^ 1, q.push(v);
  24. else if (color[v] == color[u]) { cout << "-1\n"; return 0; }
  25. }
  26. }
  27. }
  28. for (auto [a, b] : couple) cout << color[a] + 1 << ' ' << color[b] + 1 << '\n';
  29. }