Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <iostream>
- #include <algorithm>
- #include <cstdlib>
- #include <vector>
- #include <set>
- #include <cassert>
- using namespace std;
- #define N int(2e5)
- #define pb push_back
- struct vect {
- int x, y;
- void read() {
- scanf("%d %d", &x, &y);
- }
- };
- bool operator < (vect a, vect b) {
- return a.y < b.y;
- }
- struct triangle {
- vect p[3];
- void read() {
- for (int i = 0; i < 3; i++)
- p[i].read();
- sort(p, p + 3);
- }
- };
- struct event {
- vect p;
- int open, index;
- event() {}
- event(vect a, int b, int c) {
- p = a, open = b, index = c;
- }
- };
- bool operator < (event a, event b) {
- if (a.p.y == b.p.y)
- return a.open > b.open;
- return a.p < b.p;
- }
- event e[N];
- int cnt, index, st[N], color[N];
- vect lines[N][2];
- vector<int> graph[N], ans;
- set<int> Set;
- void dfs(int v) {
- color[v] = 1;
- for (int i = 0; i < graph[v].size(); i++) {
- int to = graph[v][i];
- if (!color[to])
- dfs(to);
- else {
- if (color[to] == 1) {
- cout << -1 << endl;
- assert(0);
- exit(0);
- }
- }
- }
- color[v] = 2;
- ans.pb(v);
- }
- int main() {
- int n;
- cin >> n;
- for (int i = 0; i < n; i++) {
- triangle t;
- t.read();
- if (t.p[0].y != t.p[1].y) {
- e[cnt++] = event(t.p[0], 1, index);
- e[cnt++] = event(t.p[1], -1, index);
- lines[index][0] = t.p[0];
- lines[index][1] = t.p[1];
- }
- index++;
- if (t.p[1].y != t.p[2].y) {
- e[cnt++] = event(t.p[1], 1, index);
- e[cnt++] = event(t.p[2], -1, index);
- lines[index][0] = t.p[1];
- lines[index][1] = t.p[2];
- }
- index++;
- }
- sort(e, e + cnt);
- for (int i = 0; i < cnt; i++) {
- int num = e[i].index;
- st[num] += e[i].open;
- if (st[num]) {
- for (set<int>::iterator j = Set.begin(); j != Set.end(); j++) {
- int num2 = *j;
- if (num2 / 2 == num / 2) continue;
- int y = lines[num][0].y, x = lines[num][0].x;
- vect v1 = lines[num2][0], v2 = lines[num2][1];
- int A = v1.y - v2.y, B = v2.x - v1.x;
- int C = - A * v1.x - B * v1.y;
- double x2 = - double(B * y + C) / A;
- if (x2 > x)
- graph[ num / 2 ].pb(num2 / 2);
- else
- graph[ num2 / 2 ].pb(num / 2);
- }
- Set.insert(num);
- }
- else {
- Set.erase(num);
- }
- }
- for (int i = 0; i < n; i++)
- if (!color[i])
- dfs(i);
- for (int i = 0; i < ans.size(); i++)
- cout << ans[i] + 1 << " ";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement