이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=2e5+5;
vector<tuple<int, ll, ll> > g[MAXN];
pair<ll, int> mprof[MAXN][2];
ll dp[MAXN];
ll respf[MAXN], somaf;
int n, q;
void melhora(int cur, pair<ll, int> val) {
if(val.first>=mprof[cur][0].first) {
mprof[cur][1]=mprof[cur][0];
mprof[cur][0]=val;
}
else if(val.first>mprof[cur][1].first) {
mprof[cur][1]=val;
}
}
void predfs(int cur, int p) {
for(auto aresta : g[cur]) {
int viz; ll peso, rpeso;
tie(viz, peso, rpeso)=aresta;
if(viz==p) continue;
predfs(viz, cur);
dp[cur]+=dp[viz]+rpeso;
melhora(cur, {mprof[viz][0].first+peso, viz});
}
}
void dfs(int cur, int p) {
respf[1]=max(respf[1], dp[cur]);
respf[2]=max(respf[2], dp[cur]+mprof[cur][0].first);
// printf("dp[%d]= %lld // mprof[%d][0]= %lld, %d\n", cur, dp[cur], cur, mprof[cur][0].first, mprof[cur][0].second);
for(auto aresta : g[cur]) {
int viz; ll peso, rpeso;
tie(viz, peso, rpeso)=aresta;
if(viz==p) continue;
ll dpc=dp[cur], dpv=dp[viz];
auto mprofc0=mprof[cur][0], mprofc1=mprof[cur][1];
auto mprofv0=mprof[viz][0], mprofv1=mprof[viz][1];
dp[cur]-=(dp[viz]+rpeso);
dp[viz]+=(dp[cur]+peso);
if(mprof[cur][0].second==viz) mprof[cur][0]=mprof[cur][1];
melhora(viz, {mprof[cur][0].first+rpeso, cur});
dfs(viz, cur);
dp[cur]=dpc; dp[viz]=dpv;
mprof[cur][0]=mprofc0; mprof[cur][1]=mprofc1;
mprof[viz][0]=mprofv0; mprof[viz][1]=mprofv1;
}
}
int main() {
scanf("%d", &n);
for(int i=1; i<n; i++) {
int a, b, c, d; scanf("%d %d %d %d", &a, &b, &c, &d);
g[a].push_back({b, c, d}); g[b].push_back({a, d, c});
somaf+=c; somaf+=d;
}
predfs(1, 1);
dfs(1, 1);
scanf("%d", &q);
while(q--) {
int val; scanf("%d", &val);
printf("%lld\n", somaf-respf[val]);
}
}
컴파일 시 표준 에러 (stderr) 메시지
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("%d", &n);
~~~~~^~~~~~~~~~
designated_cities.cpp:63:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int a, b, c, d; scanf("%d %d %d %d", &a, &b, &c, &d);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:71:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &q);
~~~~~^~~~~~~~~~
designated_cities.cpp:73:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int val; scanf("%d", &val);
~~~~~^~~~~~~~~~~~
# | 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... |