Submission #102513

# Submission time Handle Problem Language Result Execution time Memory
102513 2019-03-25T13:10:31 Z alexpetrescu Designated Cities (JOI19_designated_cities) C++14
100 / 100
656 ms 35320 KB
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <cassert>

//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]] - 2 * h[x] + calc[x] > h[A] + h[B] - 2 * 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[perm[poz]])
            poz++;
        ans[i] = ans[i - 1] - cost[perm[poz]];
        int x = cine[perm[poz]];
        while (luat[x] == 0) {
            luat[x] = 1;
            x = t[x];
        }
    }
}

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:124: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:128: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:141: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:145: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 11 ms 9696 KB Output is correct
2 Correct 11 ms 9856 KB Output is correct
3 Correct 11 ms 9728 KB Output is correct
4 Correct 10 ms 9728 KB Output is correct
5 Correct 11 ms 9728 KB Output is correct
6 Correct 11 ms 9856 KB Output is correct
7 Correct 10 ms 9856 KB Output is correct
8 Correct 10 ms 9728 KB Output is correct
9 Correct 10 ms 9856 KB Output is correct
10 Correct 12 ms 9828 KB Output is correct
11 Correct 12 ms 9984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 483 ms 29512 KB Output is correct
3 Correct 542 ms 31840 KB Output is correct
4 Correct 482 ms 29416 KB Output is correct
5 Correct 462 ms 29616 KB Output is correct
6 Correct 505 ms 30584 KB Output is correct
7 Correct 488 ms 29864 KB Output is correct
8 Correct 594 ms 33272 KB Output is correct
9 Correct 334 ms 29016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 9728 KB Output is correct
2 Correct 523 ms 29576 KB Output is correct
3 Correct 594 ms 31432 KB Output is correct
4 Correct 528 ms 29612 KB Output is correct
5 Correct 525 ms 29612 KB Output is correct
6 Correct 522 ms 30560 KB Output is correct
7 Correct 325 ms 30880 KB Output is correct
8 Correct 516 ms 33992 KB Output is correct
9 Correct 347 ms 29108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 9696 KB Output is correct
2 Correct 11 ms 9856 KB Output is correct
3 Correct 11 ms 9728 KB Output is correct
4 Correct 10 ms 9728 KB Output is correct
5 Correct 11 ms 9728 KB Output is correct
6 Correct 11 ms 9856 KB Output is correct
7 Correct 10 ms 9856 KB Output is correct
8 Correct 10 ms 9728 KB Output is correct
9 Correct 10 ms 9856 KB Output is correct
10 Correct 12 ms 9828 KB Output is correct
11 Correct 12 ms 9984 KB Output is correct
12 Correct 12 ms 9728 KB Output is correct
13 Correct 14 ms 10112 KB Output is correct
14 Correct 13 ms 9984 KB Output is correct
15 Correct 14 ms 9984 KB Output is correct
16 Correct 15 ms 9984 KB Output is correct
17 Correct 14 ms 9984 KB Output is correct
18 Correct 14 ms 9984 KB Output is correct
19 Correct 13 ms 9984 KB Output is correct
20 Correct 13 ms 9984 KB Output is correct
21 Correct 12 ms 9984 KB Output is correct
22 Correct 13 ms 9984 KB Output is correct
23 Correct 13 ms 9984 KB Output is correct
24 Correct 12 ms 9984 KB Output is correct
25 Correct 13 ms 9984 KB Output is correct
26 Correct 11 ms 9984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 483 ms 29512 KB Output is correct
3 Correct 542 ms 31840 KB Output is correct
4 Correct 482 ms 29416 KB Output is correct
5 Correct 462 ms 29616 KB Output is correct
6 Correct 505 ms 30584 KB Output is correct
7 Correct 488 ms 29864 KB Output is correct
8 Correct 594 ms 33272 KB Output is correct
9 Correct 334 ms 29016 KB Output is correct
10 Correct 12 ms 9728 KB Output is correct
11 Correct 523 ms 29576 KB Output is correct
12 Correct 594 ms 31432 KB Output is correct
13 Correct 528 ms 29612 KB Output is correct
14 Correct 525 ms 29612 KB Output is correct
15 Correct 522 ms 30560 KB Output is correct
16 Correct 325 ms 30880 KB Output is correct
17 Correct 516 ms 33992 KB Output is correct
18 Correct 347 ms 29108 KB Output is correct
19 Correct 10 ms 9728 KB Output is correct
20 Correct 499 ms 29528 KB Output is correct
21 Correct 524 ms 31992 KB Output is correct
22 Correct 502 ms 29564 KB Output is correct
23 Correct 525 ms 29612 KB Output is correct
24 Correct 462 ms 29560 KB Output is correct
25 Correct 513 ms 29560 KB Output is correct
26 Correct 477 ms 29688 KB Output is correct
27 Correct 477 ms 29720 KB Output is correct
28 Correct 508 ms 30612 KB Output is correct
29 Correct 537 ms 29264 KB Output is correct
30 Correct 510 ms 29968 KB Output is correct
31 Correct 409 ms 30120 KB Output is correct
32 Correct 570 ms 33520 KB Output is correct
33 Correct 349 ms 28960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 9696 KB Output is correct
2 Correct 11 ms 9856 KB Output is correct
3 Correct 11 ms 9728 KB Output is correct
4 Correct 10 ms 9728 KB Output is correct
5 Correct 11 ms 9728 KB Output is correct
6 Correct 11 ms 9856 KB Output is correct
7 Correct 10 ms 9856 KB Output is correct
8 Correct 10 ms 9728 KB Output is correct
9 Correct 10 ms 9856 KB Output is correct
10 Correct 12 ms 9828 KB Output is correct
11 Correct 12 ms 9984 KB Output is correct
12 Correct 10 ms 9728 KB Output is correct
13 Correct 483 ms 29512 KB Output is correct
14 Correct 542 ms 31840 KB Output is correct
15 Correct 482 ms 29416 KB Output is correct
16 Correct 462 ms 29616 KB Output is correct
17 Correct 505 ms 30584 KB Output is correct
18 Correct 488 ms 29864 KB Output is correct
19 Correct 594 ms 33272 KB Output is correct
20 Correct 334 ms 29016 KB Output is correct
21 Correct 12 ms 9728 KB Output is correct
22 Correct 523 ms 29576 KB Output is correct
23 Correct 594 ms 31432 KB Output is correct
24 Correct 528 ms 29612 KB Output is correct
25 Correct 525 ms 29612 KB Output is correct
26 Correct 522 ms 30560 KB Output is correct
27 Correct 325 ms 30880 KB Output is correct
28 Correct 516 ms 33992 KB Output is correct
29 Correct 347 ms 29108 KB Output is correct
30 Correct 12 ms 9728 KB Output is correct
31 Correct 14 ms 10112 KB Output is correct
32 Correct 13 ms 9984 KB Output is correct
33 Correct 14 ms 9984 KB Output is correct
34 Correct 15 ms 9984 KB Output is correct
35 Correct 14 ms 9984 KB Output is correct
36 Correct 14 ms 9984 KB Output is correct
37 Correct 13 ms 9984 KB Output is correct
38 Correct 13 ms 9984 KB Output is correct
39 Correct 12 ms 9984 KB Output is correct
40 Correct 13 ms 9984 KB Output is correct
41 Correct 13 ms 9984 KB Output is correct
42 Correct 12 ms 9984 KB Output is correct
43 Correct 13 ms 9984 KB Output is correct
44 Correct 11 ms 9984 KB Output is correct
45 Correct 10 ms 9728 KB Output is correct
46 Correct 499 ms 29528 KB Output is correct
47 Correct 524 ms 31992 KB Output is correct
48 Correct 502 ms 29564 KB Output is correct
49 Correct 525 ms 29612 KB Output is correct
50 Correct 462 ms 29560 KB Output is correct
51 Correct 513 ms 29560 KB Output is correct
52 Correct 477 ms 29688 KB Output is correct
53 Correct 477 ms 29720 KB Output is correct
54 Correct 508 ms 30612 KB Output is correct
55 Correct 537 ms 29264 KB Output is correct
56 Correct 510 ms 29968 KB Output is correct
57 Correct 409 ms 30120 KB Output is correct
58 Correct 570 ms 33520 KB Output is correct
59 Correct 349 ms 28960 KB Output is correct
60 Correct 12 ms 9780 KB Output is correct
61 Correct 591 ms 30840 KB Output is correct
62 Correct 614 ms 32872 KB Output is correct
63 Correct 568 ms 30888 KB Output is correct
64 Correct 541 ms 31132 KB Output is correct
65 Correct 585 ms 30812 KB Output is correct
66 Correct 586 ms 30940 KB Output is correct
67 Correct 536 ms 30716 KB Output is correct
68 Correct 586 ms 31068 KB Output is correct
69 Correct 593 ms 31728 KB Output is correct
70 Correct 609 ms 30260 KB Output is correct
71 Correct 586 ms 31404 KB Output is correct
72 Correct 530 ms 31996 KB Output is correct
73 Correct 656 ms 35320 KB Output is correct
74 Correct 500 ms 31908 KB Output is correct