結果
問題 | No.2822 Lights Up! (Tree Edition) |
ユーザー | FplusFplusF |
提出日時 | 2024-07-11 17:05:27 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 129 ms / 2,000 ms |
コード長 | 6,744 bytes |
コンパイル時間 | 4,026 ms |
コンパイル使用メモリ | 275,324 KB |
実行使用メモリ | 24,756 KB |
最終ジャッジ日時 | 2024-07-26 20:25:26 |
合計ジャッジ時間 | 10,942 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 109 ms
24,124 KB |
testcase_03 | AC | 77 ms
21,108 KB |
testcase_04 | AC | 69 ms
21,132 KB |
testcase_05 | AC | 129 ms
24,756 KB |
testcase_06 | AC | 66 ms
21,004 KB |
testcase_07 | AC | 76 ms
20,812 KB |
testcase_08 | AC | 36 ms
9,600 KB |
testcase_09 | AC | 63 ms
19,748 KB |
testcase_10 | AC | 94 ms
17,996 KB |
testcase_11 | AC | 42 ms
13,312 KB |
testcase_12 | AC | 37 ms
10,752 KB |
testcase_13 | AC | 67 ms
10,624 KB |
testcase_14 | AC | 85 ms
14,976 KB |
testcase_15 | AC | 45 ms
12,504 KB |
testcase_16 | AC | 3 ms
6,944 KB |
testcase_17 | AC | 4 ms
6,944 KB |
testcase_18 | AC | 7 ms
6,944 KB |
testcase_19 | AC | 5 ms
6,940 KB |
testcase_20 | AC | 4 ms
6,940 KB |
testcase_21 | AC | 10 ms
6,944 KB |
testcase_22 | AC | 6 ms
6,940 KB |
testcase_23 | AC | 8 ms
6,944 KB |
testcase_24 | AC | 6 ms
6,940 KB |
testcase_25 | AC | 4 ms
6,940 KB |
testcase_26 | AC | 7 ms
6,944 KB |
testcase_27 | AC | 3 ms
6,940 KB |
testcase_28 | AC | 8 ms
6,940 KB |
testcase_29 | AC | 12 ms
6,940 KB |
testcase_30 | AC | 8 ms
6,940 KB |
testcase_31 | AC | 4 ms
6,940 KB |
testcase_32 | AC | 8 ms
6,944 KB |
testcase_33 | AC | 8 ms
6,940 KB |
testcase_34 | AC | 9 ms
6,944 KB |
testcase_35 | AC | 8 ms
6,940 KB |
testcase_36 | AC | 11 ms
6,944 KB |
testcase_37 | AC | 7 ms
6,944 KB |
testcase_38 | AC | 8 ms
6,940 KB |
testcase_39 | AC | 8 ms
6,940 KB |
testcase_40 | AC | 9 ms
6,944 KB |
testcase_41 | AC | 7 ms
6,940 KB |
testcase_42 | AC | 8 ms
6,944 KB |
testcase_43 | AC | 11 ms
6,940 KB |
testcase_44 | AC | 7 ms
6,940 KB |
testcase_45 | AC | 12 ms
6,944 KB |
testcase_46 | AC | 8 ms
6,940 KB |
testcase_47 | AC | 12 ms
6,940 KB |
testcase_48 | AC | 2 ms
6,940 KB |
testcase_49 | AC | 3 ms
6,940 KB |
testcase_50 | AC | 3 ms
6,940 KB |
testcase_51 | AC | 3 ms
6,940 KB |
testcase_52 | AC | 3 ms
6,940 KB |
testcase_53 | AC | 3 ms
6,940 KB |
testcase_54 | AC | 2 ms
6,944 KB |
testcase_55 | AC | 2 ms
6,944 KB |
testcase_56 | AC | 3 ms
6,940 KB |
testcase_57 | AC | 2 ms
6,940 KB |
testcase_58 | AC | 2 ms
6,940 KB |
testcase_59 | AC | 2 ms
6,940 KB |
testcase_60 | AC | 2 ms
6,944 KB |
testcase_61 | AC | 3 ms
6,944 KB |
testcase_62 | AC | 3 ms
6,940 KB |
testcase_63 | AC | 2 ms
6,944 KB |
testcase_64 | AC | 3 ms
6,944 KB |
testcase_65 | AC | 2 ms
6,944 KB |
testcase_66 | AC | 3 ms
6,940 KB |
testcase_67 | AC | 2 ms
6,944 KB |
testcase_68 | AC | 3 ms
6,940 KB |
testcase_69 | AC | 2 ms
6,944 KB |
testcase_70 | AC | 2 ms
6,940 KB |
testcase_71 | AC | 2 ms
6,940 KB |
testcase_72 | AC | 2 ms
6,940 KB |
testcase_73 | AC | 2 ms
6,940 KB |
testcase_74 | AC | 3 ms
6,944 KB |
testcase_75 | AC | 3 ms
6,940 KB |
testcase_76 | AC | 3 ms
6,944 KB |
testcase_77 | AC | 3 ms
6,944 KB |
testcase_78 | AC | 3 ms
6,940 KB |
testcase_79 | AC | 2 ms
6,944 KB |
testcase_80 | AC | 2 ms
6,940 KB |
testcase_81 | AC | 2 ms
6,940 KB |
testcase_82 | AC | 2 ms
6,940 KB |
testcase_83 | AC | 2 ms
6,944 KB |
testcase_84 | AC | 2 ms
6,940 KB |
testcase_85 | AC | 2 ms
6,944 KB |
testcase_86 | AC | 2 ms
6,940 KB |
testcase_87 | AC | 2 ms
6,940 KB |
testcase_88 | AC | 2 ms
6,940 KB |
testcase_89 | AC | 1 ms
6,940 KB |
testcase_90 | AC | 2 ms
6,940 KB |
testcase_91 | AC | 2 ms
6,944 KB |
testcase_92 | AC | 2 ms
6,944 KB |
testcase_93 | AC | 2 ms
6,940 KB |
testcase_94 | AC | 3 ms
6,940 KB |
testcase_95 | AC | 2 ms
6,940 KB |
testcase_96 | AC | 2 ms
6,944 KB |
testcase_97 | AC | 2 ms
6,940 KB |
testcase_98 | AC | 2 ms
6,944 KB |
testcase_99 | AC | 2 ms
6,940 KB |
testcase_100 | AC | 2 ms
6,940 KB |
testcase_101 | AC | 2 ms
6,940 KB |
testcase_102 | AC | 2 ms
6,944 KB |
testcase_103 | AC | 2 ms
6,940 KB |
testcase_104 | AC | 2 ms
6,940 KB |
testcase_105 | AC | 2 ms
6,940 KB |
testcase_106 | AC | 2 ms
6,944 KB |
testcase_107 | AC | 2 ms
6,940 KB |
testcase_108 | AC | 2 ms
6,940 KB |
testcase_109 | AC | 2 ms
6,944 KB |
testcase_110 | AC | 2 ms
6,940 KB |
testcase_111 | AC | 2 ms
6,944 KB |
testcase_112 | AC | 3 ms
6,944 KB |
testcase_113 | AC | 2 ms
6,940 KB |
testcase_114 | AC | 2 ms
6,940 KB |
testcase_115 | AC | 2 ms
6,944 KB |
testcase_116 | AC | 2 ms
6,944 KB |
testcase_117 | AC | 2 ms
6,940 KB |
testcase_118 | AC | 3 ms
6,944 KB |
testcase_119 | AC | 2 ms
6,940 KB |
testcase_120 | AC | 2 ms
6,944 KB |
testcase_121 | AC | 2 ms
6,944 KB |
testcase_122 | AC | 2 ms
6,944 KB |
testcase_123 | AC | 2 ms
6,940 KB |
testcase_124 | AC | 2 ms
6,944 KB |
testcase_125 | AC | 2 ms
6,944 KB |
testcase_126 | AC | 2 ms
6,940 KB |
testcase_127 | AC | 2 ms
6,944 KB |
testcase_128 | AC | 2 ms
6,944 KB |
testcase_129 | AC | 2 ms
6,944 KB |
testcase_130 | AC | 2 ms
6,944 KB |
testcase_131 | AC | 2 ms
6,944 KB |
testcase_132 | AC | 2 ms
6,944 KB |
testcase_133 | AC | 2 ms
6,940 KB |
testcase_134 | AC | 2 ms
6,940 KB |
testcase_135 | AC | 2 ms
6,944 KB |
testcase_136 | AC | 2 ms
6,944 KB |
testcase_137 | AC | 2 ms
6,940 KB |
testcase_138 | AC | 3 ms
6,940 KB |
testcase_139 | AC | 2 ms
6,940 KB |
testcase_140 | AC | 2 ms
6,940 KB |
testcase_141 | AC | 2 ms
6,944 KB |
testcase_142 | AC | 2 ms
6,944 KB |
testcase_143 | AC | 2 ms
6,940 KB |
ソースコード
#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; } template<class T> struct fenwick_tree{ vector<T> v; int n; fenwick_tree(int x){ n=x+1; v.assign(n+1,0); } fenwick_tree(vector<T> &a){ n=(int)a.size()+1; v.assign(n+1,0); if((int)a.size()==0) return; v[1]=a[0]; for(int i=0;i<n-2;i++) v[i+2]=a[i+1]-a[i]; for(int idx=1;idx<=n;idx++){ if(n<idx+(idx&(-idx))) continue; v[idx+(idx&(-idx))]+=v[idx]; } } void add_sub(int i,T x){ i++; for(int idx=i;idx<=n;idx+=(idx&(-idx))){ v[idx]+=x; } } void add(int l,int r,T x){ //[l,r) assert(0<=l&&l<=n); assert(0<=r&&r<=n); assert(l<=r); add_sub(l,x); add_sub(r,-x); } T get(int i){ assert(0<=i&&i<n); i++; T ret=0; for(int idx=i;0<idx;idx-=(idx&(-idx))){ ret+=v[idx]; } return ret; } int lower_bound(T w){ if(w<=0) return 0; int x=0; static int r=1; if(r==1){ while((r<<1)<=n) r<<=1; } while((r<<1)<=n) r<<=1; for(int k=r;0<k;k>>=1){ if(x+k<=n&&v[x+k]<w){ w-=v[x+k]; x+=k; } } x++; return x; } }; 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 doubling(int x,int diff){ for(int k=0;k<max_k;k++){ if(diff&(1<<k)){ x=parent[k][x]; } } return x; } 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 in_ord=0,out_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]=in_ord; in_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]=out_ord; out_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; } }; vector<ll> out; bool comp(ll x,ll y){ return out[x]<out[y]; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); ll n; cin >> n; vector<ll> p(n,-1); HLD h(n); replr(i,1,n){ cin >> p[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]--; } h.solve(0); out.resize(n); vector<ll> ord(n,INF); rep(i,n) out[i]=h.out[i]; rep(i,n) ord[out[i]]=i; vector<set<ll,decltype(&comp)>> lr(n,set<ll,decltype(&comp)>(&comp)); rep(i,q){ if(out[v[i]]<out[u[i]]) swap(u[i],v[i]); lr[u[i]].insert(v[i]); } vector<ll> nr(n,-1); for(auto i:ord){ if(lr[i].empty()) continue; nr[i]=*lr[i].begin(); lr[i].erase(nr[i]); if(n<=nr[i]) continue; if(len(lr[nr[i]])<len(lr[i])) lr[i].swap(lr[nr[i]]); lr[nr[i]].merge(lr[i]); lr[i].clear(); } fenwick_tree<ll> BIT(n); auto add=[&](ll a,ll b){ auto now=h.query(a,b,true); for(auto [l,r]:now){ BIT.add(l,r,1); } }; auto get=[&](ll x){ auto now=h.query(x,p[x],true); return BIT.get(now[0].first); }; replr(i,1,n){ if(s[i]=='#') add(i,p[i]); } for(auto i:ord){ if(nr[i]==-1) continue; if(!(get(i)&1)) continue; add(i,nr[i]); } replr(i,1,n){ if(get(i)&1){ cout << "No\n"; return 0; } } cout << "Yes\n"; }