Submission #471733

# Submission time Handle Problem Language Result Execution time Memory
471733 2021-09-10T14:55:28 Z NicolaAbusaad2014 Race (IOI11_race) C++14
100 / 100
2128 ms 85692 KB
/**
 * Prof.Nicola
**/
#include "race.h"
#include <bits/stdc++.h>

using namespace std;
#define endl "\n"
#define mp make_pair
#define F first
#define S second
#define REP(i,l,r) for(long long i=(l);i<(r);i++)
#define PER(i,l,r) for(long long i=(r)-1;i>=(l);i--)
const long dx[4]={1,0,-1,0},dz[4]={0,1,0,-1};
const long double pi=3.14159265359;
const long long mod=1e9+7;
long long p(long long x){while(x&(x-1)){x=x&(x-1);}return x;}
long long squared(long long x){return (x*x)%mod;}
long long power(long long x,long long p){if(p==0){return 1;}if(p%2==1){return (power(x,p-1)*x)%mod;}return squared(power(x,p/2));}
long long inv(long long x){return power(x,mod-2);}
template<class T>void re(T&x){cin>>x;}
template<class T1,class T2> void re(pair<T1,T2>&x){re(x.first);re(x.second);}
template<class T>void re(vector<T>&x){for(long i=0;i<x.size();i++){re(x[i]);}}
template<class T>void re(deque<T>&x){for(long i=0;i<x.size();i++){re(x[i]);}}
template<class T>void out(T x){cout<<x<<" ";}
template<class T1,class T2>void out(pair<T1,T2>x){out(x.first);out(x.second);cout<<endl;}
template<class T>void out(vector<T>x,long l=0,long r=0){if(!r){r=x.size();}for(long i=l;i<r;i++){out(x[i]);}cout<<endl;}
template<class T>void out(deque<T>x,long l=0,long r=0){if(!r){r=x.size();}for(long i=l;i<r;i++){out(x[i]);}cout<<endl;}
template<class T>void out(set<T>x){while(!x.empty()){out(*x.begin());x.erase(*x.begin());}cout<<endl;}
template<class T1,class T2>void out(map<T1,T2>x){while(!x.empty()){out(*x.begin());x.erase(x.begin()->first);}cout<<endl;}
template<class T>void out(queue<T>x){while(!x.empty()){out(x.front());x.pop();}cout<<endl;}
template<class T>void out(priority_queue<T>x){while(!x.empty()){out(x.top());x.pop();}cout<<endl;}
template<class T>void out(stack<T>x){while(!x.empty()){out(x.top());x.pop();}cout<<endl;}
template<class T>T cross(complex<T>x,complex<T>z){return (conj(x)*z).imag();}
template<class T>T dot(complex<T>x,complex<T>z){return (conj(x)*z).real();}
set<long long>::iterator T;
long long vLE(long long x,vector<long long>&VT,long l=0,long r=-1){if(r==-1){r=VT.size()-1;}if(VT[l]>x){return -1;}long long z=p(r-l);while(z){if(l+z<=r&&VT[l+z]<=x){l+=z;}z/=2;}return l;}
long long vL(long long x,vector<long long>&VT,long l=0,long r=-1){if(r==-1){r=VT.size()-1;}if(VT[l]>=x){return -1;}long long z=p(r-l);while(z){if(l+z<=r&&VT[l+z]<x){l+=z;}z/=2;}return l;}
long long vGE(long long x,vector<long long>&VT,long l=0,long r=-1){if(r==-1){r=VT.size()-1;}if(VT[l]>=x){return l;}l=vL(x,VT,l,r);if(l==r){return -1;}return l+1;}
long long vG(long long x,vector<long long>&VT,long l=0,long r=-1){if(r==-1){r=VT.size()-1;}if(VT[l]>x){return l;}l=vLE(x,VT,l,r);if(l==r){return -1;}return l+1;}
long long sLE(long long x,set<long long>&ST){if(ST.count(x)){return x;}if(*ST.begin()>x){return -1;}ST.insert(x);T=ST.find(x);T--;ST.erase(x);return *T;}
long long sL(long long x,set<long long>&ST){if(*ST.begin()>=x){return -1;}if(ST.count(x)){T=ST.find(x);T--;return *T;}ST.insert(x);T=ST.find(x);T--;ST.erase(x);return *T;}
long long sGE(long long x,set<long long>&ST){if(ST.count(x)){return x;}if(*ST.rbegin()<x){return -1;}ST.insert(x);T=ST.find(x);T++;ST.erase(x);return *T;}
long long sG(long long x,set<long long>&ST){if(*ST.rbegin()<=x){return -1;}if(ST.count(x)){T=ST.find(x);T++;return *T;}ST.insert(x);T=ST.find(x);T++;ST.erase(x);return *T;}
long k,ans=1e9;
vector<long long>len;
vector<pair<pair<long,long>,pair<long,long> > >mn;
struct tree
{
    long long n;
    vector<vector<pair<long,long> > >edges;
    vector<long>depth,sz;
    vector<bool>vis;
    void scan0(long x,long p,long long l)
    {
        len[x]=l;
        depth[x]=depth[p]+1;
        sz[x]=1;
        REP(i,0,edges[x].size()){
            if(edges[x][i].F==p||vis[edges[x][i].F]){
                continue;
            }
            scan0(edges[x][i].F,x,l+edges[x][i].S);
            sz[x]+=sz[edges[x][i].F];
        }
    }
    void scan(long x,long p,long long l,long h)
    {
        if(l>k){
            return;
        }
        if(depth[x]<=mn[l].F.F){
            if(mn[l].F.S==h){
                mn[l].F.F=depth[x];
            }
            else{
                mn[l].S=mn[l].F;
                mn[l].F=mp(depth[x],h);
            }
        }
        else if(depth[x]<mn[l].S.F){
            if(mn[l].F.S!=h){
                mn[l].S=mp(depth[x],h);
            }
        }
        REP(i,0,edges[x].size()){
            if(edges[x][i].F==p||vis[edges[x][i].F]){
                continue;
            }
            scan(edges[x][i].F,x,l+edges[x][i].S,h);
        }
    }
    void scan2(long x,long p,long long l,long h)
    {
        if(l>k){
            return;
        }
        if(h==mn[k-l].F.S&&mn[k-l].S.S!=0){
            ans=min(ans,depth[x]+mn[k-l].S.F);
        }
        else if(h!=mn[k-l].F.S&&mn[k-l].F.S!=0){
            ans=min(ans,depth[x]+mn[k-l].F.F);
        }
        REP(i,0,edges[x].size()){
            if(edges[x][i].F==p||vis[edges[x][i].F]){
                continue;
            }
            scan2(edges[x][i].F,x,l+edges[x][i].S,h);
        }
    }
    void scan3(long x,long p,long long l,long h)
    {
        if(l>k){
            return;
        }
        mn[l]=mp(mp(1e9,0),mp(1e9,0));
        if(l==0){
            mn[l]=mp(mp(0,1),mp(0,2));
        }
        REP(i,0,edges[x].size()){
            if(edges[x][i].F==p||vis[edges[x][i].F]){
                continue;
            }
            scan3(edges[x][i].F,x,l+edges[x][i].S,h);
        }
    }
    long centroid(long x,long p,long z)
    {
        long mx=0,node=x;
        REP(i,0,edges[x].size()){
            if(!vis[edges[x][i].F]&&edges[x][i].F!=p&&sz[edges[x][i].F]>mx){
                mx=sz[edges[x][i].F];
                node=edges[x][i].F;
            }
        }
        if(mx<z){
            return x;
        }
        return centroid(node,x,z);
    }
    void read(long nodes,vector<vector<pair<long,long> > >e)
    {
        n=nodes;
        edges=e;
    }
    void build(long long root=1)
    {
        vis.clear();
        vis.resize(n+1);
        depth.clear();
        depth.resize(n+1);
        depth[0]=0;
        sz.clear();
        sz.resize(n+1);
        scan0(root,0,0);
    }
};
tree t;
void solve(long x)
{
    t.scan0(x,0,0);
    x=t.centroid(x,0,(t.sz[x]+1)/2);
    t.vis[x]=true;
    REP(i,0,t.edges[x].size()){
        if(t.vis[t.edges[x][i].F]){
            continue;
        }
        t.scan0(t.edges[x][i].F,0,t.edges[x][i].S);
        t.scan(t.edges[x][i].F,0,t.edges[x][i].S,t.edges[x][i].F);
    }
    REP(i,0,t.edges[x].size()){
        if(t.vis[t.edges[x][i].F]){
            continue;
        }
        t.scan2(t.edges[x][i].F,0,t.edges[x][i].S,t.edges[x][i].F);
    }
    REP(i,0,t.edges[x].size()){
        if(t.vis[t.edges[x][i].F]){
            continue;
        }
        t.scan3(t.edges[x][i].F,0,t.edges[x][i].S,t.edges[x][i].F);
    }
    REP(i,0,t.edges[x].size()){
        if(t.vis[t.edges[x][i].F]){
            continue;
        }
        solve(t.edges[x][i].F);
    }
}
int best_path(int N, int K, int H[][2], int L[])
{
    mn.resize(K+1);
    mn[0]=mp(mp(0,1),mp(0,2));
    REP(i,1,K){
        mn[i]=mp(mp(1e9,0),mp(1e9,0));
    }
    k=K;
    len.resize(N+1);
    vector<vector<pair<long,long> > >e(N+1);
    REP(i,0,N-1){
        e[H[i][0]+1].push_back(mp(H[i][1]+1,L[i]));
        e[H[i][1]+1].push_back(mp(H[i][0]+1,L[i]));
    }
    t.read(N,e);
    t.build();
    solve(1);
    if(ans>=999999998){
        return -1;
    }
    return ans;
}

Compilation message

race.cpp: In member function 'void tree::scan0(long int, long int, long long int)':
race.cpp:12:41: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long int, long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   12 | #define REP(i,l,r) for(long long i=(l);i<(r);i++)
      |                                         ^
race.cpp:59:9: note: in expansion of macro 'REP'
   59 |         REP(i,0,edges[x].size()){
      |         ^~~
race.cpp: In member function 'void tree::scan(long int, long int, long long int, long int)':
race.cpp:12:41: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long int, long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   12 | #define REP(i,l,r) for(long long i=(l);i<(r);i++)
      |                                         ^
race.cpp:86:9: note: in expansion of macro 'REP'
   86 |         REP(i,0,edges[x].size()){
      |         ^~~
race.cpp: In member function 'void tree::scan2(long int, long int, long long int, long int)':
race.cpp:12:41: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long int, long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   12 | #define REP(i,l,r) for(long long i=(l);i<(r);i++)
      |                                         ^
race.cpp:104:9: note: in expansion of macro 'REP'
  104 |         REP(i,0,edges[x].size()){
      |         ^~~
race.cpp: In member function 'void tree::scan3(long int, long int, long long int, long int)':
race.cpp:12:41: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long int, long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   12 | #define REP(i,l,r) for(long long i=(l);i<(r);i++)
      |                                         ^
race.cpp:120:9: note: in expansion of macro 'REP'
  120 |         REP(i,0,edges[x].size()){
      |         ^~~
race.cpp: In member function 'long int tree::centroid(long int, long int, long int)':
race.cpp:12:41: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long int, long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   12 | #define REP(i,l,r) for(long long i=(l);i<(r);i++)
      |                                         ^
race.cpp:130:9: note: in expansion of macro 'REP'
  130 |         REP(i,0,edges[x].size()){
      |         ^~~
race.cpp: In function 'void solve(long int)':
race.cpp:12:41: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long int, long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   12 | #define REP(i,l,r) for(long long i=(l);i<(r);i++)
      |                                         ^
race.cpp:164:5: note: in expansion of macro 'REP'
  164 |     REP(i,0,t.edges[x].size()){
      |     ^~~
race.cpp:12:41: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long int, long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   12 | #define REP(i,l,r) for(long long i=(l);i<(r);i++)
      |                                         ^
race.cpp:171:5: note: in expansion of macro 'REP'
  171 |     REP(i,0,t.edges[x].size()){
      |     ^~~
race.cpp:12:41: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long int, long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   12 | #define REP(i,l,r) for(long long i=(l);i<(r);i++)
      |                                         ^
race.cpp:177:5: note: in expansion of macro 'REP'
  177 |     REP(i,0,t.edges[x].size()){
      |     ^~~
race.cpp:12:41: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long int, long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   12 | #define REP(i,l,r) for(long long i=(l);i<(r);i++)
      |                                         ^
race.cpp:183:5: note: in expansion of macro 'REP'
  183 |     REP(i,0,t.edges[x].size()){
      |     ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 208 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 216 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 208 KB Output is correct
18 Correct 1 ms 208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 208 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 216 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 208 KB Output is correct
18 Correct 1 ms 208 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 3 ms 460 KB Output is correct
22 Correct 22 ms 29280 KB Output is correct
23 Correct 18 ms 24140 KB Output is correct
24 Correct 20 ms 27536 KB Output is correct
25 Correct 20 ms 26828 KB Output is correct
26 Correct 11 ms 11084 KB Output is correct
27 Correct 20 ms 25548 KB Output is correct
28 Correct 6 ms 6756 KB Output is correct
29 Correct 10 ms 10632 KB Output is correct
30 Correct 11 ms 12044 KB Output is correct
31 Correct 16 ms 20940 KB Output is correct
32 Correct 18 ms 22684 KB Output is correct
33 Correct 20 ms 25036 KB Output is correct
34 Correct 16 ms 18944 KB Output is correct
35 Correct 20 ms 25944 KB Output is correct
36 Correct 23 ms 29724 KB Output is correct
37 Correct 19 ms 25420 KB Output is correct
38 Correct 14 ms 16396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 208 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 216 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 208 KB Output is correct
18 Correct 1 ms 208 KB Output is correct
19 Correct 398 ms 25032 KB Output is correct
20 Correct 356 ms 25028 KB Output is correct
21 Correct 340 ms 25136 KB Output is correct
22 Correct 311 ms 25160 KB Output is correct
23 Correct 291 ms 25576 KB Output is correct
24 Correct 179 ms 24816 KB Output is correct
25 Correct 453 ms 25072 KB Output is correct
26 Correct 165 ms 27480 KB Output is correct
27 Correct 298 ms 50060 KB Output is correct
28 Correct 1177 ms 55012 KB Output is correct
29 Correct 1056 ms 54276 KB Output is correct
30 Correct 277 ms 50108 KB Output is correct
31 Correct 295 ms 50004 KB Output is correct
32 Correct 511 ms 50140 KB Output is correct
33 Correct 854 ms 49140 KB Output is correct
34 Correct 903 ms 49824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 208 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 216 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 208 KB Output is correct
18 Correct 1 ms 208 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 3 ms 460 KB Output is correct
22 Correct 22 ms 29280 KB Output is correct
23 Correct 18 ms 24140 KB Output is correct
24 Correct 20 ms 27536 KB Output is correct
25 Correct 20 ms 26828 KB Output is correct
26 Correct 11 ms 11084 KB Output is correct
27 Correct 20 ms 25548 KB Output is correct
28 Correct 6 ms 6756 KB Output is correct
29 Correct 10 ms 10632 KB Output is correct
30 Correct 11 ms 12044 KB Output is correct
31 Correct 16 ms 20940 KB Output is correct
32 Correct 18 ms 22684 KB Output is correct
33 Correct 20 ms 25036 KB Output is correct
34 Correct 16 ms 18944 KB Output is correct
35 Correct 20 ms 25944 KB Output is correct
36 Correct 23 ms 29724 KB Output is correct
37 Correct 19 ms 25420 KB Output is correct
38 Correct 14 ms 16396 KB Output is correct
39 Correct 398 ms 25032 KB Output is correct
40 Correct 356 ms 25028 KB Output is correct
41 Correct 340 ms 25136 KB Output is correct
42 Correct 311 ms 25160 KB Output is correct
43 Correct 291 ms 25576 KB Output is correct
44 Correct 179 ms 24816 KB Output is correct
45 Correct 453 ms 25072 KB Output is correct
46 Correct 165 ms 27480 KB Output is correct
47 Correct 298 ms 50060 KB Output is correct
48 Correct 1177 ms 55012 KB Output is correct
49 Correct 1056 ms 54276 KB Output is correct
50 Correct 277 ms 50108 KB Output is correct
51 Correct 295 ms 50004 KB Output is correct
52 Correct 511 ms 50140 KB Output is correct
53 Correct 854 ms 49140 KB Output is correct
54 Correct 903 ms 49824 KB Output is correct
55 Correct 17 ms 2892 KB Output is correct
56 Correct 19 ms 2784 KB Output is correct
57 Correct 183 ms 25448 KB Output is correct
58 Correct 67 ms 24412 KB Output is correct
59 Correct 191 ms 33300 KB Output is correct
60 Correct 2128 ms 85692 KB Output is correct
61 Correct 324 ms 50420 KB Output is correct
62 Correct 355 ms 81452 KB Output is correct
63 Correct 623 ms 81448 KB Output is correct
64 Correct 1337 ms 66788 KB Output is correct
65 Correct 600 ms 51140 KB Output is correct
66 Correct 1391 ms 84076 KB Output is correct
67 Correct 351 ms 81168 KB Output is correct
68 Correct 579 ms 81268 KB Output is correct
69 Correct 610 ms 81444 KB Output is correct
70 Correct 577 ms 79000 KB Output is correct