forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path174.cpp
More file actions
60 lines (53 loc) · 1.4 KB
/
174.cpp
File metadata and controls
60 lines (53 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//ac
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
#define MAXN 400000
#define sig(x) ((x)>0?1:-1)
#define abs(x) ((x)>0?(x):-(x))
#define _ufind_run(x) for(;p[t=abs(x)];x=sig(x)*p[abs(x)],p[t]=sig(p[t])*(p[abs(x)]?p[abs(x)]:abs(p[t])))
#define _run_both _ufind_run(i);_ufind_run(j)
#define _set_side(x) p[abs(i)]=sig(i)*(abs(i)==abs(j)?0:(x)*j)
#define _judge_side(x) (i==(x)*j&&i)
struct ufind{
int p[MAXN],t;
void init(){memset(p,0,sizeof(p));}
int set_friend(int i,int j){_run_both;_set_side(1);return !_judge_side(-1);}
int set_enemy(int i,int j){_run_both;_set_side(-1);return !_judge_side(1);}
int is_friend(int i,int j){_run_both;return _judge_side(1);}
int is_enemy(int i,int j){_run_both;return _judge_side(-1);}
};
class point {
public:
int x, y;
bool operator < (const point& c) const {
if (x != c.x) return x < c.x;
else return y < c.y;
}
};
int m;
int main() {
scanf("%d", &m);
int ret = 0;
ufind uf;
uf.init();
map<point, int> mm;
map<point, int>::iterator it;
int num = 0;
for (int i = 0; i < m; ++i) {
point a, b;
scanf("%d %d %d %d", &a.x, &a.y, &b.x, &b.y);
if (ret) continue;
it = mm.find(a);
if (it == mm.end()) mm[a] = num++;
it = mm.find(b);
if (it == mm.end()) mm[b] = num++;
int na = mm[a], nb = mm[b];
if (uf.is_friend(na, nb)) ret = i + 1;
uf.set_friend(na, nb);
}
printf("%d\n", ret);
return 0;
}