#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define u128 unsigned __int128
#define i128 __int128
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define eb emplace_back
#define mt make_tuple
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pli pair<ll, int>
#define pil pair<int, ll>
#define ff first
#define ss second
const ll inf = 9e18;
const int iinf = 2e9;
const int N = 1e5;
const ll MOD = 1e9 + 7;
vector<vector<int>> g;
void solution(){
int n;
cin >> n;
g.resize(n + 1);
bool case23 = true;
for (int i = 0; i < n - 1; ++i){
int x, y;
cin >> x >> y;
g[x].pb(y);
g[y].pb(x);
case23 &= (abs(x - y) == 1);
}
int m;
cin >> m;
vector<pair<int, int>> v(m);
vector<int> c(m);
for (int i = 0; i < m; ++i){
cin >> v[i].ff >> v[i].ss >> c[i];
if (v[i].ff > v[i].ss)
swap(v[i].ff, v[i].ss);
}
if (case23){
sort(all(v), [](auto a, auto b){
return a.ss < b.ss;
});/*
cout << "v = ";
for (int i = 0; i < m; ++i)
cout << "{" << v[i].ff << ", " << v[i].ss << "} ";
cout << "\nl, r : \n";*/
int ans = 0;
int lst_r = -1;
for (int i = 0; i < m; ++i){
if (v[i].ff > lst_r){
ans += c[i];
lst_r = v[i].ss;
}
}
cout << ans;
} else{
}
}
signed main(/* Kurmankul Nurislam */){
//freopen("fcolor.in", "r", stdin);
//freopen("fcolor.out", "w", stdout);
cin.tie(nullptr) -> sync_with_stdio(false);
int t = 1;
//cin >> t;
while (t--){
solution();
//cout << '\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |