#include <iostream> #include <stdio.h> #include <vector> #include <utility> #include <algorithm> using namespace std; int n; int xxx1[100000]; int xxx2[100000]; int yyy1[100000]; int yyy2[100000]; int repr[100000]; int repx, repy; int ffind(int x); int funion(int a, int b); bool zmiana = false; int xx1, xx2, yy1, yy2, rozmx, rozmy; bool uzyte[100000]; vector < pair <int, pair <int, pair <int, int> > > > plemiona; int ile_plemion = 0; int main() { scanf("%d", &n); for(int i = 0; i < n; i++) repr[i] = i; for(int i = 0; i < n; i++) { scanf("%d", &xxx1[i]); scanf("%d", &xxx2[i]); scanf("%d", &yyy1[i]); scanf("%d", &yyy2[i]); if(xxx1[i] > xxx2[i]) swap(xxx1[i], xxx2[i]); if(yyy1[i] > yyy2[i]) swap(yyy1[i], yyy2[i]); } while(1) { zmiana = false; for(int x = 0; x < n; x++) { for(int y = 0; y < n; y++) { repx = ffind(x); repy = ffind(y); if(repx == repy) continue; xx1 = max(xxx1[x], xxx1[y]); xx2 = min(xxx2[x], xxx2[y]); yy1 = max(yyy1[x], yyy1[y]); yy2 = min(yyy2[x], yyy2[y]); rozmx = xx2-xx1; rozmy = yy2-yy1; if(rozmx > 0 && rozmy > 0) { funion(x, y); zmiana = true; int reprez = ffind(y); xxx1[reprez] = min(xxx1[x], xxx1[y]); xxx2[reprez] = max(xxx2[x], xxx2[y]); yyy1[reprez] = min(yyy1[x], yyy1[y]); yyy2[reprez] = max(yyy2[x], yyy2[y]); } } } if(zmiana == false) break; } for(int i = 0; i < n; i++) { repx = ffind(i); if(uzyte[repx] == false) { uzyte[repx] = true; ile_plemion += 1; plemiona.push_back(make_pair(xxx1[repx],make_pair(xxx2[repx],make_pair(yyy1[repx],yyy2[repx])))); } } sort(plemiona.begin(), plemiona.end()); printf("%d\n", ile_plemion); for(int i = 0; i < ile_plemion; i++) { printf("%d ", plemiona[i].first); printf("%d ", plemiona[i].second.first); printf("%d ", plemiona[i].second.second.first); printf("%d", plemiona[i].second.second.second); printf("\n"); } return 0; } int ffind(int x) { if(repr[x] == x) return x; else { repr[x] = ffind(repr[x]); return repr[x]; } } int funion(int a, int b) { int repa = ffind(a); int repb = ffind(b); repr[repb] = repa; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | #include <iostream> #include <stdio.h> #include <vector> #include <utility> #include <algorithm> using namespace std; int n; int xxx1[100000]; int xxx2[100000]; int yyy1[100000]; int yyy2[100000]; int repr[100000]; int repx, repy; int ffind(int x); int funion(int a, int b); bool zmiana = false; int xx1, xx2, yy1, yy2, rozmx, rozmy; bool uzyte[100000]; vector < pair <int, pair <int, pair <int, int> > > > plemiona; int ile_plemion = 0; int main() { scanf("%d", &n); for(int i = 0; i < n; i++) repr[i] = i; for(int i = 0; i < n; i++) { scanf("%d", &xxx1[i]); scanf("%d", &xxx2[i]); scanf("%d", &yyy1[i]); scanf("%d", &yyy2[i]); if(xxx1[i] > xxx2[i]) swap(xxx1[i], xxx2[i]); if(yyy1[i] > yyy2[i]) swap(yyy1[i], yyy2[i]); } while(1) { zmiana = false; for(int x = 0; x < n; x++) { for(int y = 0; y < n; y++) { repx = ffind(x); repy = ffind(y); if(repx == repy) continue; xx1 = max(xxx1[x], xxx1[y]); xx2 = min(xxx2[x], xxx2[y]); yy1 = max(yyy1[x], yyy1[y]); yy2 = min(yyy2[x], yyy2[y]); rozmx = xx2-xx1; rozmy = yy2-yy1; if(rozmx > 0 && rozmy > 0) { funion(x, y); zmiana = true; int reprez = ffind(y); xxx1[reprez] = min(xxx1[x], xxx1[y]); xxx2[reprez] = max(xxx2[x], xxx2[y]); yyy1[reprez] = min(yyy1[x], yyy1[y]); yyy2[reprez] = max(yyy2[x], yyy2[y]); } } } if(zmiana == false) break; } for(int i = 0; i < n; i++) { repx = ffind(i); if(uzyte[repx] == false) { uzyte[repx] = true; ile_plemion += 1; plemiona.push_back(make_pair(xxx1[repx],make_pair(xxx2[repx],make_pair(yyy1[repx],yyy2[repx])))); } } sort(plemiona.begin(), plemiona.end()); printf("%d\n", ile_plemion); for(int i = 0; i < ile_plemion; i++) { printf("%d ", plemiona[i].first); printf("%d ", plemiona[i].second.first); printf("%d ", plemiona[i].second.second.first); printf("%d", plemiona[i].second.second.second); printf("\n"); } return 0; } int ffind(int x) { if(repr[x] == x) return x; else { repr[x] = ffind(repr[x]); return repr[x]; } } int funion(int a, int b) { int repa = ffind(a); int repb = ffind(b); repr[repb] = repa; } |