Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.08 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. typedef long long ll;
  7.  
  8.  
  9. struct P
  10. {
  11.     ll x, y;
  12. };
  13.  
  14.  
  15. struct Vec
  16. {
  17.     P a, b;
  18.     ll x, y;
  19.  
  20.     Vec() { }
  21.  
  22.     Vec(P a_, P b_) : a(a_), b(b_)
  23.     {
  24.         this->x = this->b.x - this->a.x;
  25.         this->y = this->b.y - this->a.y;
  26.     }
  27. };
  28.  
  29.  
  30. ll scalar_product(const Vec& a, const Vec& b)
  31. {
  32.     return a.x * b.x + a.y * b.y;
  33. }
  34.  
  35.  
  36. ll cross_product(const Vec& a, const Vec& b)
  37. {
  38.     return a.x * b.y - b.x * a.y;
  39. }
  40.  
  41.  
  42. bool includes(const Vec& a, const P& p)
  43. {
  44.     return cross_product(a, Vec(a.a, p)) == 0 &&
  45.            scalar_product(Vec(p, a.a), Vec(p, a.b)) <= 0;
  46. }
  47.  
  48.  
  49. bool __check(ll a, ll b)
  50. {
  51.     return (a < 0 && b > 0) || (a > 0 && b < 0);
  52. }
  53.  
  54.  
  55. bool is_intersect(const Vec& a, const Vec& b)
  56. {
  57.     ll c_p0 = cross_product(a, Vec(a.a, b.a)),
  58.        c_p1 = cross_product(a, Vec(a.a, b.b)),
  59.        c_p2 = cross_product(b, Vec(b.a, a.a)),
  60.        c_p3 = cross_product(b, Vec(b.a, a.b));
  61.  
  62.     return (__check(c_p0, c_p1) && __check(c_p2, c_p3)) ||
  63.            includes(a, b.a) || includes(a, b.b) ||
  64.            includes(b, a.a) || includes(b, a.b);
  65. }
  66.  
  67.  
  68. bool bumps_into(const Vec& a, const Vec& b)
  69. {
  70.     Vec aa_to_inf(a.a, P{INT_MAX, a.a.y}),
  71.         bb_to_inf(a.b, P{INT_MAX, a.b.y}),
  72.         ba_to_minf(b.a, P{-INT_MAX, b.a.y}),
  73.         bb_to_minf(b.b, P{-INT_MAX, b.b.y});
  74.  
  75.     return is_intersect(aa_to_inf, b) ||
  76.            is_intersect(bb_to_inf, b) ||
  77.            is_intersect(ba_to_minf, a) ||
  78.            is_intersect(bb_to_minf, a);
  79. }
  80.  
  81.  
  82. const int MAX_N = 30001;
  83. Vec tr[MAX_N];
  84.  
  85. vector <int> gr[MAX_N];
  86. int used[MAX_N] = { 0 };
  87. vector <int> topsort;
  88.  
  89.  
  90. void dfs(int v)
  91. {
  92.     used[v] = 1;
  93.  
  94.     for (const int& to: gr[v])
  95.     {
  96.         if (!used[to])
  97.         {
  98.             dfs(to);
  99.         }
  100.     }
  101.     topsort.push_back(v);
  102. }
  103.  
  104.  
  105. P get_p_with_min_y(const P& a, const P& b, const P& c)
  106. {
  107.     if (a.y <= b.y && a.y <= c.y)
  108.     {
  109.         return a;
  110.     }
  111.     if (b.y <= a.y && b.y <= c.y)
  112.     {
  113.         return b;
  114.     }
  115.     return c;
  116. }
  117.  
  118.  
  119. P get_p_with_max_y(const P& a, const P& b, const P& c)
  120. {
  121.     if (a.y >= b.y && a.y >= c.y)
  122.     {
  123.         return a;
  124.     }
  125.     if (b.y >= a.y && b.y >= c.y)
  126.     {
  127.         return b;
  128.     }
  129.     return c;
  130. }
  131.  
  132.  
  133. int main()
  134. {
  135.     ios_base::sync_with_stdio(false);
  136.     cin.tie(nullptr);
  137.     cout.tie(nullptr);
  138.  
  139.     int n;
  140.     cin >> n;
  141.  
  142.     for (int i = 1; i <= n; ++i)
  143.     {
  144.         P a, b, c;
  145.         cin >> a.x >> a.y >> b.x >> b.y >> c.x >> c.y;
  146.  
  147.         tr[i] = Vec(get_p_with_min_y(a, b, c),
  148.                     get_p_with_max_y(a, b, c));
  149.     }
  150.  
  151.     for (int i = 1; i <= n; ++i)
  152.     {
  153.         for (int j = 0; j <= n && gr[i].size() <= 100; ++j)
  154.         {
  155.             if (bumps_into(tr[i], tr[j]))
  156.             {
  157.                 gr[j].push_back(i);
  158.             }
  159.         }
  160.     }
  161.  
  162.  
  163.     for (int i = 1; i <= n; ++i)
  164.     {
  165.         if (!used[i])
  166.         {
  167.             dfs(i);
  168.         }
  169.     }
  170.  
  171.     for (auto it = topsort.rbegin(); it != topsort.rend(); ++it)
  172.     {
  173.         cout << *it << ' ';
  174.     }
  175.  
  176.     return 0;
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement