# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
261397 | patrikpavic2 | Designated Cities (JOI19_designated_cities) | C++17 | 698 ms | 39440 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 <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <set>
#define X first
#define Y second
#define PB push_back
using namespace std;
typedef long long ll;
typedef pair < int, int > pii;
typedef pair < int, pii > pip;
const int N = 2e5 + 500;
vector < pii > v[N];
vector < pip > v2[N];
set < pii > S;
int n, deg[N], bio[N];
ll cur, bst[N];
ll C[N], uk;
void start(int x, int lst){
for(pip nxt : v2[x]){
if(nxt.X == lst) continue;
C[1] += nxt.Y.X; start(nxt.X, x);
}
}
void dfs(int x, int lst){
for(pip nxt : v2[x]){
if(nxt.X == lst) continue;
C[nxt.X] = C[x] - nxt.Y.X + nxt.Y.Y;
dfs(nxt.X, x);
}
bst[1] = max(bst[1], C[x]);
}
inline int pronadi(int x){
for(pii nxt : v[x])
if(deg[nxt.X])
return nxt.Y;
return 0;
}
int main(){
for(int i = 0;i < N;i++)
bst[i] = 1e18;
scanf("%d", &n);
for(int i = 1;i < n;i++){
int x, y, a, b; scanf("%d%d%d%d", &x, &y, &b, &a);
v[x].PB({y, a}); v[y].PB({x, b});
v2[x].PB({y, {a, b}}); v2[y].PB({x, {b, a}});
uk += a + b;
deg[x]++; deg[y]++;
}
for(int i = 1;i <= n;i++){
if(deg[i] == 1)
S.insert({v[i][0].Y, i});
}
bst[(int)S.size()] = cur;
int lisce = (int)S.size();
for(;S.size() > 2;){
int brs = S.begin() -> Y;
cur += S.begin() -> X; deg[brs] = 0;
S.erase(S.begin());
for(pii nxt : v[brs]){
if(deg[nxt.X] > 0){
deg[nxt.X]--;
if(deg[nxt.X] == 1)
S.insert({pronadi(nxt.X), nxt.X});
break;
}
}
bst[(int)S.size()] = min(bst[(int)S.size()], cur);
}
bst[1] = 0;
start(1, 1); dfs(1, 1);
bst[1] = uk - bst[1];
int q; scanf("%d", &q);
for(;q--;){
int x; scanf("%d", &x);
x = min(x, lisce);
printf("%lld\n", bst[x]);
}
}
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... |