#include <bits/stdc++.h>
#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define sol (k+k)
#define sag (k+k+1)
#define orta ((bas+son)/2)
#define coc g[node][i].st
#define yol g[node][i].nd.st
#define tyol g[node][i].nd.nd
#define mod 1000000007
#define inf 1000000009
#define N 1000005
using namespace std;
typedef long long ll;
typedef pair < ll , ll > ii;
typedef pair < ll , ii > iii;
ll n, ans, bir, iki, top, say[N];
ii dp[N];
vector < iii > g[N];
void hazirla(ll node, ll par){
for(ll i = 0; i < g[node].size(); i++)
if(coc != par){
hazirla(coc, node);
say[node] += say[coc] + tyol;
}
}
void dfs(ll node, ll par, ll us){
ll top = us + say[node];
ii mx = mp(0, node), mx2 = mp(0, node);
for(ll i = 0; i < g[node].size(); i++)
if(coc != par){
dfs(coc, node, us + say[node] - (say[coc] + tyol) + yol);
mx2 = max(mx2, mp(-say[coc] - tyol + tyol + yol + dp[coc].st, dp[coc].nd) );
if(mx2 > mx)
swap(mx, mx2);
}
if(top + mx.st + mx2.st > ans){
ans = top + mx.st + mx2.st;
bir = mx.nd;
iki = mx2.nd;
}
if(say[node] + mx.st > ans){
ans = say[node] + mx.st;
bir = mx.nd;
iki = node;
}
dp[node] = mp(say[node] + mx.st, mx.nd);
// cout << node << " " << say[node] << " " << top << " " << dp[node].st << " " << dp[node].nd << endl;
}
int main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
scanf("%lld",&n);
for(ll i = 1; i < n; i++){
ll a, b, c, d;
scanf("%lld %lld %lld %lld",&a ,&b ,&c ,&d);
top += c + d;
g[a].pb(mp(b, mp(c, d)));
g[b].pb(mp(a, mp(d, c)));
}
hazirla(1, 0);
dfs(1, 0, 0);
// cout << bir << " " << iki << " " << ans << endl;
printf("%lld\n", top - ans);
return 0;
}
Compilation message
designated_cities.cpp: In function 'void hazirla(ll, ll)':
designated_cities.cpp:26:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(ll i = 0; i < g[node].size(); i++)
~~^~~~~~~~~~~~~~~~
designated_cities.cpp: In function 'void dfs(ll, ll, ll)':
designated_cities.cpp:36:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(ll i = 0; i < g[node].size(); i++)
~~^~~~~~~~~~~~~~~~
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:61:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld",&n);
~~~~~^~~~~~~~~~~
designated_cities.cpp:64:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld %lld %lld %lld",&a ,&b ,&c ,&d);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
24 ms |
23800 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
24 ms |
23800 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
23800 KB |
Output is correct |
2 |
Correct |
332 ms |
47376 KB |
Output is correct |
3 |
Correct |
413 ms |
74164 KB |
Output is correct |
4 |
Correct |
321 ms |
46616 KB |
Output is correct |
5 |
Correct |
327 ms |
47204 KB |
Output is correct |
6 |
Correct |
338 ms |
51204 KB |
Output is correct |
7 |
Correct |
282 ms |
45412 KB |
Output is correct |
8 |
Correct |
467 ms |
62328 KB |
Output is correct |
9 |
Correct |
223 ms |
43764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
24 ms |
23800 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
24 ms |
23800 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
24 ms |
23800 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |