#include <bits/stdc++.h>
#pragma GCC target("sse,sse2,avx2")
#pragma GCC optimize("unroll-loops,O2")
#define rep(i,l,r) for (int i = l; i < r; i++)
#define repr(i,r,l) for (int i = r; i >= l; i--)
#define X first
#define Y second
#define all(x) (x).begin() , (x).end()
#define pb push_back
#define endl '\n'
#define debug(x) cerr << #x << " : " << x << endl;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pll;
constexpr int N = 2e5+10,mod = 998244353,inf = 1e9+10;
inline int mkay(int a,int b){
if (a+b >= mod) return a+b-mod;
if (a+b < 0) return a+b+mod;
return a+b;
}
inline int poww(int a,int k){
if (k < 0) return 0;
int z = 1;
while (k){
if (k&1) z = 1ll*z*a%mod;
a = 1ll*a*a%mod;
k /= 2;
}
return z;
}
int par[N][20],h[N];
ll opt[N],d[N][2],up[N];
vector<pair<int,pll>> adj[N];
void pre(int v,int p){
par[v][0] = p;
for (auto u : adj[v]){
if (u.X == p) continue;
h[u.X] = h[v]+1;
d[u.X][0] = d[v][0]+u.Y.X;
d[u.X][1] = d[v][1]+u.Y.Y;
pre(u.X,v);
up[v] += up[u.X]+u.Y.Y;
}
}
int main(){
ios :: sync_with_stdio(0); cin.tie(0);
memset(opt,63,sizeof opt);
int n;
cin >> n;
ll sum = 0;
rep(i,1,n){
int u,v,a,b;
cin >> u >> v >> a >> b;
adj[u].pb({v,{a,b}});
adj[v].pb({u,{b,a}});
sum += a;
sum += b;
}
if (n == 2){
int q;
cin >> q;
while (q--){
int t;
cin >> t;
if (t == 2){
cout << 0 << endl;
continue;
}
if (t == 1){
cout << min(adj[1][0].Y.Y,adj[1][0].Y.X) << endl;
continue;
}
}
return 0;
}
int r = 1;
rep(i,2,n+1) if (adj[i].size() > 1) r = i;
pre(r,0);
rep(j,1,20){
rep(i,1,n+1){
par[i][j] = par[par[i][j-1]][j-1];
}
}
rep(i,1,n+1){
opt[1] = min(opt[1],sum-up[r]+d[i][1]-d[i][0]);
}
int q;
cin >> q;
cout << opt[1];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Incorrect |
4 ms |
6484 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6484 KB |
Output is correct |
2 |
Correct |
237 ms |
35928 KB |
Output is correct |
3 |
Correct |
233 ms |
50636 KB |
Output is correct |
4 |
Correct |
192 ms |
40896 KB |
Output is correct |
5 |
Correct |
218 ms |
42212 KB |
Output is correct |
6 |
Correct |
208 ms |
44060 KB |
Output is correct |
7 |
Correct |
183 ms |
42096 KB |
Output is correct |
8 |
Correct |
243 ms |
53012 KB |
Output is correct |
9 |
Correct |
158 ms |
41256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Incorrect |
195 ms |
35944 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Incorrect |
4 ms |
6484 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6484 KB |
Output is correct |
2 |
Correct |
237 ms |
35928 KB |
Output is correct |
3 |
Correct |
233 ms |
50636 KB |
Output is correct |
4 |
Correct |
192 ms |
40896 KB |
Output is correct |
5 |
Correct |
218 ms |
42212 KB |
Output is correct |
6 |
Correct |
208 ms |
44060 KB |
Output is correct |
7 |
Correct |
183 ms |
42096 KB |
Output is correct |
8 |
Correct |
243 ms |
53012 KB |
Output is correct |
9 |
Correct |
158 ms |
41256 KB |
Output is correct |
10 |
Correct |
3 ms |
6484 KB |
Output is correct |
11 |
Incorrect |
195 ms |
35944 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Incorrect |
4 ms |
6484 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |