Submission #171268

# Submission time Handle Problem Language Result Execution time Memory
171268 2019-12-28T06:27:30 Z gs18103 Designated Cities (JOI19_designated_cities) C++14
9 / 100
508 ms 40392 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define eb emplace_back
#define em emplace
#define all(v) v.begin(), v.end()

using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

const int MAX = 101010;
const int INF = 1e9;
const ll LINF = 1e18;

vector <pair <int, pll> > g[2*MAX];
ll d[2*MAX], sum, ans[2*MAX], up[2*MAX];
bool chk[2*MAX];
int deg[2*MAX];

void dfs(int x, int pa) {
    for(auto i : g[x]) {
        if(i.fi == pa) {
            up[x] = up[pa] + i.se.fi;
            continue;
        }
        d[i.fi] = d[x] + i.se.fi;
        sum += i.se.fi;
        dfs(i.fi, x);
    }
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0);

    int n;
    cin >> n;
    for(int i = 1; i < n; i++) {
        int u, v; ll c, d;
        cin >> u >> v >> c >> d;
        g[u].eb(v, make_pair(c, d)), g[v].eb(u, make_pair(d, c));
        deg[u]++, deg[v]++;
    }
    priority_queue <pll, vector <pll>, greater <pll> > pq;
    for(int i = 1; i <= n; i++) {
        if(deg[i] == 1) pq.em(g[i][0].se.se, i);
    }
    while(pq.size() > 2) {
        pll cur = pq.top();
        pq.pop();
        chk[cur.se] = true;
        int u;
        for(auto i : g[cur.se]) {
            if(!chk[i.fi]) {
                u = i.fi;
                break;
            }
        }
        deg[u]--;
        if(deg[u] == 1) {
            for(auto i : g[u]) {
                if(!chk[i.fi]) {
                    pq.em(cur.fi + i.se.se, u);
                    break;
                }
            }
            continue;
        }
        ans[pq.size()] = ans[pq.size()+1] + cur.fi;
    }
    dfs(1, 0);
    ans[1] = LINF;
    for(int i = 1; i <= n; i++) {
        ans[1] = min(ans[1], sum - d[i] + up[i]);
    }
    
    int q;
    cin >> q;
    while(q--) {
        int u; cin >> u;
        cout << ans[u] << '\n';
    }
}

Compilation message

designated_cities.cpp: In function 'int main()':
designated_cities.cpp:53:13: warning: 'u' may be used uninitialized in this function [-Wmaybe-uninitialized]
         int u;
             ^
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5084 KB Output is correct
2 Incorrect 6 ms 5112 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5084 KB Output is correct
2 Incorrect 445 ms 24420 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 508 ms 24568 KB Output is correct
3 Correct 365 ms 40392 KB Output is correct
4 Correct 459 ms 29420 KB Output is correct
5 Correct 442 ms 31732 KB Output is correct
6 Correct 436 ms 32560 KB Output is correct
7 Correct 346 ms 35100 KB Output is correct
8 Correct 369 ms 37116 KB Output is correct
9 Correct 335 ms 35104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5084 KB Output is correct
2 Incorrect 6 ms 5112 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5084 KB Output is correct
2 Incorrect 445 ms 24420 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5084 KB Output is correct
2 Incorrect 6 ms 5112 KB Output isn't correct
3 Halted 0 ms 0 KB -