답안 #509847

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
509847 2022-01-14T11:05:37 Z Rainbowbunny Paths (RMI21_paths) C++17
100 / 100
343 ms 32376 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

const int MAXN = 3e5 + 5;

int n, k;
ll mx[MAXN], res[MAXN], cur[MAXN];
pair <ll, int> dp[MAXN];
vector <pair <int, ll> > Adj[MAXN];

void DFS(int node, int p = -1)
{
    for(auto x : Adj[node])
    {
        if(x.first == p)
        {
            continue;
        }
        DFS(x.first, node);
        dp[x.first].first += x.second;
        dp[node] = max(dp[node], dp[x.first]);
    }
    if(dp[node].second == 0)
    {
        dp[node] = make_pair(0, node);
    }
}

ll ans;
set <pair <ll, int> > First, Last;

void Add(pair <ll, int> P)
{
    cur[P.second] = P.first; 
    if((int)First.size() < k)
    {
        ans += P.first;
        First.insert(P);
    }
    else
    {
        pair <ll, int> tmp = *First.begin();
        if(tmp.first > P.first)
        {
            Last.insert(P);
        }
        else
        {
            First.erase(First.begin());
            ans -= tmp.first;
            First.insert(P);
            ans += P.first;
            Last.insert(tmp);
        }
    }
}

void Sub(pair <ll, int> P)
{
    if(Last.count(P))
    {
        Last.erase(P);
    }
    else
    {
        ans -= P.first;
        // cout << P.first << 'T' << P.second << endl;
        // assert(First.count(P));
        First.erase(P);
        if(!Last.empty())
        {
            ans += (*Last.rbegin()).first;
            First.insert(*Last.rbegin());
            Last.erase(*Last.rbegin());
        }
    }
}

void Print()
{
    for(auto x : First)
    {
        cout << x.first << ' ' << x.second << endl;
    }
    cout << "owo" << endl;
    for(auto x : Last)
    {
        cout << x.first << ' ' << x.second << endl;
    }        
    cout << endl;
}

void Reroot(int node, int p = -1, pair <ll, int> Good = make_pair(-1, -1))
{   
    res[node] = ans;
    vector <pair <ll, int> > tmp;
    vector <pair <ll, int> > Pref;
    vector <pair <ll, int> > Suff;
    for(int i = 0; i < (int)Adj[node].size(); i++)
    {
        int x = Adj[node][i].first;
        if(x == p)
        {
            tmp.push_back(Good);
        }
        else
        {
            tmp.push_back(dp[x]);
        }
        Pref.emplace_back(max(i == 0 ? make_pair(-1ll, -1) : Pref[i - 1], tmp[i]));
    }
    Suff.resize((int)Pref.size());
    for(int i = (int)Adj[node].size() - 1; i >= 0; i--)
    {
        Suff[i] = max((i + 1 == (int)Adj[node].size()) ? make_pair(-1ll, -1) : Suff[i + 1], tmp[i]);
    }
    for(int i = 0; i < (int)Adj[node].size(); i++)
    {
        int x = Adj[node][i].first;
        if(x == p)
        {
            continue;
        }
        pair <ll, int> tmp = make_pair(-1, 0);
        if(Adj[node].size() == 1)
        {
            tmp = make_pair(0, node);
        }
        if(i)
        {
            tmp = max(tmp, Pref[i - 1]);
        }
        if(i + 1 != (int)Adj[node].size())
        {
            tmp = max(tmp, Suff[i + 1]);
        }
        Sub(tmp);
        Add(make_pair(tmp.first + Adj[node][i].second, tmp.second));

        Sub(dp[x]);
        Add(make_pair(dp[x].first - Adj[node][i].second, dp[x].second));

        Reroot(x, node, make_pair(tmp.first + Adj[node][i].second, tmp.second));
        
        Sub(make_pair(tmp.first + Adj[node][i].second, tmp.second));
        Add(tmp);
        

        Sub(make_pair(dp[x].first - Adj[node][i].second, dp[x].second));
        Add(dp[x]);
    }
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> k;
    for(int i = 1; i < n; i++)
    {
        int u, v, c;
        cin >> u >> v >> c;
        Adj[u].emplace_back(v, c);
        Adj[v].emplace_back(u, c);
    }
    DFS(1);
    for(int i = 1; i <= n; i++)
    {
        mx[dp[i].second] = max(mx[dp[i].second], dp[i].first);
    }
    for(int i = 1; i <= n; i++)
    {
        if((int)Adj[i].size() == 1)
        {
            Add(make_pair(mx[i], i));
        }
    }
    Reroot(1);
    for(int i = 1; i <= n; i++)
    {
        cout << res[i] << '\n';
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7372 KB Output is correct
2 Correct 4 ms 7372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7372 KB Output is correct
2 Correct 4 ms 7372 KB Output is correct
3 Correct 5 ms 7372 KB Output is correct
4 Correct 4 ms 7372 KB Output is correct
5 Correct 6 ms 7372 KB Output is correct
6 Correct 5 ms 7372 KB Output is correct
7 Correct 5 ms 7340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7372 KB Output is correct
2 Correct 4 ms 7372 KB Output is correct
3 Correct 5 ms 7372 KB Output is correct
4 Correct 4 ms 7372 KB Output is correct
5 Correct 6 ms 7372 KB Output is correct
6 Correct 5 ms 7372 KB Output is correct
7 Correct 5 ms 7340 KB Output is correct
8 Correct 7 ms 7504 KB Output is correct
9 Correct 8 ms 7628 KB Output is correct
10 Correct 6 ms 7572 KB Output is correct
11 Correct 6 ms 7500 KB Output is correct
12 Correct 7 ms 7504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7372 KB Output is correct
2 Correct 4 ms 7372 KB Output is correct
3 Correct 5 ms 7372 KB Output is correct
4 Correct 4 ms 7372 KB Output is correct
5 Correct 6 ms 7372 KB Output is correct
6 Correct 5 ms 7372 KB Output is correct
7 Correct 5 ms 7340 KB Output is correct
8 Correct 7 ms 7504 KB Output is correct
9 Correct 8 ms 7628 KB Output is correct
10 Correct 6 ms 7572 KB Output is correct
11 Correct 6 ms 7500 KB Output is correct
12 Correct 7 ms 7504 KB Output is correct
13 Correct 8 ms 7628 KB Output is correct
14 Correct 7 ms 8012 KB Output is correct
15 Correct 9 ms 7756 KB Output is correct
16 Correct 10 ms 7628 KB Output is correct
17 Correct 8 ms 7628 KB Output is correct
18 Correct 8 ms 7628 KB Output is correct
19 Correct 8 ms 7632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 323 ms 20696 KB Output is correct
2 Correct 330 ms 30752 KB Output is correct
3 Correct 233 ms 18388 KB Output is correct
4 Correct 305 ms 21548 KB Output is correct
5 Correct 265 ms 24980 KB Output is correct
6 Correct 265 ms 21812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7372 KB Output is correct
2 Correct 4 ms 7372 KB Output is correct
3 Correct 5 ms 7372 KB Output is correct
4 Correct 4 ms 7372 KB Output is correct
5 Correct 6 ms 7372 KB Output is correct
6 Correct 5 ms 7372 KB Output is correct
7 Correct 5 ms 7340 KB Output is correct
8 Correct 7 ms 7504 KB Output is correct
9 Correct 8 ms 7628 KB Output is correct
10 Correct 6 ms 7572 KB Output is correct
11 Correct 6 ms 7500 KB Output is correct
12 Correct 7 ms 7504 KB Output is correct
13 Correct 8 ms 7628 KB Output is correct
14 Correct 7 ms 8012 KB Output is correct
15 Correct 9 ms 7756 KB Output is correct
16 Correct 10 ms 7628 KB Output is correct
17 Correct 8 ms 7628 KB Output is correct
18 Correct 8 ms 7628 KB Output is correct
19 Correct 8 ms 7632 KB Output is correct
20 Correct 323 ms 20696 KB Output is correct
21 Correct 330 ms 30752 KB Output is correct
22 Correct 233 ms 18388 KB Output is correct
23 Correct 305 ms 21548 KB Output is correct
24 Correct 265 ms 24980 KB Output is correct
25 Correct 265 ms 21812 KB Output is correct
26 Correct 311 ms 21484 KB Output is correct
27 Correct 343 ms 30396 KB Output is correct
28 Correct 299 ms 32376 KB Output is correct
29 Correct 206 ms 19192 KB Output is correct
30 Correct 284 ms 22112 KB Output is correct
31 Correct 257 ms 20680 KB Output is correct
32 Correct 305 ms 26156 KB Output is correct
33 Correct 274 ms 21468 KB Output is correct
34 Correct 175 ms 18264 KB Output is correct
35 Correct 279 ms 21952 KB Output is correct
36 Correct 287 ms 31128 KB Output is correct