# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
114311 | tincamatei | Designated Cities (JOI19_designated_cities) | C++14 | 411 ms | 31224 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 200000;
struct Edge {
int a, b, c, d;
int other(int nod) {
return a ^ b ^ nod;
}
int cost(int src) {
if(src == a)
return c;
else
return d;
}
} edges[MAX_N - 1];
vector<int> graph[1+MAX_N];
long long rez[1+MAX_N];
void calcRoot(int nod, int papa = 0) {
for(auto it: graph[nod]) {
int son = edges[it].other(nod);
if(son != papa) {
rez[1] += edges[it].cost(nod);
calcRoot(son, nod);
}
}
}
long long calcBestPathFor1(int nod, int papa = 0, long long extra = 0LL) {
long long rez = extra;
for(auto it: graph[nod]) {
int son = edges[it].other(nod);
if(son != papa) {
long long sonRez = calcBestPathFor1(son, nod, extra - edges[it].cost(nod) + edges[it].cost(son));
rez = min(rez, sonRez);
}
}
return rez;
}
int main() {
int N, Q;
int leaves = 0;
scanf("%d", &N);
for(int i = 0; i < N - 1; ++i) {
scanf("%d%d%d%d", &edges[i].a, &edges[i].b, &edges[i].c, &edges[i].d);
graph[edges[i].a].push_back(i);
graph[edges[i].b].push_back(i);
}
for(int i = 1; i <= N; ++i)
if(graph[i].size() == 1)
++leaves;
rez[1] = calcBestPathFor1(1);
calcRoot(1);
scanf("%d", &Q);
for(int i = 0; i < Q; ++i) {
int x;
scanf("%d", &x);
printf("%lld\n", rez[x]);
}
return 0;
}
Compilation message (stderr)
# | 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... |