Submission #102509

# Submission time Handle Problem Language Result Execution time Memory
102509 2019-03-25T12:45:44 Z alexpetrescu Designated Cities (JOI19_designated_cities) C++14
33 / 100
590 ms 34040 KB
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cassert>
#include <cstdlib>

//FILE *fin = fopen("a.in", "r"), *fout = fopen("a.out", "w");
#define fin stdin
#define fout stdout

#define ll long long

#define MAXN 200009

struct myc {
    int x, y, z;
};
std::vector < int > candidat[MAXN];
std::vector < myc > g[MAXN];
ll ans[MAXN], sumaTuturor;
int e[MAXN], desteptu[MAXN], istetu[MAXN];
bool viz[MAXN], luat[MAXN], seen[MAXN];
ll sum[MAXN], calc[MAXN], h[MAXN], cost[MAXN];
int t[MAXN], cine[MAXN], perm[MAXN];
int A, B, C;

void dfs1(int x) {
    viz[x] = 1;
    for (auto &y : g[x]) {
        if (viz[y.x] == 0) {
            dfs1(y.x);
            sum[x] += sum[y.x] + y.z;
        }
    }
}

void dfs2(int x, ll dinSpate) {
    viz[x] = 1;
    calc[x] = sum[x] + dinSpate;
    ans[1] = std::min(ans[1], sumaTuturor - calc[x]);
    desteptu[x] = x;
    for (auto &y : g[x]) {
        if (viz[y.x] == 0) {
            h[y.x] = h[x] + y.y;
            dfs2(y.x, dinSpate + sum[x] - sum[y.x] - y.z + y.y);
            if (h[desteptu[y.x]] > h[desteptu[x]]) {
                istetu[x] = desteptu[x];
                desteptu[x] = desteptu[y.x];
            } else if (h[desteptu[y.x]] > h[istetu[x]])
                istetu[x] = desteptu[y.x];
        }
    }
    if (h[desteptu[x]] + h[istetu[x]] - h[x] + calc[x] > h[A] + h[B] - h[C] + calc[C])
        A = desteptu[x], B = istetu[x], C = x;
}

void dfs3(int x) {
    viz[x] = 1;
    bool frunza = 1;
    desteptu[x] = x;
    for (auto &y : g[x]) {
        if (viz[y.x] == 0) {
            frunza = 0;
            h[y.x] = h[x] + y.y;
            t[y.x] = x;
            dfs3(y.x);
            if (h[desteptu[y.x]] > h[desteptu[x]])
                desteptu[x] = desteptu[y.x];
            luat[x] |= luat[y.x];
            if (luat[y.x] == 0)
                ans[2] += y.y;
        }
    }
    if(frunza)
        e[++e[0]] = x;
}

bool cmp(const int &a, const int &b) {
    return cost[a] > cost[b];
}

inline void solve(int n) {
    int rad = 0;
    for (int i = 1; i <= n; i++)
        if ((int)g[i].size() != 1)
            rad = i;
    ans[1] = sumaTuturor;
    dfs1(rad);
    for (int i = 1; i <= n; i++)
        viz[i] = 0;
    h[0] = cost[0] = -1000000000000000000LL;
    dfs2(rad, 0);
    for (int i = 1; i <= n; i++)
        viz[i] = 0;
    luat[A] = luat[B] = 1;
    h[C] = 0;
    dfs3(C);

    for (int i = 1; i <= n; i++)
        for (auto &x : g[i])
            if (t[x.x] == i)
                cost[x.x] = h[desteptu[x.x]] - h[i], cine[x.x] = desteptu[x.x];

    for (int i = 1; i <= n; i++)
        perm[i] = i;
    std::sort(perm + 1, perm + n + 1, cmp);

    int poz = 1;
    for (int i = 3; i <= e[0]; i++) {
        while (luat[cine[perm[poz]]])
            poz++;
        ans[i] = ans[i - 1] - cost[perm[poz]];
        luat[cine[perm[poz]]] = 1;
    }
}

int main() {
    int n;
    fscanf(fin, "%d", &n);

    for (int i = 1; i < n; i++) {
        int x, y, z, t;
        fscanf(fin, "%d%d%d%d", &x, &y, &z, &t);

        g[x].push_back({y, z, t});
        g[y].push_back({x, t, z});

        sumaTuturor += z + t;
        ans[1] = std::min(z, t);
    }

    if (n > 2)
        solve(n);

    int q;
    fscanf(fin, "%d", &q);

    for (; q; q--) {
        int x;
        fscanf(fin, "%d", &x);

        fprintf(fout, "%lld\n", ans[x]);
    }

    fclose(fin);
    fclose(fout);
    return 0;
}

Compilation message

designated_cities.cpp: In function 'int main()':
designated_cities.cpp:119:11: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     fscanf(fin, "%d", &n);
     ~~~~~~^~~~~~~~~~~~~~~
designated_cities.cpp:123:15: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         fscanf(fin, "%d%d%d%d", &x, &y, &z, &t);
         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:136:11: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     fscanf(fin, "%d", &q);
     ~~~~~~^~~~~~~~~~~~~~~
designated_cities.cpp:140:15: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         fscanf(fin, "%d", &x);
         ~~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 11 ms 9856 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 11 ms 9856 KB Output is correct
5 Correct 10 ms 9728 KB Output is correct
6 Correct 13 ms 9728 KB Output is correct
7 Incorrect 9 ms 9856 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 11 ms 9856 KB Output is correct
2 Correct 522 ms 29560 KB Output is correct
3 Correct 549 ms 31900 KB Output is correct
4 Correct 478 ms 29520 KB Output is correct
5 Correct 446 ms 29532 KB Output is correct
6 Correct 530 ms 30584 KB Output is correct
7 Correct 425 ms 29864 KB Output is correct
8 Correct 540 ms 33456 KB Output is correct
9 Correct 309 ms 28840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 458 ms 29704 KB Output is correct
3 Correct 524 ms 31608 KB Output is correct
4 Correct 464 ms 29552 KB Output is correct
5 Correct 461 ms 29544 KB Output is correct
6 Correct 503 ms 30400 KB Output is correct
7 Correct 350 ms 30916 KB Output is correct
8 Correct 537 ms 34040 KB Output is correct
9 Correct 312 ms 29096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 11 ms 9856 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 11 ms 9856 KB Output is correct
5 Correct 10 ms 9728 KB Output is correct
6 Correct 13 ms 9728 KB Output is correct
7 Incorrect 9 ms 9856 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 11 ms 9856 KB Output is correct
2 Correct 522 ms 29560 KB Output is correct
3 Correct 549 ms 31900 KB Output is correct
4 Correct 478 ms 29520 KB Output is correct
5 Correct 446 ms 29532 KB Output is correct
6 Correct 530 ms 30584 KB Output is correct
7 Correct 425 ms 29864 KB Output is correct
8 Correct 540 ms 33456 KB Output is correct
9 Correct 309 ms 28840 KB Output is correct
10 Correct 10 ms 9728 KB Output is correct
11 Correct 458 ms 29704 KB Output is correct
12 Correct 524 ms 31608 KB Output is correct
13 Correct 464 ms 29552 KB Output is correct
14 Correct 461 ms 29544 KB Output is correct
15 Correct 503 ms 30400 KB Output is correct
16 Correct 350 ms 30916 KB Output is correct
17 Correct 537 ms 34040 KB Output is correct
18 Correct 312 ms 29096 KB Output is correct
19 Correct 12 ms 9776 KB Output is correct
20 Correct 553 ms 29580 KB Output is correct
21 Correct 590 ms 31864 KB Output is correct
22 Correct 486 ms 29532 KB Output is correct
23 Correct 533 ms 29632 KB Output is correct
24 Correct 544 ms 29616 KB Output is correct
25 Correct 531 ms 29432 KB Output is correct
26 Correct 522 ms 29516 KB Output is correct
27 Correct 515 ms 29516 KB Output is correct
28 Correct 522 ms 30712 KB Output is correct
29 Correct 534 ms 29248 KB Output is correct
30 Correct 479 ms 29824 KB Output is correct
31 Correct 419 ms 30220 KB Output is correct
32 Correct 552 ms 33528 KB Output is correct
33 Correct 367 ms 28948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 11 ms 9856 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 11 ms 9856 KB Output is correct
5 Correct 10 ms 9728 KB Output is correct
6 Correct 13 ms 9728 KB Output is correct
7 Incorrect 9 ms 9856 KB Output isn't correct
8 Halted 0 ms 0 KB -