Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- struct P
- {
- ll x, y;
- };
- struct Vec
- {
- P a, b;
- ll x, y;
- Vec() { }
- Vec(P a_, P b_) : a(a_), b(b_)
- {
- this->x = this->b.x - this->a.x;
- this->y = this->b.y - this->a.y;
- }
- };
- ll scalar_product(const Vec& a, const Vec& b)
- {
- return a.x * b.x + a.y * b.y;
- }
- ll cross_product(const Vec& a, const Vec& b)
- {
- return a.x * b.y - b.x * a.y;
- }
- bool includes(const Vec& a, const P& p)
- {
- return cross_product(a, Vec(a.a, p)) == 0 &&
- scalar_product(Vec(p, a.a), Vec(p, a.b)) <= 0;
- }
- bool __check(ll a, ll b)
- {
- return (a < 0 && b > 0) || (a > 0 && b < 0);
- }
- bool is_intersect(const Vec& a, const Vec& b)
- {
- ll c_p0 = cross_product(a, Vec(a.a, b.a)),
- c_p1 = cross_product(a, Vec(a.a, b.b)),
- c_p2 = cross_product(b, Vec(b.a, a.a)),
- c_p3 = cross_product(b, Vec(b.a, a.b));
- return (__check(c_p0, c_p1) && __check(c_p2, c_p3)) ||
- includes(a, b.a) || includes(a, b.b) ||
- includes(b, a.a) || includes(b, a.b);
- }
- bool bumps_into(const Vec& a, const Vec& b)
- {
- Vec aa_to_inf(a.a, P{INT_MAX, a.a.y}),
- bb_to_inf(a.b, P{INT_MAX, a.b.y}),
- ba_to_minf(b.a, P{-INT_MAX, b.a.y}),
- bb_to_minf(b.b, P{-INT_MAX, b.b.y});
- return is_intersect(aa_to_inf, b) ||
- is_intersect(bb_to_inf, b) ||
- is_intersect(ba_to_minf, a) ||
- is_intersect(bb_to_minf, a);
- }
- const int MAX_N = 30001;
- Vec tr[MAX_N];
- vector <int> gr[MAX_N];
- int used[MAX_N] = { 0 };
- vector <int> topsort;
- void dfs(int v)
- {
- used[v] = 1;
- for (const int& to: gr[v])
- {
- if (!used[to])
- {
- dfs(to);
- }
- }
- topsort.push_back(v);
- }
- P get_p_with_min_y(const P& a, const P& b, const P& c)
- {
- if (a.y <= b.y && a.y <= c.y)
- {
- return a;
- }
- if (b.y <= a.y && b.y <= c.y)
- {
- return b;
- }
- return c;
- }
- P get_p_with_max_y(const P& a, const P& b, const P& c)
- {
- if (a.y >= b.y && a.y >= c.y)
- {
- return a;
- }
- if (b.y >= a.y && b.y >= c.y)
- {
- return b;
- }
- return c;
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int n;
- cin >> n;
- for (int i = 1; i <= n; ++i)
- {
- P a, b, c;
- cin >> a.x >> a.y >> b.x >> b.y >> c.x >> c.y;
- tr[i] = Vec(get_p_with_min_y(a, b, c),
- get_p_with_max_y(a, b, c));
- }
- for (int i = 1; i <= n; ++i)
- {
- for (int j = 0; j <= n && gr[i].size() <= 100; ++j)
- {
- if (bumps_into(tr[i], tr[j]))
- {
- gr[j].push_back(i);
- }
- }
- }
- for (int i = 1; i <= n; ++i)
- {
- if (!used[i])
- {
- dfs(i);
- }
- }
- for (auto it = topsort.rbegin(); it != topsort.rend(); ++it)
- {
- cout << *it << ' ';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement