Submission #626575

# Submission time Handle Problem Language Result Execution time Memory
626575 2022-08-11T14:42:22 Z tamthegod Paths (RMI21_paths) C++14
100 / 100
355 ms 40660 KB
#include<bits/stdc++.h>

#define int long long
#define pb push_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 1e6 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e18;
int n, k;
vector<pair<int, int>> adj[maxN];
int f[maxN], dp[maxN];
void ReadInput()
{
    cin >> n >> k;
    for(int i=1; i<n; i++)
    {
        int u, v, w;
        cin >> u >> v >> w;
        adj[u].pb({v, w});
        adj[v].pb({u, w});
    }
}
void predfs(int u, int par)
{
    for(auto a : adj[u])
    {
        int v = a.fi, w = a.se;
        if(v == par) continue;
        predfs(v, u);
        f[u] = max(f[u], f[v] + w);
    }
}
void predfs1(int u, int par)
{
    vector<pair<int, int>> vc;
    for(auto a : adj[u])
    {
        int v = a.fi, w = a.se;
        if(v == par) continue;
        vc.pb({f[v] + w, v});
    }
    sort(vc.begin(), vc.end(), greater<pair<int, int>>());
    for(auto a : adj[u])
    {
        int v = a.fi, w = a.se;
        if(v == par) continue;
        if(v != vc[0].se) dp[v] = max(dp[u] + w, vc[0].fi + w);
        else dp[v] = max(dp[u] + w, vc[1].fi + w);
        predfs1(v, u);
    }
}
multiset<int, greater<int>> s, s1;
void dfs(int u, int par, int root)
{
    int _max = 0, id = 0;
    for(auto a : adj[u])
    {
        int v = a.fi, w = a.se;
        if(v == par) continue;
        dfs(v, u, root);
        if(f[v] + w > _max)
        {
            _max = f[v] + w;
            id = v;
        }
    }
   // cout << u << " " << _max << "\n";
    for(auto a : adj[u])
    {
        int v = a.fi, w = a.se;
        if(v == par || v == id) continue;
        s.insert(f[v] + w);
    }
    if(u == root) s.insert(f[u]);
}
int res[maxN], sum = 0;
void add(int val)
{
    if(s.size() < k)
    {
        s.insert(val);
        sum += val;
        return;
    }
    auto it = s.end();
    it--;
    if(*it < val)
    {
        sum -= *it;
        sum += val;
        s1.insert(*it);
        s.insert(val);
        s.erase(it);
    }
    else s1.insert(val);
}
void del(int val)
{
    if(s.find(val) != s.end())
    {
        sum -= val;
        s.erase(s.find(val));
        if(s1.empty()) return;
        auto it = s1.begin();
        sum += *it;
        s.insert(*it);
        s1.erase(it);
    }
    else if(s1.find(val) != s1.end()) s1.erase(s1.find(val));
}
void reroot(int u, int par)
{
    res[u] = sum;
    for(auto a : adj[u])
    {
        int v = a.fi, w = a.se;
        if(v == par) continue;
        add(f[v]);
        del(f[v] + w);
        add(dp[v]);
        del(dp[v] - w);
        reroot(v, u);
        add(f[v] + w);
        del(f[v]);
        add(dp[v] - w);
        del(dp[v]);
    }
}
void Solve()
{
    int t = rng() % n + 1;
    predfs(t, 0);
    predfs1(t, 0);
    dfs(t, 0, t);
    for(int v : s) sum += v;
    while(s.size() > k)
    {
        auto it = s.end();
        it--;
        sum -= *it;
        s1.insert(*it);
        s.erase(it);
    }
    //for(int v : s1) cout << v << " ";return;
    reroot(t, 0);
    for(int i=1; i<=n; i++) cout << res[i] << '\n';
}
int32_t main()
{
   // freopen("x.inp", "r", stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ReadInput();
    Solve();
}

Compilation message

Main.cpp: In function 'void add(long long int)':
Main.cpp:85:17: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int, std::greater<long long int> >::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
   85 |     if(s.size() < k)
      |        ~~~~~~~~~^~~
Main.cpp: In function 'void Solve()':
Main.cpp:142:20: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int, std::greater<long long int> >::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
  142 |     while(s.size() > k)
      |           ~~~~~~~~~^~~
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23892 KB Output is correct
2 Correct 16 ms 23740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23892 KB Output is correct
2 Correct 16 ms 23740 KB Output is correct
3 Correct 15 ms 23748 KB Output is correct
4 Correct 15 ms 23764 KB Output is correct
5 Correct 16 ms 23776 KB Output is correct
6 Correct 14 ms 23808 KB Output is correct
7 Correct 15 ms 23752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23892 KB Output is correct
2 Correct 16 ms 23740 KB Output is correct
3 Correct 15 ms 23748 KB Output is correct
4 Correct 15 ms 23764 KB Output is correct
5 Correct 16 ms 23776 KB Output is correct
6 Correct 14 ms 23808 KB Output is correct
7 Correct 15 ms 23752 KB Output is correct
8 Correct 14 ms 23944 KB Output is correct
9 Correct 16 ms 23996 KB Output is correct
10 Correct 16 ms 23892 KB Output is correct
11 Correct 18 ms 23892 KB Output is correct
12 Correct 15 ms 23892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23892 KB Output is correct
2 Correct 16 ms 23740 KB Output is correct
3 Correct 15 ms 23748 KB Output is correct
4 Correct 15 ms 23764 KB Output is correct
5 Correct 16 ms 23776 KB Output is correct
6 Correct 14 ms 23808 KB Output is correct
7 Correct 15 ms 23752 KB Output is correct
8 Correct 14 ms 23944 KB Output is correct
9 Correct 16 ms 23996 KB Output is correct
10 Correct 16 ms 23892 KB Output is correct
11 Correct 18 ms 23892 KB Output is correct
12 Correct 15 ms 23892 KB Output is correct
13 Correct 18 ms 24020 KB Output is correct
14 Correct 19 ms 24124 KB Output is correct
15 Correct 17 ms 24024 KB Output is correct
16 Correct 20 ms 24004 KB Output is correct
17 Correct 19 ms 24012 KB Output is correct
18 Correct 16 ms 23912 KB Output is correct
19 Correct 20 ms 24020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 268 ms 34752 KB Output is correct
2 Correct 290 ms 37076 KB Output is correct
3 Correct 185 ms 32732 KB Output is correct
4 Correct 309 ms 34696 KB Output is correct
5 Correct 265 ms 36388 KB Output is correct
6 Correct 282 ms 34788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23892 KB Output is correct
2 Correct 16 ms 23740 KB Output is correct
3 Correct 15 ms 23748 KB Output is correct
4 Correct 15 ms 23764 KB Output is correct
5 Correct 16 ms 23776 KB Output is correct
6 Correct 14 ms 23808 KB Output is correct
7 Correct 15 ms 23752 KB Output is correct
8 Correct 14 ms 23944 KB Output is correct
9 Correct 16 ms 23996 KB Output is correct
10 Correct 16 ms 23892 KB Output is correct
11 Correct 18 ms 23892 KB Output is correct
12 Correct 15 ms 23892 KB Output is correct
13 Correct 18 ms 24020 KB Output is correct
14 Correct 19 ms 24124 KB Output is correct
15 Correct 17 ms 24024 KB Output is correct
16 Correct 20 ms 24004 KB Output is correct
17 Correct 19 ms 24012 KB Output is correct
18 Correct 16 ms 23912 KB Output is correct
19 Correct 20 ms 24020 KB Output is correct
20 Correct 268 ms 34752 KB Output is correct
21 Correct 290 ms 37076 KB Output is correct
22 Correct 185 ms 32732 KB Output is correct
23 Correct 309 ms 34696 KB Output is correct
24 Correct 265 ms 36388 KB Output is correct
25 Correct 282 ms 34788 KB Output is correct
26 Correct 314 ms 35120 KB Output is correct
27 Correct 320 ms 36764 KB Output is correct
28 Correct 294 ms 37816 KB Output is correct
29 Correct 197 ms 35000 KB Output is correct
30 Correct 355 ms 37232 KB Output is correct
31 Correct 323 ms 35756 KB Output is correct
32 Correct 319 ms 38852 KB Output is correct
33 Correct 353 ms 37208 KB Output is correct
34 Correct 171 ms 34644 KB Output is correct
35 Correct 345 ms 37180 KB Output is correct
36 Correct 308 ms 40660 KB Output is correct