Submission #57333911


Source Code Expand

/*
  Compete against Yourself.
  Author - Aryan (@aryanc403)
*/
/*
  Credits -
  Atcoder library - https://atcoder.github.io/ac-library/production/document_en/ (namespace atcoder)
  Github source code of library - https://github.com/atcoder/ac-library/tree/master/atcoder
  https://codeforces.com/contest/4/submission/150120627
*/

#ifdef ARYANC403
    #include <header.h>
#else
    #pragma GCC optimize ("Ofast")
    // #pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
    #pragma GCC target ("sse,sse2,mmx")
    #pragma GCC optimize ("-ffloat-store")
    #include <bits/stdc++.h>
    #include <ext/pb_ds/assoc_container.hpp>
    #include <ext/pb_ds/tree_policy.hpp>
    #define dbg(args...) 42;
    #define endl "\n"
#endif

// y_combinator from @neal template https://codeforces.com/contest/1553/submission/123849801
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0200r0.html
template<class Fun> class y_combinator_result {
    Fun fun_;
public:
    template<class T> explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}
    template<class ...Args> decltype(auto) operator()(Args &&...args) { return fun_(std::ref(*this), std::forward<Args>(args)...); }
};
template<class Fun> decltype(auto) y_combinator(Fun &&fun) { return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun)); }

using namespace std;
#define fo(i,n)   for(i=0;i<(n);++i)
#define repA(i,j,n)   for(i=(j);i<=(n);++i)
#define repD(i,j,n)   for(i=(j);i>=(n);--i)
#define all(x) begin(x), end(x)
#define sz(x) ((lli)(x).size())
#define eb emplace_back
#define X first
#define Y second

using lli = long long int;
using mytype = long double;
using ii = pair<lli,lli>;
using vii = vector<ii>;
using vi = vector<lli>;

template <class T>
using ordered_set =  __gnu_pbds::tree<T,__gnu_pbds::null_type,less<T>,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>;
// X.find_by_order(k) return kth element. 0 indexed.
// X.order_of_key(k) returns count of elements strictly less than k.

// namespace Re = std::ranges;
// namespace Ve = std::ranges::views;

const auto start_time = std::chrono::high_resolution_clock::now();
void aryanc403()
{
auto end_time = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = end_time-start_time;
    cerr<<"Time Taken : "<<diff.count()<<"\n";
}

const lli INF = 0xFFFFFFFFFFFFFFFLL;
const lli SEED=chrono::steady_clock::now().time_since_epoch().count();
mt19937_64 rng(SEED);
inline lli rnd(lli l=0,lli r=INF)
{return uniform_int_distribution<lli>(l,r)(rng);}

class CMP
{public:
bool operator()(ii a , ii b) //For min priority_queue .
{    return ! ( a.X < b.X || ( a.X==b.X && a.Y <= b.Y ));   }};

void add( map<lli,lli> &m, lli x,lli cnt=1)
{
    auto jt=m.find(x);
    if(jt==m.end())         m.insert({x,cnt});
    else                    jt->Y+=cnt;
}

void del( map<lli,lli> &m, lli x,lli cnt=1)
{
    auto jt=m.find(x);
    if(jt->Y<=cnt)            m.erase(jt);
    else                      jt->Y-=cnt;
}

bool cmp(const ii &a,const ii &b)
{
    return a.X<b.X||(a.X==b.X&&a.Y<b.Y);
}

int main(void) {
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    // freopen("txt.in", "r", stdin);
    // freopen("txt.out", "w", stdout);
// cout<<std::fixed<<std::setprecision(35);
// Ve::iota(1, 6) | Ve::transform([](int x) { return x * 2; }) | Ve::reverse | Ve::take(3)
lli T=1;
// cin>>T;
while(T--)
{
    lli n;
    cin>>n;
    vector<vii> e(n);
    for(lli j=1;j<n;j++){
        lli u,v,w;
        cin>>u>>v>>w;
        u--;v--;
        e[u].eb(ii{v,w});
        e[v].eb(ii{u,w});
    }

    vi b;
    const auto tp=y_combinator([&](const auto &self,const lli u,const lli p)->lli{
        lli dis=0;
        for(const auto &x:e[u]){
            if(x.X==p)
                continue;
            const lli cld=x.Y+self(x.X,u);
            if(dis>=cld){
                b.eb(cld);
                continue;
            }
            b.eb(dis);
            dis=cld;
        }
        return dis;
    })(0,-1);
    b.eb(tp);

    sort(all(b));
    reverse(all(b));
    b.resize(n);

    for(lli j=1;j<n;j++)
        b[j]+=b[j-1];

    for(lli j=0;j<n;j++)
        cout<<2*b[j]<<endl;

}   aryanc403();
    return 0;
}

Submission Info

Submission Time
Task G - As far as possible
User aryanc403
Language C++ 20 (gcc 12.2)
Score 600
Code Size 4371 Byte
Status AC
Exec Time 120 ms
Memory 37672 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 2
AC × 55
Set Name Test Cases
Sample example_00.txt, example_01.txt
All example_00.txt, example_01.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, hand_08.txt, hand_09.txt, hand_10.txt, hand_11.txt, hand_12.txt, hand_13.txt, hand_14.txt, hand_15.txt, hand_16.txt, hand_17.txt, hand_18.txt, hand_19.txt, hand_20.txt, hand_21.txt, hand_22.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt
Case Name Status Exec Time Memory
example_00.txt AC 1 ms 3740 KiB
example_01.txt AC 1 ms 3740 KiB
hand_00.txt AC 113 ms 37672 KiB
hand_01.txt AC 71 ms 21280 KiB
hand_02.txt AC 98 ms 29128 KiB
hand_03.txt AC 110 ms 37624 KiB
hand_04.txt AC 76 ms 21260 KiB
hand_05.txt AC 103 ms 29188 KiB
hand_06.txt AC 116 ms 30492 KiB
hand_07.txt AC 107 ms 21916 KiB
hand_08.txt AC 98 ms 20580 KiB
hand_09.txt AC 88 ms 19848 KiB
hand_10.txt AC 111 ms 21912 KiB
hand_11.txt AC 85 ms 20184 KiB
hand_12.txt AC 72 ms 20332 KiB
hand_13.txt AC 85 ms 20180 KiB
hand_14.txt AC 59 ms 14984 KiB
hand_15.txt AC 120 ms 26012 KiB
hand_16.txt AC 119 ms 24336 KiB
hand_17.txt AC 99 ms 21984 KiB
hand_18.txt AC 97 ms 24672 KiB
hand_19.txt AC 101 ms 23068 KiB
hand_20.txt AC 104 ms 20796 KiB
hand_21.txt AC 76 ms 23088 KiB
hand_22.txt AC 111 ms 35924 KiB
random_00.txt AC 106 ms 20288 KiB
random_01.txt AC 108 ms 20300 KiB
random_02.txt AC 103 ms 20244 KiB
random_03.txt AC 98 ms 20292 KiB
random_04.txt AC 97 ms 20344 KiB
random_05.txt AC 104 ms 20284 KiB
random_06.txt AC 109 ms 20244 KiB
random_07.txt AC 97 ms 20304 KiB
random_08.txt AC 99 ms 20352 KiB
random_09.txt AC 98 ms 20280 KiB
random_10.txt AC 106 ms 20348 KiB
random_11.txt AC 104 ms 20260 KiB
random_12.txt AC 97 ms 20288 KiB
random_13.txt AC 100 ms 20292 KiB
random_14.txt AC 108 ms 20304 KiB
random_15.txt AC 105 ms 20428 KiB
random_16.txt AC 98 ms 20508 KiB
random_17.txt AC 94 ms 20436 KiB
random_18.txt AC 94 ms 20448 KiB
random_19.txt AC 106 ms 20408 KiB
random_20.txt AC 96 ms 20444 KiB
random_21.txt AC 97 ms 20464 KiB
random_22.txt AC 94 ms 20752 KiB
random_23.txt AC 100 ms 20776 KiB
random_24.txt AC 94 ms 20692 KiB
random_25.txt AC 93 ms 20676 KiB
random_26.txt AC 88 ms 20752 KiB
random_27.txt AC 89 ms 20796 KiB
random_28.txt AC 98 ms 20736 KiB
random_29.txt AC 93 ms 20832 KiB