Submission #1108779

# Submission time Handle Problem Language Result Execution time Memory
1108779 2024-11-05T03:55:51 Z cpptowin Paths (RMI21_paths) C++17
68 / 100
223 ms 38076 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 100010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define int 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<int,int> down[maxn], up[maxn];
int f[maxn];
int par[maxn][20];
int a[maxn];
int num[maxn];
int h[maxn];
vi leaves;
int ans, res[maxn];
set<pair<int,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<int,int>> suf(ss(ke[u]) + 2, {0, 0});
    suf[ss(ke[u]) + 1] = down[u];
    pair<int,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,int>> 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;
    int 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) 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:231:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  231 | main()
      | ^~~~
Main.cpp: In function 'int main()':
Main.cpp:236:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  236 |         freopen(name ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
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 ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8784 KB Output is correct
2 Correct 2 ms 8784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8784 KB Output is correct
2 Correct 2 ms 8784 KB Output is correct
3 Correct 2 ms 8784 KB Output is correct
4 Correct 2 ms 8784 KB Output is correct
5 Correct 2 ms 8784 KB Output is correct
6 Correct 2 ms 8784 KB Output is correct
7 Correct 2 ms 8784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8784 KB Output is correct
2 Correct 2 ms 8784 KB Output is correct
3 Correct 2 ms 8784 KB Output is correct
4 Correct 2 ms 8784 KB Output is correct
5 Correct 2 ms 8784 KB Output is correct
6 Correct 2 ms 8784 KB Output is correct
7 Correct 2 ms 8784 KB Output is correct
8 Correct 3 ms 9212 KB Output is correct
9 Correct 3 ms 9040 KB Output is correct
10 Correct 2 ms 9040 KB Output is correct
11 Correct 3 ms 9040 KB Output is correct
12 Correct 3 ms 9040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8784 KB Output is correct
2 Correct 2 ms 8784 KB Output is correct
3 Correct 2 ms 8784 KB Output is correct
4 Correct 2 ms 8784 KB Output is correct
5 Correct 2 ms 8784 KB Output is correct
6 Correct 2 ms 8784 KB Output is correct
7 Correct 2 ms 8784 KB Output is correct
8 Correct 3 ms 9212 KB Output is correct
9 Correct 3 ms 9040 KB Output is correct
10 Correct 2 ms 9040 KB Output is correct
11 Correct 3 ms 9040 KB Output is correct
12 Correct 3 ms 9040 KB Output is correct
13 Correct 4 ms 9468 KB Output is correct
14 Correct 4 ms 9552 KB Output is correct
15 Correct 4 ms 9296 KB Output is correct
16 Correct 4 ms 9476 KB Output is correct
17 Correct 4 ms 9464 KB Output is correct
18 Correct 4 ms 9296 KB Output is correct
19 Correct 5 ms 9480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 183 ms 35268 KB Output is correct
2 Correct 198 ms 37312 KB Output is correct
3 Correct 153 ms 32160 KB Output is correct
4 Correct 176 ms 35284 KB Output is correct
5 Correct 208 ms 36544 KB Output is correct
6 Correct 219 ms 35288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8784 KB Output is correct
2 Correct 2 ms 8784 KB Output is correct
3 Correct 2 ms 8784 KB Output is correct
4 Correct 2 ms 8784 KB Output is correct
5 Correct 2 ms 8784 KB Output is correct
6 Correct 2 ms 8784 KB Output is correct
7 Correct 2 ms 8784 KB Output is correct
8 Correct 3 ms 9212 KB Output is correct
9 Correct 3 ms 9040 KB Output is correct
10 Correct 2 ms 9040 KB Output is correct
11 Correct 3 ms 9040 KB Output is correct
12 Correct 3 ms 9040 KB Output is correct
13 Correct 4 ms 9468 KB Output is correct
14 Correct 4 ms 9552 KB Output is correct
15 Correct 4 ms 9296 KB Output is correct
16 Correct 4 ms 9476 KB Output is correct
17 Correct 4 ms 9464 KB Output is correct
18 Correct 4 ms 9296 KB Output is correct
19 Correct 5 ms 9480 KB Output is correct
20 Correct 183 ms 35268 KB Output is correct
21 Correct 198 ms 37312 KB Output is correct
22 Correct 153 ms 32160 KB Output is correct
23 Correct 176 ms 35284 KB Output is correct
24 Correct 208 ms 36544 KB Output is correct
25 Correct 219 ms 35288 KB Output is correct
26 Correct 223 ms 35780 KB Output is correct
27 Correct 209 ms 37320 KB Output is correct
28 Correct 222 ms 38076 KB Output is correct
29 Correct 151 ms 32340 KB Output is correct
30 Incorrect 204 ms 35564 KB Output isn't correct
31 Halted 0 ms 0 KB -