Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- $$$$$$$\ $$\ $$$$$$$\
- $$ __$$\ \__| $$ __$$\
- $$ | $$ | $$$$$$\ $$$$$$\ $$\ $$$$$$$\ $$ | $$ | $$$$$$\ $$$$$$\ $$$$$$$\ $$$$$$\
- $$$$$$$\ |$$ __$$\ $$ __$$\ $$ |$$ _____|$$$$$$$\ | \____$$\ $$ __$$\ $$ _____|\____$$\
- $$ __$$\ $$ / $$ |$$ | \__|$$ |\$$$$$$\ $$ __$$\ $$$$$$$ |$$ | \__|$$ / $$$$$$$ |
- $$ | $$ |$$ | $$ |$$ | $$ | \____$$\ $$ | $$ |$$ __$$ |$$ | $$ | $$ __$$ |
- $$$$$$$ |\$$$$$$ |$$ | $$ |$$$$$$$ |$$$$$$$ |\$$$$$$$ |$$ | \$$$$$$$\\$$$$$$$ |
- \_______/ \______/ \__| \__|\_______/ \_______/ \_______|\__| \_______|\_______|
- */
- #include <bits/stdc++.h>
- typedef long long ll;
- #define pb push_back
- #define mp make_pair
- #define pii pair <int,int>
- #define in insert
- #define X first
- #define Y second
- #define frn front
- #define bc back
- #define rep(i,a,b)for (ll (i) = (a); (i) < b(); ++(i))
- #define _ << " " <<
- #define sz(x) (int)x.size()
- #define all(a) (a).begin(),(a).end()
- using namespace std;
- int t, n, m;
- int parent[1005], r[1005];
- struct duzina
- {
- pii a;
- pii b;
- } d;
- vector <duzina> v;
- double dist(int x, int y, int _x, int _y){
- return sqrt((x-_x)*(x-_x) + (y-_y)*(y-_y));
- }
- int find(int x){
- return parent[x] = (parent[x] == x ? x : find(parent[x]));
- }
- bool isSameSet(int a, int b){
- //cout <<parent[a] _ parent[b]<<endl;
- return parent[a] == parent[b];
- }
- void Union(int p, int q){
- parent[p] = parent[q];
- }
- int ccw(pii a, pii b, pii c){
- int t = a.X*(b.Y - c.Y) + b.X*(c.Y - a.Y) + c.Y*(a.Y-b.Y);
- if (t < 0)
- return -1;
- if (t > 0)
- return 1;
- return 0;
- }
- bool sijeku(pii a, pii b, pii c, pii d){
- int t1 = ccw(a,c,b);
- int t2 = ccw(a,d,b);
- int t3 = ccw(c,a,d);
- int t4 = ccw(c,b,d);
- //cout <<t1 _ t2 _ t3 _ t4<<endl;
- //cout <<a.X _ a.Y _ b.X _ b.Y _ c.X _ c.Y _ d.X _ d.Y<<endl;
- if (a == d || b == d || c == b || a == c)
- return true;
- //cout <<"Nije jednako\n";
- if (t1 == 0){
- 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))
- return true;
- return false;
- }
- if (t2 == 0){
- 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))
- return true;
- return false;
- }
- if (t3 == 0){
- 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))
- return true;
- return false;
- }
- if (t4 == 0){
- //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;;
- 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))
- return true;
- return false;
- }
- return t1!=t2 && t3!=t4;
- }
- int main () {
- cin >>t;
- while (t--){
- scanf("%d%d", &n, &m);
- v.clear();
- for (int i = 0; i < n; ++i)
- {
- parent[i] = i;
- }
- for (int i = 0; i < n; ++i)
- {
- cin >>d.a.X>>d.a.Y>>d.b.X>>d.b.Y;
- v.pb(d);
- }
- for (int i = 0; i < n; ++i){
- for (int j = i+1; j < n; ++j)
- {
- int u = find(i);
- int s = find(j);
- bool sik = sijeku(v[i].a, v[i].b , v[j].a, v[j].b);
- //cout <<i _ j _ sik<<endl;
- if (sik)
- Union(u, s);
- }
- }
- for (int i = 0; i < m; ++i)
- {
- int x,y;
- cin >>x>>y;
- x--;
- y--;
- x = find(x);
- y = find(y);
- if (x == y)
- printf("%s\n", "YES");
- else
- printf("%s\n", "NO");
- }
- v.clear();
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment