-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathentries.cpp
72 lines (69 loc) · 1.4 KB
/
entries.cpp
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
60
61
62
63
64
65
66
67
68
69
70
71
72
# include <cstdio>
# include <algorithm>
# define NR 10005
using namespace std;
int i,j,n,m,x,y,k,VV,Rx,Ry;
int v[NR],H[NR],R[NR];
void eliminare ()
{
int i, n=VV; VV=0;
for (i=1; i<=n; ++i)
if (v[i]!=v[i-1])
{
v[++VV]=v[i];
R[VV]=VV; H[VV]=1;
}
}
int cb (int x)
{
int ci=1, cs=VV, mij;
while (ci<=cs)
{
mij=(ci+cs)/2;
if (v[mij]==x) return mij;
else if (x<v[mij]) cs=mij-1;
else ci=mij+1;
}
}
int radacina (int k)
{
while (k!=R[k])
k=R[k];
return k;
}
int main ()
{
freopen ("entries.in", "r", stdin);
freopen ("entries.out", "w", stdout);
scanf ("%d", &n);
for (i=1; i<=n; ++i)
{
scanf ("%d%d%d", &x, &y, &k);
v[++VV]=x; v[++VV]=y;
}
sort (v+1, v+VV+1);
eliminare ();
fclose(stdin);
freopen ("entries.in", "r", stdin);
scanf ("%d", &n);
for (i=1; i<=n; ++i)
{
scanf ("%d%d%d", &x, &y, &k);
x=cb(x); y=cb(y);
Rx=radacina(x);
Ry=radacina(y);
if (k==1) //unim
{
if (Rx!=Ry)
{
if (H[Rx]>H[Ry]) H[Rx]+=H[Ry], R[Ry]=Rx;
else H[Ry]+=H[Rx], R[Rx]=Ry;
}
}
else {
if (Rx==Ry) printf ("1\n");
else printf ("0\n");
}
}
return 0;
}