답안 #993837

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
993837 2024-06-06T13:44:06 Z prvocislo Designated Cities (JOI19_designated_cities) C++17
7 / 100
173 ms 41500 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};
ll dfs(int u, int p = -1)
{
    vector<ll> my;
    for (edge e : g[u]) if (e.v != p)
    {
        my.push_back(dfs(e.v, u) + e.cuv);
    }
    sort(my.begin(), my.end(), greater<ll>());
    for (int i = 1; i < my.size(); i++) v.push_back(my[i]);
    my.push_back(0), my.push_back(0);
    aa = max(aa, make_pair(dp[u] + my[0] + my[1], u));
    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));
                sort(v.begin(), v.end(), greater<ll>());
                ll s = dp[a];
                int f = (g[a].size() == 1 ? 1 : 0);
                for (int i = 0; i < v.size(); i++) s += v[i], ans[i + 1 + f] = max(ans[i + 1 + 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 'll dfs(int, int)':
designated_cities.cpp:51:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |     for (int i = 1; i < my.size(); i++) v.push_back(my[i]);
      |                     ~~^~~~~~~~~~~
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 + 1 + f] = max(ans[i + 1 + 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 348 KB Output is correct
4 Incorrect 0 ms 348 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 110 ms 28552 KB Output is correct
3 Correct 158 ms 41120 KB Output is correct
4 Correct 116 ms 28500 KB Output is correct
5 Correct 139 ms 28352 KB Output is correct
6 Correct 121 ms 30708 KB Output is correct
7 Correct 112 ms 27324 KB Output is correct
8 Correct 154 ms 41500 KB Output is correct
9 Correct 81 ms 27888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Incorrect 173 ms 29828 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Incorrect 0 ms 348 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 110 ms 28552 KB Output is correct
3 Correct 158 ms 41120 KB Output is correct
4 Correct 116 ms 28500 KB Output is correct
5 Correct 139 ms 28352 KB Output is correct
6 Correct 121 ms 30708 KB Output is correct
7 Correct 112 ms 27324 KB Output is correct
8 Correct 154 ms 41500 KB Output is correct
9 Correct 81 ms 27888 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Incorrect 173 ms 29828 KB Output isn't correct
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Incorrect 0 ms 348 KB Output isn't correct
5 Halted 0 ms 0 KB -