結果
| 問題 |
No.899 γatheree
|
| コンテスト | |
| ユーザー |
milanis48663220
|
| 提出日時 | 2020-06-07 11:29:19 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,735 bytes |
| コンパイル時間 | 1,261 ms |
| コンパイル使用メモリ | 90,168 KB |
| 実行使用メモリ | 21,376 KB |
| 最終ジャッジ日時 | 2024-12-23 16:35:37 |
| 合計ジャッジ時間 | 8,824 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | WA * 23 |
ソースコード
#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){
if(x != root_pos[r]){
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;
}
}
milanis48663220