-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGOT.cpp
More file actions
138 lines (136 loc) · 2.56 KB
/
GOT.cpp
File metadata and controls
138 lines (136 loc) · 2.56 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include<bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=a;i<b;++i)
#define RFOR(i,a,b) for(int i=a;i>=b;--i)
#define ln "\n"
#define mp make_pair
#define pb push_back
#define sz(a) ll(a.size())
#define debug1(x) cout<<x<<endl
#define debug2(x,y) cout<<x<<"-->"<<y<<endl
#define debug3(x,y,z) cout<<x<<"-->"<<y<<"-->"<<z<<endl
#define F first
#define S second
#define all(c) c.begin(),c.end()
#define trace(c,x) for(auto &x:c)
#define pii pair<ll,ll>
typedef long long ll;
typedef long double ld;
typedef priority_queue<pii,std::vector<pii>,greater<pii> > revpr;
const int L=1e5+7;
int a[L],curtime=1,level[L],parent[L][22],counter[L],start[L],ending[L],base[L];
std::vector<int> adj[L];
string out[L];
struct node
{
int l,r,c,q,LCA;
};
void eulerTour(int vertex,int _parent,int _depth)
{
level[vertex]=1+_depth;
parent[vertex][0]=_parent;
start[vertex]=curtime++;
base[curtime-1]=a[vertex];
trace(adj[vertex],x)
if(x!=_parent)
eulerTour(x,vertex,_depth+1);
ending[vertex]=curtime++;
base[curtime-1]=a[vertex];
}
int lca(int a,int b)
{
if(level[a]>level[b])swap(a,b);
int dist=level[b]-level[a];
int index=0;
while(dist>0)
{
if(dist&1){b=parent[b][index];}
dist>>=1;index++;
}
RFOR(i,18,0)
{
if(parent[a][i]!=-1 && parent[a][i]!=parent[b][i])
{
a=parent[a][i];
b=parent[b][i];
}
}
if(a==b)return a;
return parent[a][0];
}
bool comp(node x,node y)
{
if(x.l/900==y.l/900)return x.r<y.r;
return x.l<y.l;
}
void add(int idx)
{
counter[base[idx]]++;
}
void remove(int idx)
{
counter[base[idx]]--;
}
int main()
{
int n,m,u,v,LCA,l,r,c;
node temp;
std::vector<node> vct;
while(scanf("%d%d",&n,&m)!=EOF)
{
FOR(i,1,n+1)scanf("%d",&a[i]);
FOR(i,0,n-1)
{
scanf("%d%d",&u,&v);
adj[u].pb(v);
adj[v].pb(u);
}
curtime=1;
eulerTour(1,-1,0);
FOR(j,1,19)
FOR(i,1,n+1)
if(parent[i][j-1]!=-1)
parent[i][j]=parent[parent[i][j-1]][j-1];
FOR(i,0,m)
{
scanf("%d%d%d",&u,&v,&c);
if(start[u]>start[v])swap(u,v);
LCA=lca(u,v);
temp.l=ending[u],temp.r=start[v],temp.c=c,temp.q=i;
if(LCA==u)temp.LCA=-1;
else temp.LCA=a[LCA];
vct.pb(temp);
}
sort(all(vct),comp);
l=1,r=0;
trace(vct,x)
{
while(r<x.r)
{
++r;
add(r);
}
while(r>x.r)
{
remove(r);
--r;
}
while(l<x.l)
{
remove(l);
l++;
}
while(l>x.l)
{
--l;
add(l);
}
if(x.LCA==x.c || counter[x.c]>0)out[x.q]="Find";
else out[x.q]="NotFind";
}
vct.clear();
FOR(i,0,m)cout<<out[i]<<ln;
printf("\n");
}
return 0;
}