結果

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

ソースコード

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;
        for(auto to : Graph.at(pos)){
            if(to == back) continue;
            dfs(dfs,to,pos); leaf = false;
            if(s.at(pos) != s.at(to)) diff.at(pos)++;
        }
        if(leaf && s.at(pos) == '#') diff.at(pos)++;
    };
    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