結果
| 問題 |
No.2822 Lights Up! (Tree Edition)
|
| コンテスト | |
| ユーザー |
FplusFplusF
|
| 提出日時 | 2024-06-10 21:03:43 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 5,193 bytes |
| コンパイル時間 | 4,092 ms |
| コンパイル使用メモリ | 280,268 KB |
| 実行使用メモリ | 32,416 KB |
| 最終ジャッジ日時 | 2024-06-18 19:45:38 |
| 合計ジャッジ時間 | 11,058 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 86 WA * 56 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using ull=unsigned long long;
using pll=pair<ll,ll>;
using tll=tuple<ll,ll,ll>;
using ld=long double;
const ll INF=(1ll<<60);
#define rep(i,n) for (ll i=0;i<(ll)(n);i++)
#define replr(i,l,r) for (ll i=(ll)(l);i<(ll)(r);i++)
#define all(v) v.begin(),v.end()
#define len(v) ((ll)v.size())
template<class T> inline bool chmin(T &a,T b){
if(a>b){
a=b;
return true;
}
return false;
}
template<class T> inline bool chmax(T &a,T b){
if(a<b){
a=b;
return true;
}
return false;
}
#include<atcoder/lazysegtree>
ll op(ll a,ll b){
return a^b;
}
ll e(){
return 0;
}
ll mpg(ll f,ll x){
return f^x;
}
ll comp(ll f,ll g){
return f^g;
}
ll id(){
return 0;
}
struct tree{
int n,max_k=1;
vector<vector<int>> g;
vector<vector<int>> parent;
vector<int> depth;
tree(int g_size){
n=g_size;
g.resize(n);
}
void add_edge(int u,int v){
g[u].push_back(v);
g[v].push_back(u);
}
void LCA(int root=0){
while((1<<max_k)<n) max_k++;
parent.assign(max_k,vector<int>(n,-1));
depth.assign(n,-1);
dfs(root,-1,0);
for(int k=0;k+1<max_k;k++){
for(int i=0;i<n;i++){
if(parent[k][i]==-1){
parent[k+1][i]=-1;
}else{
parent[k+1][i]=parent[k][parent[k][i]];
}
}
}
}
void dfs(int v,int par,int d){
parent[0][v]=par;
depth[v]=d;
for(auto &i:g[v]){
if(i!=par){
dfs(i,v,d+1);
}
}
}
int get_lca(int u,int v){
if(depth[u]<depth[v]) swap(u,v);
int diff=depth[u]-depth[v];
for(int k=0;k<max_k;k++){
if(diff&(1<<k)){
u=parent[k][u];
}
}
if(u==v) return u;
for(int k=max_k-1;0<=k;k--){
if(parent[k][u]!=parent[k][v]){
u=parent[k][u];
v=parent[k][v];
}
}
return parent[0][u];
}
int get_dist(int u,int v){
return depth[u]+depth[v]-2*depth[get_lca(u,v)];
}
int on_path(int x,int u,int v){
return get_dist(u,x)+get_dist(x,v)==get_dist(u,v);
}
};
struct HLD{
int n;
vector<vector<int>> g;
vector<int> par,sz,in,out,head;
int ord=0;
HLD(int x){
n=x;
g.resize(n);
par.resize(n);
sz.assign(n,0);
in.resize(n);
out.resize(n);
head.resize(n);
}
void add_edge(int u,int v){
g[u].push_back(v);
g[v].push_back(u);
}
void dfs_sz(int x,int p){
sz[x]++;
if(0<g[x].size()&&g[x].front()==p) swap(g[x].front(),g[x].back());
for(auto &i:g[x]){
if(i==p) continue;
dfs_sz(i,x);
sz[x]+=sz[i];
if(sz[g[x].front()]<sz[i]) swap(g[x].front(),i);
}
}
void dfs_hld(int x,int p){
par[x]=p;
in[x]=ord;
ord++;
for(auto &i:g[x]){
if(i==p) continue;
if(i==g[x].front()) head[i]=head[x];
else head[i]=i;
dfs_hld(i,x);
}
out[x]=ord;
}
void solve(int root){
dfs_sz(root,-1);
dfs_hld(root,-1);
}
vector<pair<int,int>> query(int u,int v,bool edge){
vector<pair<int,int>> ret;
while(true){
if(in[v]<in[u]) swap(u,v);
if(head[u]==head[v]) break;
ret.emplace_back(in[head[v]],in[v]+1);
v=par[head[v]];
}
if(in[u]+edge!=in[v]+1) ret.emplace_back(in[u]+edge,in[v]+1);
return ret;
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll n;
cin >> n;
tree g(n);
vector<ll> p(n,-1);
HLD h(n);
replr(i,1,n){
cin >> p[i];
p[i]--;
g.add_edge(i,p[i]);
h.add_edge(i,p[i]);
}
string s(n,'x');
replr(i,1,n) cin >> s[i];
ll q;
cin >> q;
vector<ll> u(q),v(q);
rep(i,q){
cin >> u[i] >> v[i];
u[i]--;
v[i]--;
}
g.LCA();
vector<int> d=g.depth;
vector<ll> ord(q);
rep(i,q) ord[i]=i;
vector<pll> c(q);
rep(i,q){
c[i]={max(d[u[i]],d[v[i]]),g.get_dist(u[i],v[i])};
}
sort(all(ord),[&](ll i,ll j){
return c[i]>c[j];
});
h.solve(0);
atcoder::lazy_segtree<ll,op,e,ll,mpg,comp,id> seg(n);
auto update=[&](ll a,ll b,ll x){
auto now=h.query(a,b,true);
for(auto [l,r]:now){
seg.apply(l,r,x);
}
};
auto get=[&](ll a,ll b){
auto now=h.query(a,b,true);
ll ret=0;
for(auto [l,r]:now){
ret^=seg.prod(l,r);
}
return ret;
};
replr(i,1,n){
update(i,p[i],s[i]=='#');
}
for(auto i:ord){
ll nu=u[i]==0?0:get(u[i],p[u[i]]),nv=v[i]==0?0:get(v[i],p[v[i]]);
if(nu+nv!=0) update(u[i],v[i],1);
}
ll sum=0;
replr(i,1,n){
sum+=get(i,p[i]);
}
if(sum==0) cout << "Yes\n";
else cout << "No\n";
}
FplusFplusF