結果

問題 No.2822 Lights Up! (Tree Edition)
ユーザー FplusFplusFFplusFplusF
提出日時 2024-06-28 16:54:59
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 4,510 bytes
コンパイル時間 3,606 ms
コンパイル使用メモリ 263,572 KB
実行使用メモリ 14,336 KB
最終ジャッジ日時 2024-06-28 16:55:24
合計ジャッジ時間 14,029 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 AC 86 ms
13,952 KB
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 AC 3 ms
6,944 KB
testcase_19 AC 5 ms
6,940 KB
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 AC 5 ms
6,948 KB
testcase_24 RE -
testcase_25 AC 7 ms
6,940 KB
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 WA -
testcase_42 RE -
testcase_43 RE -
testcase_44 RE -
testcase_45 RE -
testcase_46 RE -
testcase_47 RE -
testcase_48 RE -
testcase_49 RE -
testcase_50 RE -
testcase_51 RE -
testcase_52 RE -
testcase_53 RE -
testcase_54 RE -
testcase_55 RE -
testcase_56 RE -
testcase_57 RE -
testcase_58 AC 2 ms
6,940 KB
testcase_59 AC 3 ms
6,944 KB
testcase_60 RE -
testcase_61 RE -
testcase_62 RE -
testcase_63 RE -
testcase_64 RE -
testcase_65 RE -
testcase_66 RE -
testcase_67 RE -
testcase_68 RE -
testcase_69 RE -
testcase_70 WA -
testcase_71 RE -
testcase_72 RE -
testcase_73 RE -
testcase_74 RE -
testcase_75 RE -
testcase_76 RE -
testcase_77 RE -
testcase_78 RE -
testcase_79 RE -
testcase_80 RE -
testcase_81 RE -
testcase_82 RE -
testcase_83 RE -
testcase_84 AC 2 ms
6,944 KB
testcase_85 RE -
testcase_86 RE -
testcase_87 WA -
testcase_88 AC 1 ms
6,944 KB
testcase_89 RE -
testcase_90 RE -
testcase_91 AC 2 ms
6,944 KB
testcase_92 RE -
testcase_93 RE -
testcase_94 AC 2 ms
6,944 KB
testcase_95 AC 2 ms
6,940 KB
testcase_96 RE -
testcase_97 RE -
testcase_98 WA -
testcase_99 RE -
testcase_100 RE -
testcase_101 RE -
testcase_102 WA -
testcase_103 AC 2 ms
6,940 KB
testcase_104 RE -
testcase_105 RE -
testcase_106 RE -
testcase_107 RE -
testcase_108 RE -
testcase_109 RE -
testcase_110 RE -
testcase_111 RE -
testcase_112 RE -
testcase_113 RE -
testcase_114 RE -
testcase_115 RE -
testcase_116 RE -
testcase_117 RE -
testcase_118 RE -
testcase_119 RE -
testcase_120 RE -
testcase_121 RE -
testcase_122 RE -
testcase_123 RE -
testcase_124 RE -
testcase_125 RE -
testcase_126 RE -
testcase_127 RE -
testcase_128 RE -
testcase_129 RE -
testcase_130 RE -
testcase_131 RE -
testcase_132 RE -
testcase_133 RE -
testcase_134 RE -
testcase_135 RE -
testcase_136 RE -
testcase_137 RE -
testcase_138 RE -
testcase_139 RE -
testcase_140 AC 2 ms
6,944 KB
testcase_141 AC 2 ms
6,940 KB
testcase_142 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#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 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;
    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);
    fenwick_tree<ll> BIT(n);

    auto update=[&](ll a,ll b){
        auto now=h.query(a,b,true);
        for(auto [l,r]:now){
            BIT.add(l,r,1);
        }
    };

    auto get=[&](ll a,ll b){
        auto now=h.query(a,b,true);
        return BIT.get(now[0].first);
    };
    rep(i,q) update(u[i],v[i]);
    replr(i,1,n){
        if(s[i]=='#'&&get(i,p[i])==0){
            cout << "No\n";
            return 0;
        }
    }
    rep(i,n){
        BIT.add(i,i+1,-BIT.get(i));
    }
    rep(i,q){
        if(u[i]!=0) update(u[i],p[u[i]]);
        if(v[i]!=0) update(v[i],p[v[i]]);
    }
    bool ok=false;
    replr(i,1,n){
        if(s[i]=='#'&&get(i,p[i])!=0){
            ok=true;
        }
    }
    if(ok) assert(0);
    else cout << "No\n";
}
0