답안 #993847

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
993847 2024-06-06T14:17:10 Z prvocislo Designated Cities (JOI19_designated_cities) C++17
100 / 100
298 ms 64596 KB
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
typedef long long ll;
typedef long double ld;
using namespace std;

struct edge
{
    int u, v;
    ll cuv, cvu, c2;
};
vector<vector<edge> > g;
vector<ll> dps, dp, d, v;
void dfs1a(int u, int p = -1)
{
    for (edge e : g[u]) if (e.v != p)
    {
        dfs1a(e.v, u);
        dps[u] += dps[e.v] + e.cvu;
    }
}
void dfs1b(int u, int p = -1, ll nad = 0)
{
    dp[u] = dps[u] + nad;
    for (edge e : g[u]) if (e.v != p)
    {
        dfs1b(e.v, u, nad + dps[u] - dps[e.v] - e.cvu + e.cuv);
    }
}
pair<ll, int> aa = {-1, -1};
pair<ll, int> dfs(int u, int p = -1)
{
    vector<pair<ll, int> > my;
    for (edge e : g[u]) if (e.v != p)
    {
        my.push_back(dfs(e.v, u));
        my.back().first += e.cuv;
    }
    sort(my.begin(), my.end(), greater<pair<ll, int> >());
    for (int i = 1; i < my.size(); i++) v.push_back(my[i].first);
    my.push_back({ 0, u }), my.push_back({ 0, u });
    aa = max(aa, make_pair(dp[u] + my[0].first + my[1].first, my[0].second));
    return my[0];
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    g.assign(n, {}), dps.assign(n, 0), dp.assign(n, 0), d.assign(n, 0), v.clear();
    ll sum = 0;
    for (int i = 0; i < n - 1; i++)
    {
        edge e;
        cin >> e.u >> e.v >> e.cuv >> e.cvu;
        e.u--, e.v--, e.c2 = e.cuv + e.cvu;
        g[e.u].push_back(e);
        swap(e.u, e.v), swap(e.cuv, e.cvu);
        g[e.u].push_back(e);
        sum += e.c2;
    }
    dfs1a(0);
    dfs1b(0);
    vector<ll> ans(n + 1, 0);
    ans[1] = *max_element(dp.begin(), dp.end());
    bool f = false;
    int q;
    cin >> q;
    while (q--)
    {
        int x;
        cin >> x;
        if (x == 1) cout << (sum - ans[x]) << "\n";
        else
        {
            if (!f)
            {
                dfs(0);
                int a = aa.second;

                v.clear();
                v.push_back(dfs(a).first);
                sort(v.begin(), v.end(), greater<ll>());
                ll s = dp[a];
                for (int i = 0; i < v.size(); i++) s += v[i], ans[i + 2 + f] = max(ans[i + 2 + f], s);
                
                for (int i = 1; i <= n; i++) ans[i] = max(ans[i], ans[i - 1]);
                f = true;
            }
            cout << (sum - ans[x]) << "\n";
        }
    }
    return 0;
}

Compilation message

designated_cities.cpp: In function 'std::pair<long long int, int> dfs(int, int)':
designated_cities.cpp:52:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |     for (int i = 1; i < my.size(); i++) v.push_back(my[i].first);
      |                     ~~^~~~~~~~~~~
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:98:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   98 |                 for (int i = 0; i < v.size(); i++) s += v[i], ans[i + 2 + f] = max(ans[i + 2 + f], s);
      |                                 ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 110 ms 28460 KB Output is correct
3 Correct 168 ms 41464 KB Output is correct
4 Correct 111 ms 28500 KB Output is correct
5 Correct 141 ms 28356 KB Output is correct
6 Correct 124 ms 30644 KB Output is correct
7 Correct 124 ms 27320 KB Output is correct
8 Correct 152 ms 41556 KB Output is correct
9 Correct 76 ms 26852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 155 ms 29652 KB Output is correct
3 Correct 273 ms 62992 KB Output is correct
4 Correct 163 ms 34644 KB Output is correct
5 Correct 202 ms 37572 KB Output is correct
6 Correct 250 ms 40672 KB Output is correct
7 Correct 158 ms 44464 KB Output is correct
8 Correct 260 ms 53980 KB Output is correct
9 Correct 149 ms 42332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 2 ms 604 KB Output is correct
14 Correct 2 ms 860 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 2 ms 604 KB Output is correct
17 Correct 2 ms 600 KB Output is correct
18 Correct 2 ms 604 KB Output is correct
19 Correct 2 ms 604 KB Output is correct
20 Correct 2 ms 604 KB Output is correct
21 Correct 2 ms 604 KB Output is correct
22 Correct 2 ms 604 KB Output is correct
23 Correct 2 ms 604 KB Output is correct
24 Correct 1 ms 644 KB Output is correct
25 Correct 2 ms 860 KB Output is correct
26 Correct 2 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 110 ms 28460 KB Output is correct
3 Correct 168 ms 41464 KB Output is correct
4 Correct 111 ms 28500 KB Output is correct
5 Correct 141 ms 28356 KB Output is correct
6 Correct 124 ms 30644 KB Output is correct
7 Correct 124 ms 27320 KB Output is correct
8 Correct 152 ms 41556 KB Output is correct
9 Correct 76 ms 26852 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 155 ms 29652 KB Output is correct
12 Correct 273 ms 62992 KB Output is correct
13 Correct 163 ms 34644 KB Output is correct
14 Correct 202 ms 37572 KB Output is correct
15 Correct 250 ms 40672 KB Output is correct
16 Correct 158 ms 44464 KB Output is correct
17 Correct 260 ms 53980 KB Output is correct
18 Correct 149 ms 42332 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 174 ms 36136 KB Output is correct
21 Correct 163 ms 49808 KB Output is correct
22 Correct 150 ms 34764 KB Output is correct
23 Correct 176 ms 36552 KB Output is correct
24 Correct 201 ms 35280 KB Output is correct
25 Correct 177 ms 36580 KB Output is correct
26 Correct 184 ms 35368 KB Output is correct
27 Correct 171 ms 37064 KB Output is correct
28 Correct 190 ms 40132 KB Output is correct
29 Correct 231 ms 37004 KB Output is correct
30 Correct 179 ms 36548 KB Output is correct
31 Correct 162 ms 40952 KB Output is correct
32 Correct 298 ms 54732 KB Output is correct
33 Correct 184 ms 42672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 110 ms 28460 KB Output is correct
14 Correct 168 ms 41464 KB Output is correct
15 Correct 111 ms 28500 KB Output is correct
16 Correct 141 ms 28356 KB Output is correct
17 Correct 124 ms 30644 KB Output is correct
18 Correct 124 ms 27320 KB Output is correct
19 Correct 152 ms 41556 KB Output is correct
20 Correct 76 ms 26852 KB Output is correct
21 Correct 0 ms 344 KB Output is correct
22 Correct 155 ms 29652 KB Output is correct
23 Correct 273 ms 62992 KB Output is correct
24 Correct 163 ms 34644 KB Output is correct
25 Correct 202 ms 37572 KB Output is correct
26 Correct 250 ms 40672 KB Output is correct
27 Correct 158 ms 44464 KB Output is correct
28 Correct 260 ms 53980 KB Output is correct
29 Correct 149 ms 42332 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 2 ms 604 KB Output is correct
32 Correct 2 ms 860 KB Output is correct
33 Correct 2 ms 604 KB Output is correct
34 Correct 2 ms 604 KB Output is correct
35 Correct 2 ms 600 KB Output is correct
36 Correct 2 ms 604 KB Output is correct
37 Correct 2 ms 604 KB Output is correct
38 Correct 2 ms 604 KB Output is correct
39 Correct 2 ms 604 KB Output is correct
40 Correct 2 ms 604 KB Output is correct
41 Correct 2 ms 604 KB Output is correct
42 Correct 1 ms 644 KB Output is correct
43 Correct 2 ms 860 KB Output is correct
44 Correct 2 ms 604 KB Output is correct
45 Correct 0 ms 348 KB Output is correct
46 Correct 174 ms 36136 KB Output is correct
47 Correct 163 ms 49808 KB Output is correct
48 Correct 150 ms 34764 KB Output is correct
49 Correct 176 ms 36552 KB Output is correct
50 Correct 201 ms 35280 KB Output is correct
51 Correct 177 ms 36580 KB Output is correct
52 Correct 184 ms 35368 KB Output is correct
53 Correct 171 ms 37064 KB Output is correct
54 Correct 190 ms 40132 KB Output is correct
55 Correct 231 ms 37004 KB Output is correct
56 Correct 179 ms 36548 KB Output is correct
57 Correct 162 ms 40952 KB Output is correct
58 Correct 298 ms 54732 KB Output is correct
59 Correct 184 ms 42672 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 196 ms 38368 KB Output is correct
62 Correct 297 ms 64596 KB Output is correct
63 Correct 257 ms 37184 KB Output is correct
64 Correct 211 ms 38852 KB Output is correct
65 Correct 220 ms 37584 KB Output is correct
66 Correct 209 ms 39044 KB Output is correct
67 Correct 241 ms 37540 KB Output is correct
68 Correct 215 ms 39720 KB Output is correct
69 Correct 238 ms 42636 KB Output is correct
70 Correct 218 ms 39220 KB Output is correct
71 Correct 203 ms 39364 KB Output is correct
72 Correct 191 ms 43200 KB Output is correct
73 Correct 277 ms 56520 KB Output is correct
74 Correct 181 ms 48048 KB Output is correct