이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define fr first
#define sc second
#define mk make_pair
#define pb push_back
#define all(s) s.begin(), s.end()
using namespace std;
const int N = 2e5 + 5;
int n, q, x, y, c, d, cnt;
long long ans, pref[N], res, ALL;
vector < vector < pair <int, pair <int, int> > > > g;
void dfs (int v, int p = 0, long long cur = 0)
{
if (g[v].size() == 1 && p)
cnt++;
for (auto to : g[v]){
if (to.fr == p) continue;
dfs(to.fr, v, cur + to.sc.fr - to.sc.sc);
pref[v] = max( pref[v], pref[to.fr] + to.sc.fr);
ans += to.sc.sc;
ALL += to.sc.fr;
ALL += to.sc.sc;
}
vector <long long> vec;
for (auto to : g[v]){
if (to.fr == p) continue;
vec.pb( pref[to.fr] + to.sc.fr);
}
sort(all(vec));
reverse(all(vec));
if (vec.size() > 1)
res = max(res, vec[0] + vec[1] + cur);
}
main(){
cin >> n;
g.resize(n + 1);
for (int i = 1; i < n; i++){
scanf("%d%d%d%d", &x, &y, &c, &d);
g[x].pb( mk( y, mk(c, d) ) );
g[y].pb( mk( x, mk(d, c) ) );
}
dfs(1);
cin >> q;
while (q--){
cin >> x;
assert(x == 2);
if(cnt == 2)
puts("0");
else
cout << ALL - res - ans << endl;
}
}
/**
4
1 2 1 2
1 3 3 4
1 4 5 6
**/
컴파일 시 표준 에러 (stderr) 메시지
designated_cities.cpp:48:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main(){
^
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:54:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d%d", &x, &y, &c, &d);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |