답안 #993825

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
993825 2024-06-06T13:18:07 Z prvocislo Designated Cities (JOI19_designated_cities) C++17
7 / 100
179 ms 41552 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);
    }
}
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]);
    if (my.empty()) my.push_back(0);
    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)
            {
                int a = max_element(dp.begin(), dp.end()) - dp.begin();

                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:50:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |     for (int i = 1; i < my.size(); i++) v.push_back(my[i]);
      |                     ~~^~~~~~~~~~~
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:95:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |                 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 348 KB Output is correct
2 Correct 0 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 Incorrect 0 ms 348 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 125 ms 28584 KB Output is correct
3 Correct 159 ms 41248 KB Output is correct
4 Correct 108 ms 28500 KB Output is correct
5 Correct 111 ms 28356 KB Output is correct
6 Correct 124 ms 30548 KB Output is correct
7 Correct 102 ms 27484 KB Output is correct
8 Correct 179 ms 41552 KB Output is correct
9 Correct 78 ms 26952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 148 ms 30032 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 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 Incorrect 0 ms 348 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 125 ms 28584 KB Output is correct
3 Correct 159 ms 41248 KB Output is correct
4 Correct 108 ms 28500 KB Output is correct
5 Correct 111 ms 28356 KB Output is correct
6 Correct 124 ms 30548 KB Output is correct
7 Correct 102 ms 27484 KB Output is correct
8 Correct 179 ms 41552 KB Output is correct
9 Correct 78 ms 26952 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Incorrect 148 ms 30032 KB Output isn't correct
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 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 Incorrect 0 ms 348 KB Output isn't correct
6 Halted 0 ms 0 KB -