結果

問題 No.899 γatheree
ユーザー milanis48663220milanis48663220
提出日時 2020-06-07 11:24:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,671 bytes
コンパイル時間 1,005 ms
コンパイル使用メモリ 89,908 KB
実行使用メモリ 26,588 KB
最終ジャッジ日時 2023-08-25 04:22:43
合計ジャッジ時間 8,712 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
20,040 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
const int N_MAX = 160000;
const int N_LOG_MAX = 25;
int root;

using namespace std;
typedef long long ll;

struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) { }
  bool unionSet(int x, int y) {
    x = root(x); y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y]; data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) {
    return root(x) == root(y);
  }
  int root(int x) {
    return data[x] < 0 ? x : data[x] = root(data[x]);
  }
  int size(int x) {
    return -data[root(x)];
  }
};

int parent[N_LOG_MAX][N_MAX];
int depth[N_MAX];
vector<int> G[100000];

void dfs(int v, int p, int d){
    parent[0][v] = p;
    depth[v] = d;
    for(int i = 0; i < G[v].size(); i++){
        if(G[v][i] != p) dfs(G[v][i], v, d+1); 
    }
}

void init(int V){
    dfs(root, -1, 0);
    for(int i = 0; i < N_LOG_MAX-1; i++){
        for(int v = 0; v < V; v++){
            if(parent[i][v] < 0) parent[i+1][v] = -1;
            else parent[i+1][v] = parent[i][parent[i][v]];
        }
    }
}

int lca(int u, int v){
    if(depth[u] > depth[v]) swap(u, v);
    for(int i = 0; i < N_LOG_MAX; i++){
        if((depth[v] - depth[u]) >> i & 1){
            v = parent[i][v];
        }
    }
    if(u == v) return u;
    for(int i = N_LOG_MAX-1; i >= 0; i--){
        if(parent[i][u] != parent[i][v]) {
            u = parent[i][u];
            v = parent[i][v];
        }
    }
    return parent[0][u];
}

int dist(int u, int v){
    int lca_ = lca(u, v);
    return depth[u]+depth[v]-2*depth[lca_];
}

int N;
ll A[100000];
ll root_pos[100000];
bool used[100000];
bool used1[100000];
bool used2[100000];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    cin >> N;
    for(int i = 0; i < N-1; i++){
        int u, v;
        cin >> u >> v;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    for(int i = 0; i < N; i++) cin >> A[i];
    for(int i = 0; i < N; i++) root_pos[i] = i;
    init(N);
    int Q;
    cin >> Q;
    UnionFind uf(N);
    for(int i = 0; i < Q; i++){
        int x;
        cin >> x;
        // cout << x << '-' << A[x] << endl;
        if(used2[x]){
            int r = uf.root(x);
            if(dist(x, root_pos[r]) <= 2){
                A[x] += A[root_pos[r]];
                A[root_pos[r]]  = 0;
                root_pos[r] = x;
            }
        }else{
            for(int to1 : G[x]){
                // cout << to1 << endl;
                int r1 = uf.root(to1);
                if(dist(x, root_pos[r1]) <= 2){
                    A[x] += A[root_pos[r1]];
                    A[root_pos[r1]] = 0;
                    uf.unionSet(x, to1);
                    // int r1 = uf.root(x);
                    // root_pos[r1] = x;
                }
                if(!used1[to1]){
                    used1[to1] = true;
                    for(int to2 : G[to1]){
                        if(to2 == x) continue;
                        int r2 = uf.root(to2);
                        if(dist(x, root_pos[r2]) <= 2){
                            A[x] += A[root_pos[r2]];
                            A[root_pos[r2]] = 0;
                            uf.unionSet(x, to2);
                            // int r2 = uf.root(x);
                            // root_pos[r2] = x;
                        }
                    }
                }
            }
            int r = uf.root(x);
            root_pos[r] = x;
            used2[x] = true;
        }
        cout << A[x] << endl;
    }
}
0