Submission #1243090

#TimeUsernameProblemLanguageResultExecution timeMemory
1243090hoangmc2009Race (IOI11_race)C++17
100 / 100
643 ms39184 KiB
#include <bits/stdc++.h>
#define fi first
#define se second
#define Log2(x) 63-__builtin_clzll(x)
using namespace std;
using i64 = long long;
using f64 = long double;
using ui64 = unsigned long long;
using pII = pair<int,int>;
using pLL = pair<i64,i64>;
mt19937 rng32(time(0));
mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
int n,k,vis[200009],sz[200009],mnL=2e9,c=0;
vector<pII> adj[200009];
map<int,int> tmp;
void calcSize(int u,int pu)
{
    sz[u]=1;
    for(auto v:adj[u])
    {
        if(v.fi!=pu and !vis[v.fi])
        {
            calcSize(v.fi,u);
            sz[u]+=sz[v.fi];
        }
    }
}
int centroid(int root,int u,int pu)
{
    for(auto v:adj[u])
    {
        if(v.fi!=pu and !vis[v.fi] and sz[v.fi]>sz[root]/2)
            return centroid(root,v.fi,u);
    }
    return u;
}
void preCalc(int u,int pu,int val,int depth=1)
{
    tmp[val]=min((tmp.find(val)!=tmp.end()?tmp[val]:int(2e9)),depth);
    for(auto& v:adj[u])
    {
        if(v.fi!=pu and !vis[v.fi])
            preCalc(v.fi,u,val+v.se,depth+1);
    }
}
void postCalc(int u,int pu,int val,int depth=1)
{
    if(val>k) return;
    mnL=min(mnL,depth+(tmp.find(k-val)!=tmp.end()?tmp[k-val]:int(2e9)));
    for(auto& v:adj[u])
    {
        if(v.fi!=pu and !vis[v.fi])
            postCalc(v.fi,u,val+v.se,depth+1);
    }
}
void calc(int root)
{
    tmp.clear();
    vis[root]=1; tmp[0]=0;
    for(auto& u:adj[root])
    {
        if(!vis[u.fi])
        {
            postCalc(u.fi,root,u.se);
            preCalc(u.fi,root,u.se);
        }
    }
    for(auto& u:adj[root])
    {
        if(!vis[u.fi])
        {
            calcSize(u.fi,root);
            calc(centroid(u.fi,u.fi,root));
        }
    }
}
int best_path(int N,int K,int H[][2],int L[])
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); n=N; k=K;
    for(int i=0;i<N-1;++i)
    {
        adj[H[i][0]].push_back({H[i][1],L[i]});
        adj[H[i][1]].push_back({H[i][0],L[i]});
    }
    calcSize(1,0); calc(centroid(1,1,0));
    return (mnL>1e9?-1:mnL);
}
/*
                                                                                                   ........................
                                                                                            ..::-+**#%@@@@@@@@@@@@@@@@@@%#**+::..
                                                                                  ..:=++#%%@@@@@@@@@@@@@@@@%%%%%%%%%@@@@@@@@@@@@%+.
                                                                           ..:-+*#%@@@@@@@@%%%%##############################%%@@@@*:.
                                                                     ..:-+*%@@@@@@@%%%##########################################%@@@@+:.
                                                                ..:=+%@@@@@@%%#####################################################@@@@+..
                                                          ....-+%@@@@@@%#############################################################@@@@=..
                                                        ..=#@@@@@%%###################################################################%@@@%-.
                                                    .:+#%@@@@%%#########################################################################%@@@#:.
                                                .:=*%@@@%%###############################################################################%@@@@-.
                                            ..-*%@@@%%#####################################################################################%@@@=.
                                          ..+@@@@###########################################################################################%@@@*..
                                         .*@@@#*##############################################################################################@@@%.
                                       .+@@@#**################################################################################################@@@%:.
                                     .-%@@#**####%##############################################################################################%@@%:.
                                    .+@@#***###%%################################################################################################%@@@-.
                                   :#@%****###%%*#################################################################################################%@@@-.
                                  -@@%****###@%*###################################################################################################%@@@-.
                                 -@@#***###%@#*#####################################################################################################%@@@=.
                                -@@#***###@@#*#######################################################################################################@@@%-.
                               :@@#***###@%**#########################################################################################################@@@+.
                              :%@#***##%@%**##########################################################################################################%@@@.
                             .#@%***##%@%**############################################################################################################%@@*.
                            .+@%***##@@#**##############################################################################################@@##############@@@:.
                            :@@***##@@#**###############################################################################################@@@%############%@@#.
                           .%@#***#@@#**####################################%@@%#######################################################@@@@@@############@@@=.
                          .*@%***%@@#***##################################%@@@@@@@%###################################################%@@@@@@@############@@%.
                          -@@***%@@#***##################################@@@@@@@@@@@%#################################################@@@@@@@@@###########@@@=.
                         .#@#**#@@#****################################%@@@@@@@@@@@@@@@%#############################################%@@@@@@@@@@###########@@%.
                         =@%**#@@%****################################@@@@@@@@@@@@@@@@@@@%###########################################@@@@@@@@@@@@##########%@@=.
                        .@@**#@@%*****##############################%@@@@@@@@@@@@@@@@@@@@@@@########################################@@@@@@@@@@@@@@##########@@%.
                       .#@###@@%*****##############################@@@@@@@@@@@@@@@@@@@@@@@@@@@#####################################@@@@@@@@@@@@@@@@#########@@@-
                     ..#@###%@@******############################%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@##################################%@@@@@@@@@@@@@@@@@#########@@#.
                    .-%@###%@@#*****############################%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@###############################%@@@@@@@@@@@@@@@@@@@########@@@:.
                ...-%@%####@@#******###########################@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%############################%@@@@@@@@@@@@@@@@@@@@%#######%@@*.
             ..:+%@@######%@%******###########################@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%##########################@@@@@@@@@@@@@@@@@@@@@@%#######@@@.
          .#@%%%##########@@*******#########################%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%######################%@@@@@@@@@@@@@@@@@@@@@@@@%######%@@+.
           :#@%##########@@#******#########################@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@####################%@@@@@@@@@@@@@@@@@@@@@@@@@@#######@@%.
            .+%@%#######%@%*******########################@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%#################%@@@@@@@@@@@@@@@@@@@@@@@@@@@@######%@@-.
              .+%@@%####@@#******#######################%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@###############%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@######@@#.
                .:*%@@@@@%*******######################%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#############@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%#####%@@:.
                 .....-@@********#####################%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%#########%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%#####@@*.
                     .=@%*******#####################%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#######@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#####@@@.
                     .%@#*******####################@@@@@@@@@@@@@@@@@@@@@@@@@@%##*******##%@@@@@@@@@@@@@@@@@@@@@@####%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#####@@=.
                    .+@@********###################@@@@@@@@@@@@@@@@@@@@@@#*=:...............:=*#@@@@@@@@@@@@@@@@@@#%@@@@@@@@@@@@@@#*+-:.......:-=+#%@@@@@@@@@%####@@%.
                    .@@%*******###################@@@@@@@@@@@@@@@@@@@@#-.........................-*%@@@@@@@@@@@@@@@@@@@@@@@@@@%*-....................=#@@@@@@@#####@@=.
                   .+@@********##################@@@@@@@@@@@@@@@@@@@=...............................-%@@@@@@@@@@@@@@@@@@@@@@%-..........................=@@@@@@####@@%.
                   :@@@********#################@@@@@@@@@@@@@@@@@@=...................................-@@@@@@@@@@@@@@@@@@@@-..............................+@@@@%####@@+.
                  .*@@#********################%@@@@@@@@@@@@@@@@#:..........................:=+++=:....:*@@@@@@@@@@@@@@@@*:....:=+++=:.....................:%@@@####%@@:.
                  :@@@********################%@@@@@@@@@@@@@@@@*...........................=*#%%%#*=.....+@@@@@@@@@@@@@@=.....-*#%%%#*-.....................:#@@%####@@#.
                 .*@@%********###############%@@@@@@@@@@@@@@@@#...........................-*%@@@@@%*-.....*@@@@@@@@@@@@+.....-*%@@@@@#*-.....................:@@@####%@@*.
                .=@@@*********###############@@@@@@@@@@@@@@@@@:...........................*=-@@@@@@#*.....:@@@@@@@@@@@@......*=+@@@@@@*+......................=@@@####@@@=.
                -@@@**********##############@@@@@@@@@@@@@@@@@#............................*..+@@@@@#*......#@@@@@@@@@@*.....:*..#@@@@@#*......................:@@@%####@@@=.
              .+@@@#**********#############%@@@@@@@@@@@@@@@@@*............................+-.%@@@@@*+......#@@@@@@@@@@+......*:.%@@@@@*+.......................@@@@#####@@@=.
            .-%@@@***********##############@@@@@@@@@@@@@@@@@@#............................-*#@@@@@#*:......%@@@@@@@@@@+......=*#@@@@@#*:......................:@@@@%#####@@@+.
         ..=#@@@#************#############@@@@@@@@@@@@@@@@@@@@:............................-+*###*+:......:@@@@@@@@@@@#.......=*#%%#*+:.......................=@@@@@######@@@#.
    ...:=*@@@%#*************#############%@@@@@@@@@@@@@@@@@@@@*..............................:===:........#@@@@@@@@@@@@-.......:=++=-.........................%@@@@@@######@@@%-.
 .-+*%@@@@%#****************#############@@@@@@@@@@@@@@@@@@@@@@=.........................................+@@@@@@@@@@@@@@:....................................#@@@@@@@#######%@@@*..
 -@@%**********************#############%@@@@@@@@@@@@@@@@@@@@@@@+.......................................#@@@@@@@@@@@@@@@@:..................................#@@@@@@@@@########@@@@=.
 .:%@%********************##############@@@@@@@@@@@@@@@@@@@@@@@@@%-...................................=@@@@@@@@@@@@@@@@@@@*...............................=@@@@@@@@@@@%########%@@@%+.
   .+@@#******************##############@@@@@@@@@@@@@@@@@@@@@@@@@@@#=..............................:+%@@@@@@@@@@@@@@@@@@@@@%+:..........................=#@@@@@@@@@@@@@##########%@@@@*:.
    .:%@@#****************#############%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%+-........................:=*@@@@@@@@@%%@@@@@@@@@@%@@@@@*-:....................-+@@@@@@@@@@@@@@@@%###########%@@@+.
      .+@@%***************#############%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*=-:..............::-+#@@@@@@@%#+-:::-#@@@@@@#::-+#@@@@@#=-::.........::-=*@@@@@@@@@@@@@@@@@@@@##########%@@@+.
        .#@@%*************#############@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#*++++++*#%@@@@@@@@@@%=:::::::::::+@@@=:::::::-#@@@@@@@@@%%%%%@@@@@@@@@@@@@@@@@@@@@@@@@@@%########%@@@=.
         .-%@@%************############@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#=-:::::::::::::::-:::::::::::-=@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%#######@@@@-.
          ..=%@@%**********############@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@+-:::::::::::::::::::::::::::::::::=%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@######%@@@#:.
             .=%@@%*********###########%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%=:::::::::::::::-+####=::::::::::::::::+@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%######@@@@=..
               .=%@@%#*******############%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%=::::::::::::::::+#+-::=#*::::::::::::::::-#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@######%@@@%-.
                 .=%@@@#******#############%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@+:::::::::::::::::*-:::::::*+:::::::::::::::::*@@@@@@@@@@@@@@@@@@@@@@@@@@@%######%@@@+..
                   .-%@@@%*****###############@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#::::::::::::::::::-::::::::::=::::::::::::::::::=@@@@@@@@@@@@@@@@@@@@@@@@@#######@@@@:.
                    ..-#@@@@#***################@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@=::::::::::::::::::::::::::::::::::::::::::::::::::-@@@@@@@@@@@@@@@@@@@@@@%######@@@@+..
                       .:+@@@@%#**################%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%-::::::::::::::::::::::::::::::::::::::::::::::::::::-@@@@@@@@@@@@@@@@@@@%######%@@@*:.
                         ..=#@@@@#**################%%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#:::::::::::::::::::::::::::::::::::::::::::::::::::::::=@@@@@@@@@@@@@@@@%######%@@@#-.
                            .:*%@@@%###################%@@@@@@@@@@@@@@@@@@@@@@@@@@*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::+@@@@@@@@@@@@@%#######@@@%=..
                               .-#@@@@%###################%@@@@@@@@@@@@@@@@@@@@@@*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::-=@@@@@@@@@@@%######%@@@%=..
                                ...-%@@@@@####################@@@@@@@@@@@@@@@@@@@@+::::::::::::::::::::::::::::::::::::::::::::::::::::::-#@@@@@@@@@@@%######%@@@%-..
                                   ...-#@@@@@%###################%@@@@@@@@@@@@@@@@@@@#+:::::::::::::::::::::::::::::::::::::::::::::::+%@@@@@@@@@@@%######%@@@@*:..
                                      ..:-*@@@@@@%%##################%@@@@@@@@@@@@@@@@@@%#+=:::::::::::::::::::::::::::::::::::::=*#%@@@@@@@@@@@%######%@@@@@#=..
                                          ..-+#@@@@@@%%##################%%@@@@@@@@@@@@@@@@@@%*+==-:::::::::::::::::::::::-=+*#%@@@@@@@@@@@@@%######%@@@@@*-:..
                                              ..=#%@@@@@@%%###################%@@@@@@@@@@@@@@@@@@@@@%#*+===------===+*#%@@@@@@@@@@@@@@@@@@%######%@@@@@%*-.
                                                  ..-*@@@@@@@@@%####################%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%#######%@@@@@@#-..
                                                    .....=#@@@@@@@@@@%######################%%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%%########%@@@@@@%+:...
                                                          ..::=*%@@@@@@@@@@%%#######################%%%%%@@@@@@@@@@@@%%%%############%@@@@@@@%=:..
                                                               ...-=+#%@@@@@@@@@@@@%%%##########################################%%@@@@@@%*=-:..
                                                                     ...:-+*#%@@@@@@@@@@@@@@%%%%%#########################%%%@@@@@@@%*=:..
                                                                             ...*@@%@@@@@@@@@@@@@@@@@@@@@@@%%%%%%####%%@@@@@@@@%#+:..
                                                                               .*@@****#####%%%@@@@@@@@@@@@@@@@@@@@@@@@@@@@#=:..
                                                                               .@@%****##########@#====%%###@##@%%%@@@%-:....
                                                                               -@@#****##########@#===+@#***@%=@%###@@%.
                                                                               =@@*****##########%@===#@#***%@=%@###%@@-
                                                                              .*@@*****###########@*==*%%%%%@@+@%###%@@+.
                                                                              .@@%****############%@+=====++==%@#####@@%.
                                                                              =@@#****#############@@========#@######@@@.
                                                                             .*@@*****############@@%@+=====*@#######@@@=.
                                                                             .%@@*****###########%@##@@#===#@########%@@*.
                                                                             -@@%****###########%@%##@%%%*#@##########@@%.
                                                                             =@@#****###########@%###@##%@@###########@@@-.
                                                                             +@@*****##########@@###%@##%@@###########@@@+.
                                                                            .%@@****##########@@####@%##%@@###########%@@%.
                                                                            -@@%****#########@@#####@###%@@############@@@:
                                                                            =@@#****########%@#####%@####@@############@@@=.
                                                                            #@@*****#######%@######@%####@@############%@@#.
                                                                           .%@@****#######%@######%@#####@@############%@@%.
                                                                           -@@%****######%@#######%@#####@@%############@@@-.
                                                                           =@@#****#####%@########@%#####@@%############@@@+.
                                                                           +@@#****####%@########%@######@@%############%@@#.
                                                                          .%@@****####%@#########@@######@@%############%@@@:
                                                                          :@@%****###%@##########@%######@@%#############@@@=.
                                                                          =@@%****##%%##########%@#######@@%#############@@@*.
                                                                          #@@#****#%%###########@@#######@@@#############%@@%.
                                                                         .%@@*****%############%@%#######@@@#############%@@@:
                                                                         -@@@****##############@@########%@@##############@@@+.
                                                                         -@@%****##############@%########%@@##############@@@#.
                                                                        .*@@#****#############@@#########%@@##############%@@@:
                                                                        .%@@*****#############@%#########%@@##############%@@@-.
                                                                        :@@@*****############%@##########%@@###############@@@*.
                                                                        -@@%*****############@%##########%@@###############@@@%.
                                                                        #@@#****############%@############@@###############%@@@:
                                                                       .%@@#****############@%############@@%###############@@@=.
                                                                       :@@@*****###########%@#############@@%###############@@@*.
                                                                       -@@%*****###########@%#############@@%###############@@@%.
                                                                       =@@%*****##########%@##############@@%###############%@@@:
                                                                      .#@@#*****##########@%##############@@%################@@@+.
                                                                      :@@@*****###########@###############@@%################@@@#.
*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...