답안 #859444

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
859444 2023-10-10T07:26:01 Z lucri Paths (RMI21_paths) C++17
100 / 100
239 ms 20536 KB
#include <bits/stdc++.h>
using namespace std;
long long dp[200010],sum,n,x,y,z,k,ans[100010],or1[100010],or2[100010];
set<pair<long long,long long>>s;
vector<vector<pair<int,int>>>a;
set<pair<long long,long long>>::iterator it,it2;
void initializeaza(int nod,int t)
{
    for(auto x:a[nod])
    {
        if(x.first==t)
        {
            dp[nod]=x.second;
            continue;
        }
        initializeaza(x.first,nod);
    }
    int pozmax=0;
    for(auto x:a[nod])
    {
        if(x.first==t)
            continue;
        if(dp[pozmax]<=dp[x.first])
            pozmax=x.first;
    }
    dp[nod]+=dp[pozmax];
    for(auto x:a[nod])
    {
        if(x.first==t||x.first==pozmax)
            continue;
        s.insert({dp[x.first],x.first});
    }
}
void swaprad(int rad,int newrad,int val)
{
    if(or1[rad]==0)
    {
        for(auto x:a[rad])
        {
            if(dp[x.first]>=dp[or1[rad]])
            {
                or1[rad]=x.first;
            }
        }
    }
    if(or2[rad]==0)
    {
        for(auto x:a[rad])
        {
            if(x.first!=or1[rad]&&dp[x.first]>=dp[or2[rad]])
            {
                or2[rad]=x.first;
            }
        }
    }
    int leg=or1[rad];
    if(leg==newrad)
        leg=or2[rad];
    it2=s.find({dp[leg],leg});
    if(*it2>=*it)
    {
        --it;
        sum-=dp[leg];
        sum+=(*it).first;
    }
    s.erase({dp[leg],leg});
    dp[rad]=dp[leg]+val;
    s.insert({dp[rad],rad});
    it2=s.find({dp[rad],rad});
    if(*it2>*it)
    {
        sum-=(*it).first;
        ++it;
        sum+=dp[rad];
    }
    it2=s.find({dp[newrad],newrad});
    if(*it2>=*it)
    {
        --it;
        sum-=dp[newrad];
        sum+=(*it).first;
    }
    s.erase({dp[newrad],newrad});
    dp[newrad]=0;
    if(or1[newrad]==0)
    {
        for(auto x:a[newrad])
        {
            if(dp[x.first]>=dp[or1[newrad]])
            {
                or1[newrad]=x.first;
            }
        }
    }
    if(or2[newrad]==0)
    {
        for(auto x:a[newrad])
        {
            if(x.first!=or1[newrad]&&dp[x.first]>=dp[or2[newrad]])
            {
                or2[newrad]=x.first;
            }
        }
    }
    leg=or1[newrad];
    if(leg==rad)
        leg=or2[newrad];
    s.insert({dp[leg],leg});
    it2=s.find({dp[leg],leg});
    if(*it2>*it)
    {
        sum-=(*it).first;
        ++it;
        sum+=dp[leg];
    }
    ans[newrad]=sum;
}
void raspunde(int nod,int nodant,int val)
{
    for(auto x:a[nod])
    {
        if(x.first==nodant||x.first>n)
            continue;
        swaprad(nod,x.first,x.second);
        raspunde(x.first,nod,x.second);
    }
    if(nodant)
        swaprad(nod,nodant,val);
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>k;
    a.resize(2*n+5);
    for(int i=1;i<n;++i)
    {
        cin>>x>>y>>z;
        a[x].push_back({y,z});
        a[y].push_back({x,z});
    }
    int nr=n;
    for(int i=1;i<=n;++i)
    {
        if(a[i].size()==1)
        {
            ++nr;
            a[i].push_back({nr,0});
            a[nr].push_back({i,0});
        }
    }
    initializeaza(1,0);
    for(auto x:a[1])
    {
        if(s.find({dp[x.first],x.first})==s.end())
        {
            or1[1]=x.first;
            s.insert({dp[x.first],x.first});
            break;
        }
    }
    dp[1]=0;
    if(k>=s.size())
    {
        for(auto x:s)
            sum+=x.first;
        for(int i=1;i<=n;++i)
            cout<<sum<<'\n';
        return 0;
    }
    it=s.end();
    for(int i=1;i<=k;++i)
    {
        --it;
        sum+=(*it).first;
    }
    ans[1]=sum;
    raspunde(1,0,0);
    for(int i=1;i<=n;++i)
        cout<<ans[i]<<'\n';
    return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:164:9: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::set<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  164 |     if(k>=s.size())
      |        ~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2552 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2552 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 2 ms 2648 KB Output is correct
9 Correct 2 ms 2648 KB Output is correct
10 Correct 2 ms 2396 KB Output is correct
11 Correct 2 ms 2652 KB Output is correct
12 Correct 2 ms 2652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2552 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 2 ms 2648 KB Output is correct
9 Correct 2 ms 2648 KB Output is correct
10 Correct 2 ms 2396 KB Output is correct
11 Correct 2 ms 2652 KB Output is correct
12 Correct 2 ms 2652 KB Output is correct
13 Correct 3 ms 2652 KB Output is correct
14 Correct 4 ms 2908 KB Output is correct
15 Correct 4 ms 2652 KB Output is correct
16 Correct 3 ms 2652 KB Output is correct
17 Correct 4 ms 2652 KB Output is correct
18 Correct 2 ms 2652 KB Output is correct
19 Correct 3 ms 2680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 235 ms 18284 KB Output is correct
2 Correct 224 ms 20264 KB Output is correct
3 Correct 163 ms 13532 KB Output is correct
4 Correct 232 ms 18300 KB Output is correct
5 Correct 212 ms 19272 KB Output is correct
6 Correct 202 ms 18600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2552 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 2 ms 2648 KB Output is correct
9 Correct 2 ms 2648 KB Output is correct
10 Correct 2 ms 2396 KB Output is correct
11 Correct 2 ms 2652 KB Output is correct
12 Correct 2 ms 2652 KB Output is correct
13 Correct 3 ms 2652 KB Output is correct
14 Correct 4 ms 2908 KB Output is correct
15 Correct 4 ms 2652 KB Output is correct
16 Correct 3 ms 2652 KB Output is correct
17 Correct 4 ms 2652 KB Output is correct
18 Correct 2 ms 2652 KB Output is correct
19 Correct 3 ms 2680 KB Output is correct
20 Correct 235 ms 18284 KB Output is correct
21 Correct 224 ms 20264 KB Output is correct
22 Correct 163 ms 13532 KB Output is correct
23 Correct 232 ms 18300 KB Output is correct
24 Correct 212 ms 19272 KB Output is correct
25 Correct 202 ms 18600 KB Output is correct
26 Correct 231 ms 18520 KB Output is correct
27 Correct 222 ms 20116 KB Output is correct
28 Correct 211 ms 20536 KB Output is correct
29 Correct 163 ms 13764 KB Output is correct
30 Correct 231 ms 18692 KB Output is correct
31 Correct 216 ms 16560 KB Output is correct
32 Correct 212 ms 19360 KB Output is correct
33 Correct 210 ms 18552 KB Output is correct
34 Correct 130 ms 13240 KB Output is correct
35 Correct 239 ms 18672 KB Output is correct
36 Correct 185 ms 19900 KB Output is correct