-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path014-1-1-1-free-tree.cpp
More file actions
100 lines (80 loc) · 2.34 KB
/
Copy path014-1-1-1-free-tree.cpp
File metadata and controls
100 lines (80 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* 1-1-1, Free Tree! [2126F]
* Problem: https://codeforces.com/problemset/problem/2126/F
* Verdict: ACCEPTED Solved: 2025-10-23
* Language: C++23 (GCC 14-64, msys2)
* Runtime: 765 ms Memory: 74800 KB
* Tags: brute force, data structures, dfs and similar, graphs, implementation, trees
* Author: BidoTeima
* Source: https://codeforces.com/contest/2126/submission/345425372
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5 + 3;
int par[N],a[N],cost[N],costNode[N];
vector<pair<int,int>>adj[N];
map<int,ll>sum[N]; // sum[v][c] -> sum of children of v w/ color c
void dfs(int node, int p = -1){
par[node] = p;
for(auto& [child, edge] : adj[node]){
if(child == p) continue;
sum[node][a[child]] += cost[edge];
costNode[child] = cost[edge];
dfs(child, node);
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--){
int n,q;
cin>>n>>q;
for(int i = 1; i <= n; i++)
cin >> a[i],par[i]=-1,sum[i].clear(),adj[i].clear();
ll tot = 0;
for(int i = 0; i + 1 < n; i++){
int u, v, c;
cin >> u >> v >> c;
adj[u].push_back({v, i});
adj[v].push_back({u, i});
cost[i] = c;
if(a[u] != a[v])
tot += c;
}
dfs(1);
/*for(int i = 1; i <= n; i++){
cout << par[i] << ' ';
}cout << '\n';*/
while(q--) {
int v, x;
cin >> v >> x;
int cur = a[v];
a[v] = x;
if(cur == x){
cout << tot << '\n';
continue;
}
tot += sum[v][cur] - sum[v][x];
if(par[v] != -1){
// cout << v << ": " << costNode[v] << '\n';
if(a[par[v]] == x){
tot -= costNode[v];
}else if(a[par[v]] == cur){
tot += costNode[v];
}
sum[par[v]][x] += costNode[v];
sum[par[v]][cur] -= costNode[v];
}else{
// cout << v << ": gigachad. Or is it?\n";
}
cout << tot << '\n';
}
// cout << '\n';
}
return 0;
}