답안 #571246

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
571246 2022-06-01T15:24:18 Z YouAreMyGalaxy Paths (RMI21_paths) C++17
100 / 100
351 ms 17184 KB
//Make CSP great again
//Vengeance
#include <bits/stdc++.h>
#define TASK "TESTCODE"
#define Log2(x) 31 - __builtin_clz(x)
using namespace std;
const int N = 1e5;
vector<pair<int, int> > adj[N + 1];
int n, k;
long long dp[N + 1], g[N + 1], res[N + 1], val = 0;
multiset<long long> in, out;
void add(long long a)
{
    val += a;
    in.insert(a);
    if (in.size() > k)
    {
        val -= *in.begin();
        out.insert(*in.begin());
        in.erase(in.begin());
    }
}
void del(long long a)
{
    auto it = out.find(a);
    if (it != out.end())
    {
        out.erase(it);
        return ;
    }
    it = in.find(a);
    assert(it != in.end());
    val -= a;
    in.erase(it);
    if (!out.empty())
    {
        val += *out.rbegin();
        in.insert(*out.rbegin());
        it = out.end();
        --it;
        out.erase(it);
    }
}
void DFS_down(int u, int p)
{
    for (auto &x : adj[u])
    {
        if (x.first == p)
        {
            continue;
        }
        DFS_down(x.first, u);
        dp[u] = max(dp[u], dp[x.first] + x.second);
    }
}
void DFS_up(int u, int p)
{
    long long s[2], f[2];
    s[0] = s[1] = -1;
    f[0] = f[1] = 0;
    for (auto &x : adj[u])
    {
        int v = x.first, w = x.second;
        if (v == p)
        {
            continue;
        }
        for (int i = 1; i >= 0; -- i)
        {
            if (dp[v] + w > f[i])
            {
                for (int j = 0; j < i; ++ j)
                {
                    f[j] = f[j + 1];
                    s[j] = s[j + 1];
                }
                f[i] = dp[v] + w;
                s[i] = v;
                break;
            }
        }
    }
    for (auto &x : adj[u])
    {
        int v = x.first, w = x.second;
        if (v == p)
        {
            continue;
        }
        g[v] = g[u];
        for (int i = 1; i >= 0; -- i)
        {
            if (s[i] != v)
            {
                g[v] = max(g[v], f[i]);
                break;
            }
        }
        g[v] += w;
        DFS_up(v, u);
        if (v != s[1])
        {
            add(dp[v] + w);
        }
    }
    //cerr << u << ' ' << dp[u] << ' ' << g[u] << '\n';
}
void DFS(int u, int p)
{
    res[u] = val;
    for (auto &x : adj[u])
    {
        int v = x.first, w = x.second;
        if (v == p)
        {
            continue;
        }
        add(dp[v]);
        del(dp[v] + w);
        add(g[v]);
        del(g[v] - w);
        DFS(v, u);
        del(dp[v]);
        add(dp[v] + w);
        del(g[v]);
        add(g[v] - w);
    }
}
void read()
{
    cin >> n >> k;
    for (int i = 1; i < n; ++ i)
    {
        int u, v, w;
        cin >> u >> v >> w;
        adj[u].push_back({v, w});
        adj[v].push_back({u, w});
    }
}
void solve()
{
    DFS_down(1, 0);
    DFS_up(1, 0);
    add(dp[1]);
    add(0);
    DFS(1, 0);
    for (int u = 1; u <= n; ++ u)
    {
        cout << res[u] << '\n';
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    if (fopen(TASK".INP", "r"))
    {
        freopen(TASK".INP", "r", stdin);
        //freopen(TASK".OUT", "w", stdout);
    }
    int t = 1;
    bool typetest = false;
    if (typetest)
    {
        cin >> t;
    }
    for (int __ = 1; __ <= t; ++ __)
    {
        //cout << "Case " << __ << ": ";
        read();
        solve();
    }
}

Compilation message

Main.cpp: In function 'void add(long long int)':
Main.cpp:16:19: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   16 |     if (in.size() > k)
      |         ~~~~~~~~~~^~~
Main.cpp: In function 'int main()':
Main.cpp:158:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  158 |         freopen(TASK".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2676 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2676 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 3 ms 2644 KB Output is correct
5 Correct 3 ms 2704 KB Output is correct
6 Correct 3 ms 2688 KB Output is correct
7 Correct 3 ms 2672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2676 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 3 ms 2644 KB Output is correct
5 Correct 3 ms 2704 KB Output is correct
6 Correct 3 ms 2688 KB Output is correct
7 Correct 3 ms 2672 KB Output is correct
8 Correct 4 ms 2772 KB Output is correct
9 Correct 4 ms 2832 KB Output is correct
10 Correct 4 ms 2772 KB Output is correct
11 Correct 3 ms 2716 KB Output is correct
12 Correct 3 ms 2772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2676 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 3 ms 2644 KB Output is correct
5 Correct 3 ms 2704 KB Output is correct
6 Correct 3 ms 2688 KB Output is correct
7 Correct 3 ms 2672 KB Output is correct
8 Correct 4 ms 2772 KB Output is correct
9 Correct 4 ms 2832 KB Output is correct
10 Correct 4 ms 2772 KB Output is correct
11 Correct 3 ms 2716 KB Output is correct
12 Correct 3 ms 2772 KB Output is correct
13 Correct 5 ms 2900 KB Output is correct
14 Correct 5 ms 2900 KB Output is correct
15 Correct 4 ms 2900 KB Output is correct
16 Correct 5 ms 2904 KB Output is correct
17 Correct 7 ms 2772 KB Output is correct
18 Correct 5 ms 2864 KB Output is correct
19 Correct 6 ms 2900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 270 ms 14268 KB Output is correct
2 Correct 249 ms 16676 KB Output is correct
3 Correct 161 ms 11984 KB Output is correct
4 Correct 271 ms 14340 KB Output is correct
5 Correct 290 ms 15412 KB Output is correct
6 Correct 267 ms 14460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2676 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 3 ms 2644 KB Output is correct
5 Correct 3 ms 2704 KB Output is correct
6 Correct 3 ms 2688 KB Output is correct
7 Correct 3 ms 2672 KB Output is correct
8 Correct 4 ms 2772 KB Output is correct
9 Correct 4 ms 2832 KB Output is correct
10 Correct 4 ms 2772 KB Output is correct
11 Correct 3 ms 2716 KB Output is correct
12 Correct 3 ms 2772 KB Output is correct
13 Correct 5 ms 2900 KB Output is correct
14 Correct 5 ms 2900 KB Output is correct
15 Correct 4 ms 2900 KB Output is correct
16 Correct 5 ms 2904 KB Output is correct
17 Correct 7 ms 2772 KB Output is correct
18 Correct 5 ms 2864 KB Output is correct
19 Correct 6 ms 2900 KB Output is correct
20 Correct 270 ms 14268 KB Output is correct
21 Correct 249 ms 16676 KB Output is correct
22 Correct 161 ms 11984 KB Output is correct
23 Correct 271 ms 14340 KB Output is correct
24 Correct 290 ms 15412 KB Output is correct
25 Correct 267 ms 14460 KB Output is correct
26 Correct 351 ms 14652 KB Output is correct
27 Correct 327 ms 16528 KB Output is correct
28 Correct 307 ms 17184 KB Output is correct
29 Correct 161 ms 12176 KB Output is correct
30 Correct 306 ms 14704 KB Output is correct
31 Correct 273 ms 13004 KB Output is correct
32 Correct 328 ms 15612 KB Output is correct
33 Correct 308 ms 14656 KB Output is correct
34 Correct 159 ms 11868 KB Output is correct
35 Correct 266 ms 14788 KB Output is correct
36 Correct 240 ms 17180 KB Output is correct