Submission #1095209

# Submission time Handle Problem Language Result Execution time Memory
1095209 2024-10-01T15:01:04 Z 0pt1mus23 Janjetina (COCI21_janjetina) C++14
110 / 110
379 ms 20096 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define endl '\n'
#define ins insert
#define pb push_back
#define putr(x) cout<<x<<endl;return;
#define all(x) x.begin(),x.end()
#define _ << " " <<  
const int   mod = 1e9+7 ,
            sze= 1e5 +23 ,
            inf = INT_MAX ,
            L = 31 ;

int arr[sze];
int ans=0;
int T[sze +23];
int k;
vector<pair<int,int>> graph[sze];

void upd(int node,int x){
    for(++node;node<=sze;node += (node & -node)){
        T[node]+=x;
    }
}
int qry(int node){
    int sum=0;
    for(++node;node>0;node -= (node & -node)){
        sum+=T[node];
    }
    return sum;
}


int cmp(pair<int,int >x, pair<int,int> y){
    if(x.second != y.second){
        return x.second < y.second;
    }
    return x.first < y.first;
}
vector<pair<int,int>> event;
int depth[sze];
int sub[sze];
int sil[sze];
int curr=0;
void dfs1(int node,int par){
    curr++;
    sub[node]=1;
    for(auto [v,w]:graph[node]){
        if(v!=par && !sil[v]){
            depth[v]=depth[node]+1;
            dfs1(v,node);
            sub[node]+=sub[v];
        }
    }
}
int dfs2(int node,int par){
    for(auto [v,w]:graph[node]){
        if(v!=par && !sil[v]){
            if(sub[v]*2>curr){
                return dfs2(v,node);
            }
        }
    }
    return node;
}
void dfs3(int node,int par,int mx){
    event.pb({node,mx});
    for(auto [v,w]:graph[node]){
        if(v!=par && !sil[v]){
            dfs3(v,node,max(mx,w));
        }
    }
}

void decomp(int node){
    curr=0;
    depth[node]=0;
    dfs1(node,-1);
    int centroid = dfs2(node,-1);
    
    curr=0;
    depth[centroid]=0;
    dfs1(centroid,-1);
    dfs3(centroid,-1,0);

    sort(all(event),cmp);
    for(auto [v,w]:event){
        ans+=qry(w - depth[v] - k);
        upd(depth[v],1);
    }
    for(auto [v,w]:event){
        upd(depth[v],-1);
    }
    event.clear();
    for(auto [v,w]:graph[centroid]){
        if(!sil[v]){
            dfs3(v,centroid,w);
            sort(all(event),cmp);
            for(auto ev:event){
                ans-=qry(ev.second - depth[ev.first] -k);
                upd(depth[ev.first],1);
            }
            for(auto ev:event){
                upd(depth[ev.first],-1);
            }
            event.clear();
        }
    }
    sil[centroid]=1;
    for(auto [v,w]:graph[centroid]){
        if(!sil[v]){
            decomp(v);
        }
    }
}

void opt1z(){
    int n;
    cin>>n>>k;
    for(int i=1;i<n;i++){
        int u,v,w;
        cin>>u>>v>>w;
        graph[u].pb({v,w});
        graph[v].pb({u,w});
    }
    decomp(1);
    cout<<ans*2LL<<endl;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
 
    int tt = 1;
    // cin>>tt;
    while(tt--){
        opt1z();
    }
 
    return 0;
}

Compilation message

Main.cpp: In function 'void dfs1(long long int, long long int)':
Main.cpp:50:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   50 |     for(auto [v,w]:graph[node]){
      |              ^
Main.cpp: In function 'long long int dfs2(long long int, long long int)':
Main.cpp:59:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   59 |     for(auto [v,w]:graph[node]){
      |              ^
Main.cpp: In function 'void dfs3(long long int, long long int, long long int)':
Main.cpp:70:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   70 |     for(auto [v,w]:graph[node]){
      |              ^
Main.cpp: In function 'void decomp(long long int)':
Main.cpp:89:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   89 |     for(auto [v,w]:event){
      |              ^
Main.cpp:93:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   93 |     for(auto [v,w]:event){
      |              ^
Main.cpp:97:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   97 |     for(auto [v,w]:graph[centroid]){
      |              ^
Main.cpp:112:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  112 |     for(auto [v,w]:graph[centroid]){
      |              ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2908 KB Output is correct
2 Correct 2 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 3 ms 2816 KB Output is correct
5 Correct 2 ms 2908 KB Output is correct
6 Correct 2 ms 2904 KB Output is correct
7 Correct 3 ms 2908 KB Output is correct
8 Correct 3 ms 2904 KB Output is correct
9 Correct 2 ms 2916 KB Output is correct
10 Correct 2 ms 2904 KB Output is correct
11 Correct 2 ms 2908 KB Output is correct
12 Correct 2 ms 2908 KB Output is correct
13 Correct 2 ms 2908 KB Output is correct
14 Correct 3 ms 2908 KB Output is correct
15 Correct 2 ms 2904 KB Output is correct
16 Correct 2 ms 2908 KB Output is correct
17 Correct 2 ms 2908 KB Output is correct
18 Correct 3 ms 2904 KB Output is correct
19 Correct 3 ms 2904 KB Output is correct
20 Correct 2 ms 2904 KB Output is correct
21 Correct 3 ms 2908 KB Output is correct
22 Correct 2 ms 2908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 3 ms 2908 KB Output is correct
5 Correct 23 ms 4620 KB Output is correct
6 Correct 159 ms 11460 KB Output is correct
7 Correct 258 ms 19552 KB Output is correct
8 Correct 306 ms 19904 KB Output is correct
9 Correct 316 ms 19644 KB Output is correct
10 Correct 352 ms 19896 KB Output is correct
11 Correct 260 ms 19392 KB Output is correct
12 Correct 295 ms 20036 KB Output is correct
13 Correct 321 ms 19484 KB Output is correct
14 Correct 371 ms 19904 KB Output is correct
15 Correct 373 ms 19608 KB Output is correct
16 Correct 354 ms 19916 KB Output is correct
17 Correct 366 ms 20096 KB Output is correct
18 Correct 351 ms 19880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2908 KB Output is correct
2 Correct 2 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 3 ms 2816 KB Output is correct
5 Correct 2 ms 2908 KB Output is correct
6 Correct 2 ms 2904 KB Output is correct
7 Correct 3 ms 2908 KB Output is correct
8 Correct 3 ms 2904 KB Output is correct
9 Correct 2 ms 2916 KB Output is correct
10 Correct 2 ms 2904 KB Output is correct
11 Correct 2 ms 2908 KB Output is correct
12 Correct 2 ms 2908 KB Output is correct
13 Correct 2 ms 2908 KB Output is correct
14 Correct 3 ms 2908 KB Output is correct
15 Correct 2 ms 2904 KB Output is correct
16 Correct 2 ms 2908 KB Output is correct
17 Correct 2 ms 2908 KB Output is correct
18 Correct 3 ms 2904 KB Output is correct
19 Correct 3 ms 2904 KB Output is correct
20 Correct 2 ms 2904 KB Output is correct
21 Correct 3 ms 2908 KB Output is correct
22 Correct 2 ms 2908 KB Output is correct
23 Correct 1 ms 2652 KB Output is correct
24 Correct 1 ms 2652 KB Output is correct
25 Correct 1 ms 2652 KB Output is correct
26 Correct 3 ms 2908 KB Output is correct
27 Correct 23 ms 4620 KB Output is correct
28 Correct 159 ms 11460 KB Output is correct
29 Correct 258 ms 19552 KB Output is correct
30 Correct 306 ms 19904 KB Output is correct
31 Correct 316 ms 19644 KB Output is correct
32 Correct 352 ms 19896 KB Output is correct
33 Correct 260 ms 19392 KB Output is correct
34 Correct 295 ms 20036 KB Output is correct
35 Correct 321 ms 19484 KB Output is correct
36 Correct 371 ms 19904 KB Output is correct
37 Correct 373 ms 19608 KB Output is correct
38 Correct 354 ms 19916 KB Output is correct
39 Correct 366 ms 20096 KB Output is correct
40 Correct 351 ms 19880 KB Output is correct
41 Correct 2 ms 2652 KB Output is correct
42 Correct 259 ms 19320 KB Output is correct
43 Correct 303 ms 19900 KB Output is correct
44 Correct 318 ms 19700 KB Output is correct
45 Correct 362 ms 19900 KB Output is correct
46 Correct 262 ms 19344 KB Output is correct
47 Correct 302 ms 19928 KB Output is correct
48 Correct 301 ms 19400 KB Output is correct
49 Correct 379 ms 19900 KB Output is correct
50 Correct 349 ms 19644 KB Output is correct
51 Correct 348 ms 19908 KB Output is correct
52 Correct 120 ms 13460 KB Output is correct
53 Correct 140 ms 13760 KB Output is correct
54 Correct 115 ms 13500 KB Output is correct
55 Correct 133 ms 13920 KB Output is correct
56 Correct 130 ms 13744 KB Output is correct
57 Correct 299 ms 13436 KB Output is correct
58 Correct 305 ms 13744 KB Output is correct
59 Correct 327 ms 13760 KB Output is correct
60 Correct 329 ms 13768 KB Output is correct
61 Correct 318 ms 13496 KB Output is correct
62 Correct 240 ms 13252 KB Output is correct
63 Correct 274 ms 13704 KB Output is correct
64 Correct 280 ms 13504 KB Output is correct
65 Correct 10 ms 3420 KB Output is correct
66 Correct 1 ms 2652 KB Output is correct