fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5. typedef pair<int, int> PII;
  6.  
  7. const int MAX_N = 5e5+5;
  8.  
  9. int n;
  10. PII a[MAX_N];
  11.  
  12. bool czy_przecinaja(int i, int j) {
  13. PII x = a[i];
  14. PII y = a[j];
  15.  
  16. if (x.first < y.first && y.first < x.second && x.second < y.second) return true;
  17. if (y.first < x.first && x.first < y.second && y.second < x.second) return true;
  18.  
  19. return false;
  20. }
  21.  
  22. PII czy_dwa_przecinajaca(int doPominiecia) {
  23. for (int i = 0; i < n-2; i++) {
  24. for (int j = 0; j < n-2; j++) {
  25. if (i == j || i == doPominiecia || j == doPominiecia) continue;
  26.  
  27. if (czy_przecinaja(i, j)) return make_pair(i, j);
  28. }
  29. }
  30.  
  31. return make_pair(-1, -1);
  32. }
  33.  
  34. void solve() {
  35. cin >> n;
  36. for (int i = 0; i < n-2; i++) {
  37. cin >> a[i].first >> a[i].second;
  38. }
  39.  
  40. PII tmp = czy_dwa_przecinajaca(-1);
  41.  
  42. int K1 = tmp.first;
  43. int K2 = tmp.second;
  44.  
  45. if (czy_dwa_przecinajaca(K1).first == -1) {
  46. cout << a[K1].first << ' ' << a[K1].second << '\n';
  47. return;
  48. }
  49.  
  50. cout << a[K2].first << ' ' << a[K2].second << '\n';
  51. }
  52.  
  53. int main() {
  54. ios_base::sync_with_stdio(0);
  55. cin.tie(0);
  56.  
  57. int T;
  58. cin >> T;
  59.  
  60. for (int i = 0; i < T; i++) {
  61. solve();
  62. }
  63.  
  64. return 0;
  65. }
  66.  
Success #stdin #stdout 0s 5312KB
stdin
2
6
1 3
3 5
2 6
1 5
4
1 3
2 4
stdout
2 6
1 3