Submission #717436

# Submission time Handle Problem Language Result Execution time Memory
717436 2023-04-01T22:59:37 Z vjudge1 Paths (RMI21_paths) C++17
31 / 100
600 ms 24064 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define int long long
#define endl '\n'
using namespace std;
using namespace __gnu_pbds;
using ordered_set = tree<pair<int,int>,null_type,less<pair<int,int>>,rb_tree_tag,tree_order_statistics_node_update>;
vector<pair<int,int>>vct[200001];
vector<pair<int,int>>vec[200001];
int fath[200001];
int mx[200001];
int mxx[200001];
int mp[1001][1001];
int n,in=1;
long long ans;
void dfss(int in){
    if(in==1){
        vector<pair<int,int>>v;
        for(auto &i:vec[in]){
            v.push_back({mx[i.first]+i.second,i.first});
        }
        sort(v.begin(),v.end());
        for(auto &i:vec[in]){
            if(i.first!=v.back().second)mxx[i.first]=v.back().first;
            else if(v.size()>1)mxx[i.first]=v[v.size()-2].first;
            mxx[i.first]+=i.second;
            dfss(i.first);
        }
    }else{
        for(auto &i:vec[in]){
            mxx[i.first]=i.second+mxx[in];
            dfss(i.first);
        }
    }
}
int dfs(int in){
    for(auto &i:vec[in]){
        mx[in]=max(mx[in],dfs(i.first)+i.second);
    }
    return mx[in];
}
void slv(){
    deque<pair<long long,int>>pq={{0,in}};
    vector<int>vis(n+1);
    long long g=0;
    vis[in]=in;
    while(pq.size()){
        int a=pq.front().second;
        long long b=pq.front().first;
        pq.pop_front();
        if(b>=g){
            g=b;
            in=a;
        }
        for(auto &i:vct[a]){
            if(!vis[i.first]){
                vis[i.first]=a;
                pq.push_back({b+mp[a][i.first],i.first});
            }
        }
    }
    int I=in;
    while(vis[I]!=I){
        ans+=mp[vis[I]][I];
        mp[vis[I]][I]=0;
        mp[I][vis[I]]=0;
        I=vis[I];
    }
}
signed main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int k;
    cin>>n>>k;
    for(int i=1;i<n;i++){
        int a,b,c=1;
        cin>>a>>b>>c;
        vct[a].push_back({b,c});
        vct[b].push_back({a,c});
    }
    if(k!=1){
        vector<int>v(n+1,-1);
        int K=k;
        for(int w=1;w<=n;w++){
            ans=0;
            in=w;
            for(int j=1;j<=n;j++){
                for(auto &z:vct[j]){
                    mp[j][z.first]=z.second;
                }
            }
            k=K;
            while(k--)slv();
            v[in]=ans;
            cout<<ans<<endl;
        }
    }else{
        deque<int>dq={1};
        vector<bool>vis(n+1);
        vis[1]=1;
        while(dq.size()){
            int a=dq.front();
            dq.pop_front();
            for(auto &i:vct[a]){
                if(!vis[i.first]){
                    vis[i.first]=1;
                    dq.push_back(i.first);
                    vec[a].push_back(i);
                    fath[i.first]=a;
                }
            }
        }
        dfs(1);
        dq.push_back(1);
        for(int i=1;i<=n;i++)vis[i]=0;
        while(dq.size()){
            int a=dq.front();
            dq.pop_front();
            vector<pair<int,int>>v;
            for(auto &i:vec[a]){
                v.push_back({mx[i.first]+i.second,i.first});
                dq.push_back(i.first);
            }
            sort(v.begin(),v.end());
            for(auto &i:vec[a]){
                if(i.first!=v.back().second)mxx[i.first]=v.back().first;
                else if(v.size()>1)mxx[i.first]=v[v.size()-2].first;
                mxx[i.first]=max(mxx[i.first]+i.second,mxx[a]+i.second);
            }
        }
        for(int i=1;i<=n;i++)cout<<max(mx[i],mxx[i])<<endl;
    }
}
/*
11 1
1 2 5
2 3 3
2 6 5
3 4 4
3 5 2
1 7 6
7 8 4
7 9 5
1 10 1
10 11 1
*/
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9692 KB Output is correct
2 Correct 7 ms 9684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9692 KB Output is correct
2 Correct 7 ms 9684 KB Output is correct
3 Correct 19 ms 10580 KB Output is correct
4 Correct 20 ms 10580 KB Output is correct
5 Correct 17 ms 10608 KB Output is correct
6 Correct 15 ms 10580 KB Output is correct
7 Correct 18 ms 10520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9692 KB Output is correct
2 Correct 7 ms 9684 KB Output is correct
3 Correct 19 ms 10580 KB Output is correct
4 Correct 20 ms 10580 KB Output is correct
5 Correct 17 ms 10608 KB Output is correct
6 Correct 15 ms 10580 KB Output is correct
7 Correct 18 ms 10520 KB Output is correct
8 Execution timed out 1075 ms 14980 KB Time limit exceeded
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9692 KB Output is correct
2 Correct 7 ms 9684 KB Output is correct
3 Correct 19 ms 10580 KB Output is correct
4 Correct 20 ms 10580 KB Output is correct
5 Correct 17 ms 10608 KB Output is correct
6 Correct 15 ms 10580 KB Output is correct
7 Correct 18 ms 10520 KB Output is correct
8 Execution timed out 1075 ms 14980 KB Time limit exceeded
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 128 ms 22332 KB Output is correct
2 Correct 91 ms 24064 KB Output is correct
3 Correct 82 ms 23272 KB Output is correct
4 Correct 87 ms 22940 KB Output is correct
5 Correct 99 ms 23516 KB Output is correct
6 Correct 97 ms 23044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9692 KB Output is correct
2 Correct 7 ms 9684 KB Output is correct
3 Correct 19 ms 10580 KB Output is correct
4 Correct 20 ms 10580 KB Output is correct
5 Correct 17 ms 10608 KB Output is correct
6 Correct 15 ms 10580 KB Output is correct
7 Correct 18 ms 10520 KB Output is correct
8 Execution timed out 1075 ms 14980 KB Time limit exceeded
9 Halted 0 ms 0 KB -