結果

問題 No.1790 Subtree Deletion
ユーザー 👑 NachiaNachia
提出日時 2021-12-20 01:40:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 128 ms / 3,000 ms
コード長 2,654 bytes
コンパイル時間 990 ms
コンパイル使用メモリ 91,300 KB
実行使用メモリ 14,612 KB
最終ジャッジ日時 2023-10-13 19:20:40
合計ジャッジ時間 3,623 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 122 ms
13,760 KB
testcase_04 AC 128 ms
14,560 KB
testcase_05 AC 117 ms
13,760 KB
testcase_06 AC 110 ms
14,608 KB
testcase_07 AC 122 ms
13,756 KB
testcase_08 AC 17 ms
4,348 KB
testcase_09 AC 122 ms
13,856 KB
testcase_10 AC 119 ms
14,612 KB
testcase_11 AC 118 ms
13,860 KB
testcase_12 AC 97 ms
13,360 KB
testcase_13 AC 89 ms
11,660 KB
testcase_14 AC 26 ms
5,440 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <atcoder/fenwicktree>
using namespace std;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
#define rep(i,n) for(int i=0; i<(n); i++)


namespace rq{
    struct XorInt{
        u64 val;
        XorInt(u64 nval=0) : val(nval) {}
        XorInt& operator+=(XorInt r){ val ^= r.val; return *this; }
        XorInt& operator-=(XorInt r){ val ^= r.val; return *this; }
        XorInt operator-(XorInt r) const { return { val ^ r.val }; }
        XorInt operator-() const { return *this; }
    };

    using rq = atcoder::fenwick_tree<XorInt>;
}


struct Edge{ int u,v; u64 a; };


int N;
vector<Edge> edges;
vector<vector<int>> E;
vector<u64> A;
vector<int> I;
vector<int> depth;
vector<int> in_time;
vector<int> out_time;
vector<int> out_time_from_in;


void dfs(int p = 0, int pre = -1){
    in_time[p] = I.size();
    I.push_back(p);
    for(int e : E[p]) if(e != pre){
        depth[e] = depth[p] + 1;
        dfs(e,p);
    }
    out_time[p] = I.size();
}

rq::rq sum_A;
vector<int> erase_visited;


int Q;


int main() {
    cin >> N;
    edges.resize(N-1);
    E.resize(N);
    rep(i,N-1){
        int u,v; cin >> u >> v; u--; v--;
        u64 a; cin >> a;
        edges[i] = {u,v,a};
        E[u].push_back(v);
        E[v].push_back(u);
    }

    depth.assign(N, 0);
    in_time.assign(N, 0);
    out_time.assign(N, 0);
    out_time_from_in.resize(N);
    dfs();
    rep(i,N) out_time_from_in[in_time[i]] = out_time[i];

    E.clear();
    E.resize(N);
    A.assign(N, 0);

    for(auto e : edges){
        if(depth[e.u] > depth[e.v]) swap(e.u, e.v);
        A[e.v] = e.a;
        E[e.u].push_back(e.v);
    }

    erase_visited.assign(N, 0);

    vector<rq::XorInt> sum_A_tmp(N,{0});
    rep(i,N) sum_A_tmp[in_time[i]] = {A[i]};
    sum_A = rq::rq(N);
    rep(i,N) sum_A.add(i, sum_A_tmp[i]);
    
    cin >> Q;
    rep(queryid, Q){
        int t; cin >> t;
        if(t == 1){
            int x; cin >> x; x--;
            for(int p=in_time[x]; p<out_time[x]; ){
                if(erase_visited[p]){ p = out_time_from_in[p]; continue; }
                sum_A.add(p, sum_A_tmp[p]);
                erase_visited[p] = 1;
                p++;
            }
        }
        if(t == 2){
            int x; cin >> x; x--;
            int l = in_time[x] + 1;
            int r = out_time[x];
            u64 ans = sum_A.sum(l,r).val;
            cout << ans << "\n";
        }
    }

    return 0;
}


struct ios_do_not_sync{
    ios_do_not_sync(){
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
    }
} ios_do_not_sync_instance;

0