Submission #57693

# Submission time Handle Problem Language Result Execution time Memory
57693 2018-07-15T20:17:15 Z Benq Factories (JOI14_factories) C++14
0 / 100
6948 ms 234820 KB
#include "factories.h"
 
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
 
using namespace std;
using namespace __gnu_pbds;
 
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
 
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
 
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
 
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
 
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
 
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
 
const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 500005;
 
 
template<int SZ> struct centroidDecomp {
    int N;
    bool done[SZ];
    int sub[SZ], par[SZ], cen[SZ];
    ll ans[SZ];
    vl dist[SZ];
    vpi adj[SZ];
    
    // INITIALIZE
    
    void addEdge(int a, int b, int d) { adj[a].pb({b,d}), adj[b].pb({a,d}); }
    
    void dfs (int no) {
        sub[no] = 1;
        for (pi i: adj[no]) if (!done[i.f] && i.f != par[no]) {
            par[i.f] = no;
            dfs(i.f);
            sub[no] += sub[i.f];
        }
    }
    
    void genDist(int par, int no, int t, ll dis) {
        dist[no].pb(dis);
        for (pi i: adj[no]) if (!done[i.f] && i.f != par) {
            cen[i.f] = t;
            genDist(no,i.f,t,dis+i.s);
        }
    }
    
    int getCentroid(int x) {
        par[x] = -1; dfs(x);
        int sz = sub[x];
        while (1) {
            pi mx = {0,0};
            for (pi i: adj[x]) if (!done[i.f] && i.f != par[x]) mx = max(mx,{sub[i.f],i.f});
            if (mx.f*2 > sz) x = mx.s;
            else return x;
        }
    }
    
    void solve (int x) {
        x = getCentroid(x); done[x] = 1;
        genDist(0,x,x,0);
        for (pi i: adj[x]) if (!done[i.f]) solve(i.f);
    }
    
    void init() {
    	F0R(i,N) cen[i] = -1, ans[i] = INF;
    	solve(0);
    }
    
    // QUERY 
    
    void upd(int v, int x = 1) {
        for (int V = v, ind = sz(dist[v])-1; V >= 0; V = cen[V], ind --) {
            // cout << "OH " << v << " " << V << " " << x << "\n";
        	if (x == 1) ans[V] = min(ans[V],dist[v][ind]);
        	else ans[V] = INF;
        }
    }
    
    ll query(int v) {
        ll ret = INF;
        for (int V = v, ind = sz(dist[v])-1; V >= 0; V = cen[V], ind --) {
            // cout << "AH " << V << " " << v << "\n";
            ret = min(ret,ans[V]+dist[v][ind]);
        }
        return ret;
    }
};
 
centroidDecomp<MX> C;
 
void Init(int N, int A[], int B[], int D[]) {
	C.N = N;
	F0R(i,N-1) C.addEdge(A[i],B[i],D[i]);
	C.init();
}
 
long long Query(int S, int X[], int T, int Y[]) {
	ll ans = INF;
	F0R(i,S) C.upd(X[i]);
	F0R(i,T) ans = min(ans,C.query(Y[i]));
	F0R(i,S) C.upd(X[i],-1);
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 32 ms 24184 KB Output is correct
2 Correct 468 ms 32744 KB Output is correct
3 Incorrect 489 ms 39488 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 27 ms 39488 KB Output is correct
2 Correct 4175 ms 138932 KB Output is correct
3 Correct 5158 ms 173700 KB Output is correct
4 Correct 1376 ms 173700 KB Output is correct
5 Correct 6948 ms 234820 KB Output is correct
6 Correct 5802 ms 234820 KB Output is correct
7 Correct 1560 ms 234820 KB Output is correct
8 Incorrect 653 ms 234820 KB Output isn't correct
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 32 ms 24184 KB Output is correct
2 Correct 468 ms 32744 KB Output is correct
3 Incorrect 489 ms 39488 KB Output isn't correct
4 Halted 0 ms 0 KB -