Submission #569055

# Submission time Handle Problem Language Result Execution time Memory
569055 2022-05-26T14:29:39 Z Tien_Noob Paths (RMI21_paths) C++17
100 / 100
319 ms 17300 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);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 3 ms 2680 KB Output is correct
4 Correct 4 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2676 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 3 ms 2680 KB Output is correct
4 Correct 4 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2676 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 5 ms 2720 KB Output is correct
9 Correct 3 ms 2820 KB Output is correct
10 Correct 3 ms 2772 KB Output is correct
11 Correct 3 ms 2692 KB Output is correct
12 Correct 4 ms 2788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 3 ms 2680 KB Output is correct
4 Correct 4 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2676 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 5 ms 2720 KB Output is correct
9 Correct 3 ms 2820 KB Output is correct
10 Correct 3 ms 2772 KB Output is correct
11 Correct 3 ms 2692 KB Output is correct
12 Correct 4 ms 2788 KB Output is correct
13 Correct 5 ms 2820 KB Output is correct
14 Correct 4 ms 3000 KB Output is correct
15 Correct 4 ms 2900 KB Output is correct
16 Correct 5 ms 2820 KB Output is correct
17 Correct 4 ms 2824 KB Output is correct
18 Correct 4 ms 2820 KB Output is correct
19 Correct 5 ms 2900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 233 ms 14372 KB Output is correct
2 Correct 217 ms 16816 KB Output is correct
3 Correct 160 ms 12028 KB Output is correct
4 Correct 241 ms 14308 KB Output is correct
5 Correct 246 ms 15420 KB Output is correct
6 Correct 253 ms 14452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 3 ms 2680 KB Output is correct
4 Correct 4 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2676 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 5 ms 2720 KB Output is correct
9 Correct 3 ms 2820 KB Output is correct
10 Correct 3 ms 2772 KB Output is correct
11 Correct 3 ms 2692 KB Output is correct
12 Correct 4 ms 2788 KB Output is correct
13 Correct 5 ms 2820 KB Output is correct
14 Correct 4 ms 3000 KB Output is correct
15 Correct 4 ms 2900 KB Output is correct
16 Correct 5 ms 2820 KB Output is correct
17 Correct 4 ms 2824 KB Output is correct
18 Correct 4 ms 2820 KB Output is correct
19 Correct 5 ms 2900 KB Output is correct
20 Correct 233 ms 14372 KB Output is correct
21 Correct 217 ms 16816 KB Output is correct
22 Correct 160 ms 12028 KB Output is correct
23 Correct 241 ms 14308 KB Output is correct
24 Correct 246 ms 15420 KB Output is correct
25 Correct 253 ms 14452 KB Output is correct
26 Correct 319 ms 14664 KB Output is correct
27 Correct 252 ms 16588 KB Output is correct
28 Correct 262 ms 17100 KB Output is correct
29 Correct 148 ms 12348 KB Output is correct
30 Correct 308 ms 14708 KB Output is correct
31 Correct 234 ms 13128 KB Output is correct
32 Correct 268 ms 15608 KB Output is correct
33 Correct 300 ms 14672 KB Output is correct
34 Correct 143 ms 11852 KB Output is correct
35 Correct 257 ms 14716 KB Output is correct
36 Correct 237 ms 17300 KB Output is correct