Guest User

Untitled

a guest
Jan 18th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. /*
  2. $$$$$$$\ $$\ $$$$$$$\
  3. $$ __$$\ \__| $$ __$$\
  4. $$ | $$ | $$$$$$\ $$$$$$\ $$\ $$$$$$$\ $$ | $$ | $$$$$$\ $$$$$$\ $$$$$$$\ $$$$$$\
  5. $$$$$$$\ |$$ __$$\ $$ __$$\ $$ |$$ _____|$$$$$$$\ | \____$$\ $$ __$$\ $$ _____|\____$$\
  6. $$ __$$\ $$ / $$ |$$ | \__|$$ |\$$$$$$\ $$ __$$\ $$$$$$$ |$$ | \__|$$ / $$$$$$$ |
  7. $$ | $$ |$$ | $$ |$$ | $$ | \____$$\ $$ | $$ |$$ __$$ |$$ | $$ | $$ __$$ |
  8. $$$$$$$ |\$$$$$$ |$$ | $$ |$$$$$$$ |$$$$$$$ |\$$$$$$$ |$$ | \$$$$$$$\\$$$$$$$ |
  9. \_______/ \______/ \__| \__|\_______/ \_______/ \_______|\__| \_______|\_______|
  10. */
  11. #include <bits/stdc++.h>
  12.  
  13. typedef long long ll;
  14. #define pb push_back
  15. #define mp make_pair
  16. #define pii pair <int,int>
  17. #define in insert
  18. #define X first
  19. #define Y second
  20. #define frn front
  21. #define bc back
  22. #define rep(i,a,b)for (ll (i) = (a); (i) < b(); ++(i))
  23. #define _ << " " <<
  24. #define sz(x) (int)x.size()
  25. #define all(a) (a).begin(),(a).end()
  26.  
  27. using namespace std;
  28.  
  29. int t, n, m;
  30. int parent[1005], r[1005];
  31.  
  32. struct duzina
  33. {
  34. pii a;
  35. pii b;
  36. } d;
  37.  
  38. vector <duzina> v;
  39.  
  40. double dist(int x, int y, int _x, int _y){
  41. return sqrt((x-_x)*(x-_x) + (y-_y)*(y-_y));
  42. }
  43.  
  44. int find(int x){
  45. return parent[x] = (parent[x] == x ? x : find(parent[x]));
  46. }
  47.  
  48. bool isSameSet(int a, int b){
  49. //cout <<parent[a] _ parent[b]<<endl;
  50. return parent[a] == parent[b];
  51. }
  52.  
  53. void Union(int p, int q){
  54. parent[p] = parent[q];
  55. }
  56.  
  57. int ccw(pii a, pii b, pii c){
  58. int t = a.X*(b.Y - c.Y) + b.X*(c.Y - a.Y) + c.Y*(a.Y-b.Y);
  59. if (t < 0)
  60. return -1;
  61. if (t > 0)
  62. return 1;
  63. return 0;
  64. }
  65.  
  66. bool sijeku(pii a, pii b, pii c, pii d){
  67. int t1 = ccw(a,c,b);
  68. int t2 = ccw(a,d,b);
  69. int t3 = ccw(c,a,d);
  70. int t4 = ccw(c,b,d);
  71. //cout <<t1 _ t2 _ t3 _ t4<<endl;
  72. //cout <<a.X _ a.Y _ b.X _ b.Y _ c.X _ c.Y _ d.X _ d.Y<<endl;
  73. if (a == d || b == d || c == b || a == c)
  74. return true;
  75. //cout <<"Nije jednako\n";
  76. if (t1 == 0){
  77. if (dist(a.X, c.X, a.Y, c.Y) + dist(b.X, c.X, b.Y, c.Y) == dist(a.X, b.X, a.Y, b.Y))
  78. return true;
  79. return false;
  80. }
  81. if (t2 == 0){
  82. if (dist(a.X, d.X, a.Y, d.Y) + dist(b.X, d.X, b.Y, d.Y) == dist(a.X, b.X, a.Y, b.Y))
  83. return true;
  84. return false;
  85. }
  86. if (t3 == 0){
  87. if (dist(a.X, c.X, a.Y, c.Y) + dist(a.X, d.X, a.Y, d.Y) == dist(c.X, d.X, c.Y, d.Y))
  88. return true;
  89. return false;
  90. }
  91. if (t4 == 0){
  92. //cout <<(dist(b.X, c.X, b.Y, c.Y) + dist(b.X, d.X, b.Y, d.Y) == dist(c.X, d.X, c.Y, d.Y))<<endl;;
  93. if (dist(b.X, c.X, b.Y, c.Y) + dist(b.X, d.X, b.Y, d.Y) <= dist(c.X, d.X, c.Y, d.Y))
  94. return true;
  95. return false;
  96. }
  97. return t1!=t2 && t3!=t4;
  98. }
  99.  
  100. int main () {
  101. cin >>t;
  102. while (t--){
  103. scanf("%d%d", &n, &m);
  104. v.clear();
  105. for (int i = 0; i < n; ++i)
  106. {
  107. parent[i] = i;
  108. }
  109. for (int i = 0; i < n; ++i)
  110. {
  111. cin >>d.a.X>>d.a.Y>>d.b.X>>d.b.Y;
  112. v.pb(d);
  113. }
  114. for (int i = 0; i < n; ++i){
  115. for (int j = i+1; j < n; ++j)
  116. {
  117. int u = find(i);
  118. int s = find(j);
  119. bool sik = sijeku(v[i].a, v[i].b , v[j].a, v[j].b);
  120. //cout <<i _ j _ sik<<endl;
  121. if (sik)
  122. Union(u, s);
  123. }
  124. }
  125. for (int i = 0; i < m; ++i)
  126. {
  127. int x,y;
  128. cin >>x>>y;
  129. x--;
  130. y--;
  131. x = find(x);
  132. y = find(y);
  133. if (x == y)
  134. printf("%s\n", "YES");
  135. else
  136. printf("%s\n", "NO");
  137. }
  138. v.clear();
  139. }
  140. return 0;
  141. }
Add Comment
Please, Sign In to add comment