結果

問題 No.2822 Lights Up! (Tree Edition)
ユーザー GOTKAKO
提出日時 2024-07-27 16:24:06
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 69 ms / 2,000 ms
コード長 1,691 bytes
コンパイル時間 2,583 ms
コンパイル使用メモリ 208,072 KB
最終ジャッジ日時 2025-02-23 19:11:28
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 142
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int N; cin >> N;
    vector<int> P(N-1);
    for(auto &p : P) cin >> p,p--;
    string s; cin >> s;
    s.insert(s.begin(),'.');
    vector<vector<int>> Graph(N);
    for(int i=0; i<N-1; i++){
        Graph.at(P.at(i)).push_back(i+1);
        Graph.at(i+1).push_back(P.at(i));
    }
    vector<int> diff(N);
    auto dfs = [&](auto dfs,int pos,int back) -> void {
        bool leaf = true;
        int black = 0;
        for(auto to : Graph.at(pos)){
            if(to == back) continue;
            leaf = false;
            dfs(dfs,to,pos);
            if(s.at(to) == '#') black ^= 1;  
        }
        if(leaf && s.at(pos) == '#') diff.at(pos) = 1;
        if(!leaf){
            if(s.at(pos) == '#') diff.at(pos) = black^1;
            else diff.at(pos) = black; 
        }
    };
    dfs(dfs,0,-1);
    for(auto &d : diff) d &= 1;

    Graph.assign(N,{});
    int K; cin >> K;
    while(K--){
        int u,v; cin >> u >> v;
        u--; v--;
        Graph.at(u).push_back(v);
        Graph.at(v).push_back(u);
    }
    vector<bool> already(N);
    bool yes = true;
    for(int i=0; i<N; i++) if(already.at(i) == false){
        already.at(i) = true;
        queue<int> Q; Q.push(i);
        int sumd = 0;
        while(Q.size()){
            int pos = Q.front(); Q.pop();
            sumd += diff.at(pos);
            for(auto to : Graph.at(pos)) if(already.at(to) == false){
                already.at(to) = true; Q.push(to); 
            }
        }
        if(sumd%2) yes = false;
    }
    if(yes) cout << "Yes" << endl;
    else cout << "No" << endl;
}
0