Submission #270380

# Submission time Handle Problem Language Result Execution time Memory
270380 2020-08-17T14:33:31 Z Autoratch Factories (JOI14_factories) C++14
100 / 100
6846 ms 400024 KB
#include "factories.h"
#include <bits/stdc++.h>
using namespace std;
 
const int N = 5e5 + 1;
const int K = 19;
 
vector<pair<int,int> > adj[N];
int sz[N],lv[N],dp[K][N],pa[N],lcc[N][K];
long long dis[N];
vector<pair<long long,int> > diss[N];
bool blocked[N];
long long res[N][2],ans;
vector<int> rev;
 
void dfs(int u,int p)
{
    dp[0][u] = p,lv[u] = lv[p]+1;
    for(auto [d,v] : adj[u]) if(v!=p) dis[v] = dis[u]+d,dfs(v,u);
}
 
void getsz(int u,int p,int st,long long w)
{ 
    sz[u] = 1; 
    if(st!=-1) diss[u].push_back({w,st}); 
    for(auto [d,v] : adj[u]) if(v!=p and !blocked[v]) getsz(v,u,st,w+d),sz[u]+=sz[v]; 
}
 
void build(int u,int cp)
{
    getsz(u,cp,-1,0);
    int c = u,prev = 0;
    while(true)
    {
        int mx = -1,r = 0;
        for(auto [d,v] : adj[c]) if(v!=prev and !blocked[v] and sz[v]>mx) mx = sz[v],r = v;
        if(mx*2>sz[u]) prev = c,c = r;
        else break;
    }
    getsz(c,c,c,0);
    pa[c] = cp,blocked[c] = true;
    for(auto [d,v] : adj[c]) if(!blocked[v]) build(v,c);
}
  
void Init(int n,int a[],int b[],int d[]) 
{
    for(int i = 0;i < n-1;i++) a[i]++,b[i]++;
    for(int i = 0;i < n-1;i++)
    {
        adj[a[i]].push_back({d[i],b[i]});
        adj[b[i]].push_back({d[i],a[i]});
    }
    dfs(1,0);
    build(1,0);
    for(int i = 1;i < K;i++) for(int j = 1;j <= n;j++) dp[i][j] = dp[i-1][dp[i-1][j]];
    for(int i = 1;i <= n;i++) res[i][0] = res[i][1] = 1e18;
}
 
long long dist(int a,int b,int cn)
{
    int l = lcc[a][cn];
    return dis[a]+dis[b]-dis[l]-dis[l]; 
}
 
void upd(int t,int x)
{
    for(auto [d,u] : diss[x])
    {
        rev.push_back(u);
        res[u][t] = min(res[u][t],d);
    }
}
 
long long Query(int s,int x[],int t,int y[]) 
{
    for(int i = 0;i < s;i++) x[i]++;
    for(int i = 0;i < t;i++) y[i]++;
    ans = LLONG_MAX;
    for(int i = 0;i < s;i++) upd(0,x[i]);
    for(int i = 0;i < t;i++) upd(1,y[i]);
    for(int x : rev) ans = min(ans,res[x][0]+res[x][1]),res[x][0] = res[x][1] = 1e18;
    rev.clear();
    return ans;
}

Compilation message

factories.cpp: In function 'void dfs(int, int)':
factories.cpp:19:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   19 |     for(auto [d,v] : adj[u]) if(v!=p) dis[v] = dis[u]+d,dfs(v,u);
      |              ^
factories.cpp: In function 'void getsz(int, int, int, long long int)':
factories.cpp:26:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   26 |     for(auto [d,v] : adj[u]) if(v!=p and !blocked[v]) getsz(v,u,st,w+d),sz[u]+=sz[v];
      |              ^
factories.cpp: In function 'void build(int, int)':
factories.cpp:36:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   36 |         for(auto [d,v] : adj[c]) if(v!=prev and !blocked[v] and sz[v]>mx) mx = sz[v],r = v;
      |                  ^
factories.cpp:42:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   42 |     for(auto [d,v] : adj[c]) if(!blocked[v]) build(v,c);
      |              ^
factories.cpp: In function 'void upd(int, int)':
factories.cpp:67:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   67 |     for(auto [d,u] : diss[x])
      |              ^
# Verdict Execution time Memory Grader output
1 Correct 25 ms 24448 KB Output is correct
2 Correct 481 ms 33784 KB Output is correct
3 Correct 574 ms 34156 KB Output is correct
4 Correct 552 ms 34424 KB Output is correct
5 Correct 593 ms 34936 KB Output is correct
6 Correct 369 ms 33148 KB Output is correct
7 Correct 567 ms 34168 KB Output is correct
8 Correct 574 ms 34432 KB Output is correct
9 Correct 592 ms 34936 KB Output is correct
10 Correct 359 ms 33144 KB Output is correct
11 Correct 539 ms 34040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 24320 KB Output is correct
2 Correct 3601 ms 237972 KB Output is correct
3 Correct 4796 ms 309772 KB Output is correct
4 Correct 1318 ms 134876 KB Output is correct
5 Correct 6044 ms 400024 KB Output is correct
6 Correct 4926 ms 310060 KB Output is correct
7 Correct 1622 ms 77864 KB Output is correct
8 Correct 854 ms 55176 KB Output is correct
9 Correct 1804 ms 91932 KB Output is correct
10 Correct 1689 ms 78924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 24448 KB Output is correct
2 Correct 481 ms 33784 KB Output is correct
3 Correct 574 ms 34156 KB Output is correct
4 Correct 552 ms 34424 KB Output is correct
5 Correct 593 ms 34936 KB Output is correct
6 Correct 369 ms 33148 KB Output is correct
7 Correct 567 ms 34168 KB Output is correct
8 Correct 574 ms 34432 KB Output is correct
9 Correct 592 ms 34936 KB Output is correct
10 Correct 359 ms 33144 KB Output is correct
11 Correct 539 ms 34040 KB Output is correct
12 Correct 17 ms 24320 KB Output is correct
13 Correct 3601 ms 237972 KB Output is correct
14 Correct 4796 ms 309772 KB Output is correct
15 Correct 1318 ms 134876 KB Output is correct
16 Correct 6044 ms 400024 KB Output is correct
17 Correct 4926 ms 310060 KB Output is correct
18 Correct 1622 ms 77864 KB Output is correct
19 Correct 854 ms 55176 KB Output is correct
20 Correct 1804 ms 91932 KB Output is correct
21 Correct 1689 ms 78924 KB Output is correct
22 Correct 4057 ms 241332 KB Output is correct
23 Correct 4011 ms 245896 KB Output is correct
24 Correct 5549 ms 318412 KB Output is correct
25 Correct 5614 ms 320772 KB Output is correct
26 Correct 5394 ms 311960 KB Output is correct
27 Correct 6846 ms 399740 KB Output is correct
28 Correct 1677 ms 140652 KB Output is correct
29 Correct 5220 ms 310600 KB Output is correct
30 Correct 5267 ms 310012 KB Output is correct
31 Correct 5282 ms 310760 KB Output is correct
32 Correct 1770 ms 99508 KB Output is correct
33 Correct 810 ms 56520 KB Output is correct
34 Correct 1310 ms 70492 KB Output is correct
35 Correct 1315 ms 71348 KB Output is correct
36 Correct 1488 ms 76624 KB Output is correct
37 Correct 1596 ms 76604 KB Output is correct