Advertisement
Malinovsky239

D

Jan 13th, 2012
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <cstdlib>
  5. #include <vector>
  6. #include <set>
  7. #include <cassert>
  8.  
  9. using namespace std;
  10.  
  11. #define N int(2e5)
  12. #define pb push_back
  13.  
  14. struct vect {
  15.     int x, y;
  16.  
  17.     void read() {
  18.         scanf("%d %d", &x, &y);
  19.     }
  20. };
  21.  
  22. bool operator < (vect a, vect b) {
  23.     return a.y < b.y;
  24. }
  25.  
  26. struct triangle {
  27.     vect p[3];
  28.  
  29.     void read() {
  30.         for (int i = 0; i < 3; i++)
  31.             p[i].read();
  32.         sort(p, p + 3);            
  33.     }
  34. };
  35.  
  36. struct event {
  37.     vect p;
  38.     int open, index;
  39.  
  40.     event() {}
  41.  
  42.     event(vect a, int b, int c) {
  43.         p = a, open = b, index = c;    
  44.     }          
  45. };
  46.  
  47. bool operator < (event a, event b) {
  48.     if (a.p.y == b.p.y)
  49.         return a.open > b.open;
  50.     return a.p < b.p;
  51. }
  52.  
  53. event e[N];
  54. int cnt, index, st[N], color[N];
  55. vect lines[N][2];
  56. vector<int> graph[N], ans;
  57. set<int> Set;
  58.  
  59. void dfs(int v) {  
  60.     color[v] = 1;
  61.     for (int i = 0; i < graph[v].size(); i++) {
  62.         int to = graph[v][i];
  63.         if (!color[to])
  64.             dfs(to);
  65.         else {
  66.             if (color[to] == 1) {
  67.                 cout << -1 << endl;
  68.                 assert(0);
  69.                 exit(0);
  70.             }
  71.         }
  72.     }
  73.     color[v] = 2;
  74.     ans.pb(v);
  75. }
  76.  
  77. int main() {
  78.     int n;
  79.     cin >> n;
  80.  
  81.     for (int i = 0; i < n; i++) {
  82.         triangle t;
  83.         t.read();
  84.         if (t.p[0].y != t.p[1].y) {
  85.             e[cnt++] = event(t.p[0],  1, index);
  86.             e[cnt++] = event(t.p[1], -1, index);
  87.             lines[index][0] = t.p[0];
  88.             lines[index][1] = t.p[1];
  89.         }
  90.         index++;
  91.         if (t.p[1].y != t.p[2].y) {
  92.             e[cnt++] = event(t.p[1], 1, index);
  93.             e[cnt++] = event(t.p[2], -1, index);
  94.             lines[index][0] = t.p[1];
  95.             lines[index][1] = t.p[2];                  
  96.         }
  97.         index++;
  98.     }  
  99.  
  100.     sort(e, e + cnt);
  101.     for (int i = 0; i < cnt; i++) {
  102.         int num = e[i].index;
  103.         st[num] += e[i].open;          
  104.  
  105.         if (st[num]) {
  106.             for (set<int>::iterator j = Set.begin(); j != Set.end(); j++) {
  107.                 int num2 = *j;
  108.                 if (num2 / 2 == num / 2) continue;
  109.                 int y = lines[num][0].y, x = lines[num][0].x;
  110.                
  111.                 vect v1 = lines[num2][0], v2 = lines[num2][1];
  112.                 int A = v1.y - v2.y, B = v2.x - v1.x;
  113.                 int C = - A * v1.x - B * v1.y;
  114.  
  115.                 double x2 = - double(B * y + C) / A;
  116.                 if (x2 > x)
  117.                     graph[ num / 2 ].pb(num2 / 2);
  118.                 else
  119.                     graph[ num2 / 2 ].pb(num / 2); 
  120.             }
  121.             Set.insert(num);                   
  122.         }
  123.         else {
  124.             Set.erase(num);        
  125.         }
  126.     }              
  127.  
  128.     for (int i = 0; i < n; i++)
  129.         if (!color[i])
  130.             dfs(i);
  131.  
  132.     for (int i = 0; i < ans.size(); i++)
  133.         cout << ans[i] + 1 << " ";
  134.  
  135.     return 0;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement