#include <bits/stdc++.h>
using namespace std ;
const int MAX = 5e5 + 10 ;
int arr[MAX] ;
int n ;
vector<int>v[MAX] ;
int par[MAX] , sz[MAX] ;
int L[MAX] , R[MAX] ;
int mark[MAX] ;
void init()
{
for(int i = 1 ; i <= n ; ++i)
{
par[i] = i , sz[i] = 1 ;
L[i] = R[i] = i ;
}
}
int root(int node)
{
if(par[node] == node)
return node ;
return (par[node] = root(par[node])) ;
}
void Union(int x , int y)
{
int a = root(x) ;
int b = root(y) ;
if(a == b)
return ;
par[b] = a ;
sz[a] += sz[b] ;
L[a] = min(L[a] , L[b]) , R[a] = max(R[a] , R[b]) ;
}
bool HaveKey(int x , int y)
{
x = root(x) ;
int idx = lower_bound(v[y].begin() , v[y].end() , L[x]) - v[y].begin() ;
if(idx == v[y].size())
return false ;
else
return (v[y][idx] <= R[x]) ;
}
int main()
{
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
cin>>n ;
for(int i = 1 ; i <= n-1 ; ++i)
cin>>arr[i] ;
for(int i = 1 ; i <= n ; ++i)
{
int sz ;
cin>>sz ;
v[i].resize(sz) ;
for(auto &j : v[i])
{
cin>>j ;
v[j].push_back(i) ;
}
}
init() ;
stack<int>sr , sl ;
sr.push(n) , sl.push(n) ;
for(int i = n-1 ; i >= 1 ; --i)
{
while(sr.size())
{
int idx = sr.top() ;
int x = root(i) ;
if(HaveKey(x , arr[idx-1]))
R[x] = max(R[x] , R[root(idx)]) , sr.pop() ;
else
break ;
}
while(sl.size())
{
int idx = sl.top() ;
int y = root(idx) ;
if(HaveKey(y , arr[i]))
break ;
else
L[y] = i+1 , sl.pop() ;
}
while(sl.size())
{
int idx = sl.top() ;
int x = root(i) , y = root(idx) ;
if(R[x] == R[y]) // same component
Union(i , idx) , sl.pop() ;
else
break ;
}
sr.push(i) , sl.push(i) ;
}
deque<int>d ;
while(sl.size())
{
int idx = sl.top() ;
int y = root(idx) ;
L[y] = 1 ;
d.push_back(y) ;
sl.pop() ;
}
for(int i = 1 ; i <= n ; ++i)
{
while(d.size())
{
int idx = d.front() ;
int x = root(idx) ;
if(R[x] < i && !mark[arr[i-1]])
d.pop_front() ;
else
break ;
}
if(!d.size())
break ;
int x = root(d.front()) ;
R[x] = max(R[x] , i) ;
int y = root(i) ;
if(L[y] == 1) // was in sl
Union(x , y) ;
for(auto &j : v[i])
mark[j] = 1 ;
}
while(d.size())
{
int idx = d.front() ;
d.pop_front() ;
int y = root(idx) ;
R[y] = n ;
}
int q ;
cin>>q ;
while(q--)
{
int x , y ;
cin>>x>>y ;
x = root(x) ;
if(L[x] <= y && R[x] >= y)
cout<<"YES\n" ;
else
cout<<"NO\n" ;
}
return 0 ;
}
Compilation message
long_mansion.cpp: In function 'bool HaveKey(int, int)':
long_mansion.cpp:48:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | if(idx == v[y].size())
| ~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
12336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
12336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
176 ms |
26036 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
12336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |