이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
using namespace std;
// mt19937 rnd (chrono::steady_clock::now().time_since_epoch().count());
#define sz(s) int(s.size())
#define ll long long
#define ull unsigned long long
#define F first
#define S second
#define pb push_back
#define all(v) v.begin(), v.end()
// #define int ll
#define all(v) v.begin(), v.end()
#define file(s) freopen(s".in", "r", stdin); freopen(s".out", "w", stdout)
const int N=5e4+7, mod=1e9+7, Mod=998244353;
vector<vector<pair<int, int> > > g(N);
ll ans=0, dp[N];
set<int> st;
void dfs(int v, int cnt, int p) {
for(auto to : g[v]) {
if(to.F!=p) {
dp[to.F]=dp[v]+to.S;
if(st.count(to.F)) {
dfs(to.F, to.F, v);
ans+=dp[to.F]-dp[cnt];
// cout << v << ' ' << cnt << ' ' << dp[v] << ' ' << dp[cnt] << '\n';
}else {
dfs(to.F, cnt, v);
}
}
}
}
void solve() {
int n;
cin>>n;
for(int i=1; i<n; i++) {
int x, y, w;
cin>>x>>y>>w;
g[x].pb({y, w});
g[y].pb({x, w});
// if(x>y)swap(x, y);
// mp[{x, y}]=i;
}
int q;
cin>>q;
while(q--) {
ans=0;
st.clear();
int a, b, c, d, e;
cin>>a>>b>>c>>d>>e;
st.insert(a);
st.insert(b);
st.insert(c);
st.insert(d);
st.insert(e);
dp[a]=0;
dfs(a, a, a);
cout << ans << '\n';
}
}
signed main() {
// file("floor4");
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int T=1;
// cin>>T;
while(T--)solve();
}
# | 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... |