Submission #53161382


Source Code Expand

//for test

/*
  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
*/

#include<atcoder/dsu>
#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,m;
    cin>>n>>m;
    vector<vii> e(n);
    atcoder::dsu d(n);
    for(lli k=0;k<m;k++){
        lli u,v,c;
        cin>>u>>v>>c;
        u--;v--;
        if(d.same(u,v))
            continue;
        e[v].eb(ii{u,c});
        e[u].eb(ii{v,-c});
        d.merge(u,v);
    }

    const auto gps=d.groups();
    vi hei(n,-1);
    vii msks;
    vi maxAllowed;
    for(const auto &gp:gps){
        const lli root = gp[0];
        map<lli,lli> he;
        y_combinator([&](const auto &self,const lli u,const lli h)->void{
            if(he.count(u)){
                assert(he[u]==h);
                return;
            }
            he[u]=h;
            for(const auto &v:e[u])
                self(v.X,h+v.Y);
        })(root,0);

        lli minDis = INF,node=-1;
        for(const auto &u:he){
            if(minDis>u.Y){
                minDis=u.Y;
                node=u.X;
            }
        }

        lli curMsk=0,maxDist=-INF;
        for(const auto &u:he){
            const lli h=u.Y-minDis;
            hei[u.X]=h;
            assert(0<=h&&h<n);
            assert((curMsk&(1LL<<h))==0);
            curMsk|=1LL<<h;
            maxDist=max(maxDist,h);
        }
        msks.eb(ii{node,curMsk});
        maxAllowed.eb(n-maxDist);
    }
    dbg(msks);
    const lli tot = (1LL<<n)-1;
    const lli M = sz(gps);

    lli tim = 0,tr=-2,fal=-3;
    auto chk=[&](const lli exc,const lli rest)->bool{
        vector<vi> vis(M,vi(1LL<<n,-1));
        tim++;
        tr = tim;
        tim++;
        fal=tim;
        // dbg(exc,rest);
        return y_combinator([&](const auto &self,const lli idx,const lli rem)->bool{
            // dbg(exc,rest,idx,rem);
            if(idx==M)
                return rem==0;
            if(idx==exc)
                return self(idx+1,rem);
            auto &ans=vis[idx][rem];
            if(ans==tr||ans==fal)
                return ans == tr;
            for(lli r=0;r<=n;r++){
                const lli curMsk = msks[idx].Y<<r;
                dbg(msks[idx],r,curMsk);
                if((rem&curMsk)!=curMsk)
                    continue;
                if(self(idx+1,rem^curMsk)){
                    ans=tr;
                    return true;
                }
            }
            ans=fal;
            return false;
        })(0,rest);
    };

    vi ans(n,-1);
    dbg(hei);

    if(chk(-1,tot))
    for(lli j=0;j<M;j++){
        const auto &z=msks[j];
        vi posRank;
        for(lli r=0;r<=n;r++){
            const lli curMsk = z.Y<<r;
            if((tot&curMsk)!=curMsk)
                continue;
            const auto resp = chk(j,tot^curMsk);
            dbg(r,z,resp);
            if(resp){
                dbg("passed",r,z);
                posRank.eb(r);
            }
        }
        dbg(z,posRank);
        if(sz(posRank)!=1)
            continue;
        for(const auto &k:gps[j])
            ans[k]=posRank[0]+hei[k];
    }
    for(lli i=0;i<n;i++){
        if(ans[i]!=-1)
            ans[i]++;
        cout<<ans[i]<<" \n"[i+1==n];
    }
}   aryanc403();
    return 0;
}

Submission Info

Submission Time
Task F - Estimate Order
User TAoz1
Language C++ 20 (gcc 12.2)
Score 525
Code Size 6730 Byte
Status AC
Exec Time 690 ms
Memory 11992 KiB

Compile Error

Main.cpp: In function ‘int main()’:
Main.cpp:26:26: warning: statement has no effect [-Wunused-value]
   26 |     #define dbg(args...) 42;
      |                          ^~
Main.cpp:164:5: note: in expansion of macro ‘dbg’
  164 |     dbg(msks);
      |     ^~~
Main.cpp:26:26: warning: statement has no effect [-Wunused-value]
   26 |     #define dbg(args...) 42;
      |                          ^~
Main.cpp:201:5: note: in expansion of macro ‘dbg’
  201 |     dbg(hei);
      |     ^~~
Main.cpp:26:26: warning: statement has no effect [-Wunused-value]
   26 |     #define dbg(args...) 42;
      |                          ^~
Main.cpp:212:13: note: in expansion of macro ‘dbg’
  212 |             dbg(r,z,resp);
      |             ^~~
Main.cpp:26:26: warning: statement has no effect [-Wunused-value]
   26 |     #define dbg(args...) 42;
      |                          ^~
Main.cpp:214:17: note: in expansion of macro ‘dbg’
  214 |                 dbg("passed",r,z);
      |                 ^~~
Main.cpp:26:26: warning: statement has no effect [-Wunused-value]
   26 |     #define dbg(args...) 42;
      |                          ^~
Main.cpp:218:9: note: in expansion of macro ‘dbg’
  218 |         dbg(z,posRank);
      |         ^~~
Main.cpp: In instantiation of ‘main()::<lambda(lli, lli)>::<lambda(const auto:54&, lli, lli)> [with auto:54 = std::reference_wrapper<y_combinator_result<main()::<lambda(lli, lli)>::<lambda(const auto:54&, lli, lli)> > >; lli = long long int]’:
Main.cpp:36:84:   required from ‘decltype(auto) y_combinator_result<Fun>::operator()(Args&& ...) [with Args = {int, const long long int&}; Fun = main()::<lambda(lli, lli)>::<lambda(const auto:54&, lli, lli)>]’
Main.cpp:197:11:   required from here
Main.cpp:26:26: warning: statement has no effect [-Wunused-value]
   26 |     #define dbg(args...) 42;
      |                          ^~
Main.cpp:187:17: note: in expansion of macro ‘dbg’
  187 |                 dbg(msks[idx],r,curMsk);
      |                 ^~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 525 / 525
Status
AC × 3
AC × 62
Set Name Test Cases
Sample sample00.txt, sample01.txt, sample02.txt
All sample00.txt, sample01.txt, sample02.txt, testcase00.txt, testcase01.txt, testcase02.txt, testcase03.txt, testcase04.txt, testcase05.txt, testcase06.txt, testcase07.txt, testcase08.txt, testcase09.txt, testcase10.txt, testcase11.txt, testcase12.txt, testcase13.txt, testcase14.txt, testcase15.txt, testcase16.txt, testcase17.txt, testcase18.txt, testcase19.txt, testcase20.txt, testcase21.txt, testcase22.txt, testcase23.txt, testcase24.txt, testcase25.txt, testcase26.txt, testcase27.txt, testcase28.txt, testcase29.txt, testcase30.txt, testcase31.txt, testcase32.txt, testcase33.txt, testcase34.txt, testcase35.txt, testcase36.txt, testcase37.txt, testcase38.txt, testcase39.txt, testcase40.txt, testcase41.txt, testcase42.txt, testcase43.txt, testcase44.txt, testcase45.txt, testcase46.txt, testcase47.txt, testcase48.txt, testcase49.txt, testcase50.txt, testcase51.txt, testcase52.txt, testcase53.txt, testcase54.txt, testcase55.txt, testcase56.txt, testcase57.txt, testcase58.txt
Case Name Status Exec Time Memory
sample00.txt AC 1 ms 3844 KiB
sample01.txt AC 1 ms 3816 KiB
sample02.txt AC 1 ms 3708 KiB
testcase00.txt AC 2 ms 4296 KiB
testcase01.txt AC 2 ms 4168 KiB
testcase02.txt AC 2 ms 4156 KiB
testcase03.txt AC 3 ms 4792 KiB
testcase04.txt AC 4 ms 4788 KiB
testcase05.txt AC 3 ms 4712 KiB
testcase06.txt AC 9 ms 5204 KiB
testcase07.txt AC 12 ms 5204 KiB
testcase08.txt AC 11 ms 5312 KiB
testcase09.txt AC 21 ms 5904 KiB
testcase10.txt AC 18 ms 5764 KiB
testcase11.txt AC 25 ms 5732 KiB
testcase12.txt AC 40 ms 6356 KiB
testcase13.txt AC 39 ms 6364 KiB
testcase14.txt AC 36 ms 6304 KiB
testcase15.txt AC 68 ms 6792 KiB
testcase16.txt AC 67 ms 6812 KiB
testcase17.txt AC 62 ms 6812 KiB
testcase18.txt AC 89 ms 7336 KiB
testcase19.txt AC 91 ms 7356 KiB
testcase20.txt AC 82 ms 7408 KiB
testcase21.txt AC 128 ms 7820 KiB
testcase22.txt AC 135 ms 7912 KiB
testcase23.txt AC 130 ms 7888 KiB
testcase24.txt AC 189 ms 8488 KiB
testcase25.txt AC 192 ms 8376 KiB
testcase26.txt AC 158 ms 8536 KiB
testcase27.txt AC 224 ms 8980 KiB
testcase28.txt AC 230 ms 8888 KiB
testcase29.txt AC 233 ms 8884 KiB
testcase30.txt AC 286 ms 9572 KiB
testcase31.txt AC 287 ms 9492 KiB
testcase32.txt AC 289 ms 9512 KiB
testcase33.txt AC 358 ms 9964 KiB
testcase34.txt AC 346 ms 9876 KiB
testcase35.txt AC 354 ms 10040 KiB
testcase36.txt AC 440 ms 10384 KiB
testcase37.txt AC 420 ms 10624 KiB
testcase38.txt AC 427 ms 10312 KiB
testcase39.txt AC 554 ms 10872 KiB
testcase40.txt AC 504 ms 10872 KiB
testcase41.txt AC 492 ms 10880 KiB
testcase42.txt AC 589 ms 11440 KiB
testcase43.txt AC 597 ms 11452 KiB
testcase44.txt AC 602 ms 11456 KiB
testcase45.txt AC 688 ms 11992 KiB
testcase46.txt AC 690 ms 11844 KiB
testcase47.txt AC 689 ms 11832 KiB
testcase48.txt AC 111 ms 5284 KiB
testcase49.txt AC 39 ms 4004 KiB
testcase50.txt AC 282 ms 7340 KiB
testcase51.txt AC 2 ms 4068 KiB
testcase52.txt AC 13 ms 4316 KiB
testcase53.txt AC 27 ms 4976 KiB
testcase54.txt AC 1 ms 4068 KiB
testcase55.txt AC 2 ms 3972 KiB
testcase56.txt AC 2 ms 4072 KiB
testcase57.txt AC 9 ms 4868 KiB
testcase58.txt AC 3 ms 4772 KiB