답안 #118223

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
118223 2019-06-18T11:43:39 Z songc Designated Cities (JOI19_designated_cities) C++14
9 / 100
462 ms 25468 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL, int> pii;

int N, Q;
vector<pii> G[202020];
int deg[202020];
bool chk[202020];
LL ans[202020];

priority_queue<pii> PQ;

LL d(int u, int p){
    LL ret = 0;
    for (pii v : G[u]){
        if (v.second != p) ret += d(v.second, u);
        else ret += v.first;
    }
    return ret;
}

void f(int u, int p, LL c){
    for (pii v : G[u]) {
        if (v.second != p) f(v.second, u, c+v.first);
        else c -= v.first;
    }
    ans[1] = min(ans[1], c);
}

int main(){
    int u, v;
    LL x, y;
    scanf("%d", &N);
    for (int i=1; i<N; i++){
        scanf("%d %d %lld %lld", &u, &v, &x, &y);
        G[u].push_back(pii(y, v));
        G[v].push_back(pii(x, u));
        deg[u]++, deg[v]++;
    }
    for (int i=1; i<=N; i++) if (deg[i] == 1) PQ.push(pii(-G[i][0].first, i));
    while (PQ.size()>2){
        pii T = PQ.top();
        PQ.pop();
        chk[T.second] = true;
        for (pii it : G[T.second]){
            if (!chk[it.second]){
                u = it.second;
                break;
            }
        }
        deg[u]--;
        if (deg[u] == 1){
            for (pii it : G[u]){
                if (!chk[it.second]){
                    PQ.push(pii(T.first-it.first, u));
                    break;
                }
            }
            continue;
        }
        ans[PQ.size()] = ans[PQ.size()+1] - T.first;
    }
    ans[1] = 1234567890123456ll;
    f(1, 0, d(1, 0));
    scanf("%d", &Q);
    while (Q--){
        scanf("%d", &u);
        printf("%lld\n", ans[u]);
    }
    return 0;
}

Compilation message

designated_cities.cpp: In function 'int main()':
designated_cities.cpp:34:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &N);
     ~~~~~^~~~~~~~~~
designated_cities.cpp:36:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d %lld %lld", &u, &v, &x, &y);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:66:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &Q);
     ~~~~~^~~~~~~~~~
designated_cities.cpp:68:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &u);
         ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5120 KB Output is correct
2 Incorrect 6 ms 5120 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5120 KB Output is correct
2 Incorrect 452 ms 18292 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5120 KB Output is correct
2 Correct 462 ms 18416 KB Output is correct
3 Correct 389 ms 25468 KB Output is correct
4 Correct 408 ms 18408 KB Output is correct
5 Correct 420 ms 18724 KB Output is correct
6 Correct 437 ms 19816 KB Output is correct
7 Correct 311 ms 21560 KB Output is correct
8 Correct 406 ms 23148 KB Output is correct
9 Correct 305 ms 21472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5120 KB Output is correct
2 Incorrect 6 ms 5120 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5120 KB Output is correct
2 Incorrect 452 ms 18292 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5120 KB Output is correct
2 Incorrect 6 ms 5120 KB Output isn't correct
3 Halted 0 ms 0 KB -