답안 #859435

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
859435 2023-10-10T07:19:30 Z lucri Paths (RMI21_paths) C++17
100 / 100
308 ms 22504 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()
{
    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:161: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]
  161 |     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 2396 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 2396 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 2652 KB Output is correct
9 Correct 2 ms 2652 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 2396 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 2652 KB Output is correct
9 Correct 2 ms 2652 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 4 ms 2652 KB Output is correct
14 Correct 4 ms 2908 KB Output is correct
15 Correct 3 ms 2652 KB Output is correct
16 Correct 4 ms 2652 KB Output is correct
17 Correct 3 ms 2652 KB Output is correct
18 Correct 3 ms 2652 KB Output is correct
19 Correct 4 ms 2648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 237 ms 20240 KB Output is correct
2 Correct 216 ms 22352 KB Output is correct
3 Correct 195 ms 15808 KB Output is correct
4 Correct 248 ms 20412 KB Output is correct
5 Correct 277 ms 21484 KB Output is correct
6 Correct 270 ms 20832 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 2396 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 2652 KB Output is correct
9 Correct 2 ms 2652 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 4 ms 2652 KB Output is correct
14 Correct 4 ms 2908 KB Output is correct
15 Correct 3 ms 2652 KB Output is correct
16 Correct 4 ms 2652 KB Output is correct
17 Correct 3 ms 2652 KB Output is correct
18 Correct 3 ms 2652 KB Output is correct
19 Correct 4 ms 2648 KB Output is correct
20 Correct 237 ms 20240 KB Output is correct
21 Correct 216 ms 22352 KB Output is correct
22 Correct 195 ms 15808 KB Output is correct
23 Correct 248 ms 20412 KB Output is correct
24 Correct 277 ms 21484 KB Output is correct
25 Correct 270 ms 20832 KB Output is correct
26 Correct 257 ms 20588 KB Output is correct
27 Correct 217 ms 22096 KB Output is correct
28 Correct 280 ms 22504 KB Output is correct
29 Correct 198 ms 15956 KB Output is correct
30 Correct 251 ms 20944 KB Output is correct
31 Correct 278 ms 18080 KB Output is correct
32 Correct 285 ms 21420 KB Output is correct
33 Correct 308 ms 20608 KB Output is correct
34 Correct 183 ms 15240 KB Output is correct
35 Correct 293 ms 20792 KB Output is correct
36 Correct 250 ms 21776 KB Output is correct