Submission #1108777

# Submission time Handle Problem Language Result Execution time Memory
1108777 2024-11-05T03:51:42 Z cpptowin Paths (RMI21_paths) C++17
68 / 100
264 ms 75196 KB
#include <bits/stdc++.h>
#define fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 1000010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define int long long
#define ll long long
#define bitcount(x) __builtin_popcountll(x)
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1LL << j))
#define onbit(i, j) (i | (1LL << j))
#define vi vector<int>
#define all(x) x.begin(), x.end()
#define ss(x) (int)x.size()
#define UNIQUE(v) v.erase(unique(all(v)), v.end())
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}
using namespace std;
const int nsqrt = 450;
const int mod = 1e9 + 7;
void add(int &x, int k)
{
    x += k;
    x %= mod;
    if (x < 0)
        x += mod;
}
void del(int &x, int k)
{
    x -= k;
    x %= mod;
    if (x < 0)
        x += mod;
}
vii ke[maxn];
int n, k;
pair<ll,int> down[maxn], up[maxn];
ll f[maxn];
int par[maxn][20];
int a[maxn];
ll num[maxn];
int h[maxn];
vi leaves;
ll ans, res[maxn];
set<pair<ll,int>> q, q1;
void dfs(int u)
{
    up[u].se = u;
    for (auto [v, w] : ke[u])
        if (v != par[u][0])
        {
            par[v][0] = u;
            h[v] = h[u] + 1;
            f[v] = f[u] + w;
            fo(i, 1, 19) par[v][i] = par[par[v][i - 1]][i - 1];
            dfs(v);
            if (maximize(up[u].fi, up[v].fi + w))
                up[u].se = up[v].se;
        }
}
void dfsdown(int u)
{
    vector<pair<ll,int>> suf(ss(ke[u]) + 2, {0, 0});
    suf[ss(ke[u]) + 1] = down[u];
    pair<ll,int> pre = {-1, -1};
    fod(i, ss(ke[u]), 1)
    {
        auto [v, w] = ke[u][i - 1];
        suf[i] = suf[i + 1];
        if (v == par[u][0])
            continue;
        if (maximize(suf[i].fi, up[v].fi + w))
            suf[i].se = up[v].se;
    }
    fo(i, 1, ss(ke[u]))
    {
        auto [v, w] = ke[u][i - 1];
        if (v == par[u][0])
            continue;
        down[v] = {pre.fi + w, pre.se};
        if (maximize(down[v].fi, suf[i + 1].fi + w))
            down[v].se = suf[i + 1].se;
        if (maximize(pre.fi, up[v].fi + w))
            pre.se = up[v].se;
    }
    for (auto [v, w] : ke[u])
        if (v != par[u][0])
        {
            dfsdown(v);
        }
}
int find(int u)
{
    int cc = u;
    fod(i, 19, 0) if (up[par[u][i]].se == cc)
    {
        u = par[u][i];
    }
    return par[u][0];
}
vector<pair<int,ll>> event;
void to(int x, int y)
{
    int u = down[y].se;
    event.pb(u, num[u]);
    if (q.find({num[u], u}) != q.end())
    {
        q.erase(q.find({num[u], u}));
        num[u] = down[y].fi;
        q.insert({num[u], u});
    }
    else if (q1.find({num[u], u}) != q1.end())
    {
        q1.erase(q1.find({num[u], u}));
        ans -= num[u];
        num[u] = down[y].fi;
        ans += num[u];
        q1.insert({num[u], u});
    }
    u = up[y].se;
    event.pb(u, num[u]);
    if (q.find({num[u], u}) != q.end())
    {
        q.erase(q.find({num[u], u}));
        num[u] = up[y].fi;
        q.insert({num[u], u});
    }
    else if (q1.find({num[u], u}) != q1.end())
    {
        q1.erase(q1.find({num[u], u}));
        ans -= num[u];
        num[u] = up[y].fi;
        ans += num[u];
        q1.insert({num[u], u});
    }
    if (ss(q) and ss(q1) and q1.begin()->fi < q.rbegin()->fi)
    {
        ans -= q1.begin()->fi;
        ans += q.rbegin()->fi;
        pii it = *q1.begin();
        pii it1 = *q.rbegin();
        q1.erase(q1.find(it));
        q.erase(q.find(it1));
        q1.insert(it1);
        q.insert(it);
    }
}
void rollback()
{
    int u = event.back().fi;
    ll du = event.back().se;
    event.pop_back();
    if (q.find({num[u], u}) != q.end())
    {
        q.erase(q.find({num[u], u}));
        num[u] = du;
        q.insert({num[u], u});
    }
    else if (q1.find({num[u], u}) != q1.end())
    {
        q1.erase(q1.find({num[u], u}));
        ans -= num[u];
        num[u] = du;
        ans += num[u];
        q1.insert({num[u], u});
    }
    u = event.back().fi;
    du = event.back().se;
    event.pop_back();
    if (q.find({num[u], u}) != q.end())
    {
        q.erase(q.find({num[u], u}));
        num[u] = du;
        q.insert({num[u], u});
    }
    else if (q1.find({num[u], u}) != q1.end())
    {
        q1.erase(q1.find({num[u], u}));
        ans -= num[u];
        num[u] = du;
        ans += num[u];
        q1.insert({num[u], u});
    }
    if (ss(q) and ss(q1) and q1.begin()->fi < q.rbegin()->fi)
    {
        ans -= q1.begin()->fi;
        ans += q.rbegin()->fi;
        pii it = *q1.begin();
        pii it1 = *q.rbegin();
        q1.erase(q1.find(it));
        q.erase(q.find(it1));
        q1.insert(it1);
        q.insert(it);
    }
}
void dfs1(int u)
{
    res[u] = ans;
    for (auto [v, w] : ke[u])
        if (v != par[u][0])
        {
            to(u, v);
            dfs1(v);
            rollback();
        }
}
main()
{
#define name "TASK"
    if (fopen(name ".inp", "r"))
    {
        freopen(name ".inp", "r", stdin);
        freopen(name ".out", "w", stdout);
    }
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> k;
    fo(i, 1, n - 1)
    {
        int u, v, w;
        cin >> u >> v >> w;
        ke[u].pb(v, w);
        ke[v].pb(u, w);
    }
    dfs(1);
    down[1] = {0,1};
    dfsdown(1);
    // fo(i,1,n) cout << up[i].fi << ' ' << up[i].se << ' ' << down[i].fi << ' ' << down[i].se << "\n";
    fo(i, 1, n) if (ss(ke[i]) == 1) leaves.pb(i);
    for (int it : leaves)
    {
        a[it] = find(it);
        if(!a[it]) a[it] = 1;
        num[it] = f[it] - f[a[it]];
        q.insert({num[it], it});
    }
    down[1] = {0, 1};
    int cc = k;
    while (ss(q) and cc > 0)
    {
        ans += q.rbegin()->fi;
        q1.insert(*q.rbegin());
        q.erase(q.find(*q.rbegin()));
        cc--;
    }
    dfs1(1);
    fo(i, 1, n) cout << res[i] << "\n";
}

Compilation message

Main.cpp:232:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  232 | main()
      | ^~~~
Main.cpp: In function 'int main()':
Main.cpp:237:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  237 |         freopen(name ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:238:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  238 |         freopen(name ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 35152 KB Output is correct
2 Correct 8 ms 35152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 35152 KB Output is correct
2 Correct 8 ms 35152 KB Output is correct
3 Correct 9 ms 35164 KB Output is correct
4 Correct 8 ms 35408 KB Output is correct
5 Correct 8 ms 35320 KB Output is correct
6 Correct 9 ms 35152 KB Output is correct
7 Correct 8 ms 35152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 35152 KB Output is correct
2 Correct 8 ms 35152 KB Output is correct
3 Correct 9 ms 35164 KB Output is correct
4 Correct 8 ms 35408 KB Output is correct
5 Correct 8 ms 35320 KB Output is correct
6 Correct 9 ms 35152 KB Output is correct
7 Correct 8 ms 35152 KB Output is correct
8 Correct 9 ms 35548 KB Output is correct
9 Correct 10 ms 35664 KB Output is correct
10 Correct 10 ms 35580 KB Output is correct
11 Correct 10 ms 35628 KB Output is correct
12 Correct 9 ms 35800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 35152 KB Output is correct
2 Correct 8 ms 35152 KB Output is correct
3 Correct 9 ms 35164 KB Output is correct
4 Correct 8 ms 35408 KB Output is correct
5 Correct 8 ms 35320 KB Output is correct
6 Correct 9 ms 35152 KB Output is correct
7 Correct 8 ms 35152 KB Output is correct
8 Correct 9 ms 35548 KB Output is correct
9 Correct 10 ms 35664 KB Output is correct
10 Correct 10 ms 35580 KB Output is correct
11 Correct 10 ms 35628 KB Output is correct
12 Correct 9 ms 35800 KB Output is correct
13 Correct 11 ms 35920 KB Output is correct
14 Correct 11 ms 35920 KB Output is correct
15 Correct 10 ms 35920 KB Output is correct
16 Correct 12 ms 35788 KB Output is correct
17 Correct 13 ms 35664 KB Output is correct
18 Correct 14 ms 35664 KB Output is correct
19 Correct 12 ms 35920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 232 ms 70284 KB Output is correct
2 Correct 258 ms 72636 KB Output is correct
3 Correct 148 ms 67144 KB Output is correct
4 Correct 241 ms 70360 KB Output is correct
5 Correct 250 ms 71616 KB Output is correct
6 Correct 247 ms 70360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 35152 KB Output is correct
2 Correct 8 ms 35152 KB Output is correct
3 Correct 9 ms 35164 KB Output is correct
4 Correct 8 ms 35408 KB Output is correct
5 Correct 8 ms 35320 KB Output is correct
6 Correct 9 ms 35152 KB Output is correct
7 Correct 8 ms 35152 KB Output is correct
8 Correct 9 ms 35548 KB Output is correct
9 Correct 10 ms 35664 KB Output is correct
10 Correct 10 ms 35580 KB Output is correct
11 Correct 10 ms 35628 KB Output is correct
12 Correct 9 ms 35800 KB Output is correct
13 Correct 11 ms 35920 KB Output is correct
14 Correct 11 ms 35920 KB Output is correct
15 Correct 10 ms 35920 KB Output is correct
16 Correct 12 ms 35788 KB Output is correct
17 Correct 13 ms 35664 KB Output is correct
18 Correct 14 ms 35664 KB Output is correct
19 Correct 12 ms 35920 KB Output is correct
20 Correct 232 ms 70284 KB Output is correct
21 Correct 258 ms 72636 KB Output is correct
22 Correct 148 ms 67144 KB Output is correct
23 Correct 241 ms 70360 KB Output is correct
24 Correct 250 ms 71616 KB Output is correct
25 Correct 247 ms 70360 KB Output is correct
26 Correct 237 ms 72908 KB Output is correct
27 Correct 264 ms 74524 KB Output is correct
28 Correct 243 ms 75196 KB Output is correct
29 Correct 150 ms 69448 KB Output is correct
30 Incorrect 230 ms 72712 KB Output isn't correct
31 Halted 0 ms 0 KB -