Submission #71956

# Submission time Handle Problem Language Result Execution time Memory
71956 2018-08-26T03:33:25 Z anishmahto Factories (JOI14_factories) C++14
100 / 100
7217 ms 412144 KB
#include <iostream>
#include <cstdio>
#include <string.h>
#include <vector>
#include <limits>
#include <algorithm>
#include <stdlib.h>
#include <math.h>
#include <deque>
#define SET(x,y) x |= (1 << y)
#define CLEAR(x,y) x &= ~(1<< y)
#define READ(x,y) ((0u == (x & (1<<y)))?0u:1u)
#define TOGGLE(x,y) (x ^= (1<<y))
#define watch(x) cout << (#x) << " is " << (x) << endl
#define pb push_back
#define mp make_pair
#define si(n) scanf("%d",&n)
#define sf(n) scanf("%f",&n)
#define sl(n) scanf("%lld",&n)
#define slu(n) scanf("%llu",&n)
#define sd(n) scanf("%lf",&n)
#define ss(n) scanf("%s",n)
#define pnl printf("\n")
#define ll long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FORR(i,n) for(int i=(n);i>=0;i--)
#define CL(a,b) memset(a,b,sizeof(a))
#define sqr(x) ((x)*(x))
#define intLIMIT numeric_limits<int>
#define longLIMIT numeric_limits<long long>
#define dbl(x) (double)(x)
#define vi vector <int>
#define vii vector < pair <int, int> > 
#define vll vector <long long>
#define fastCin cin.sync_with_stdio(0);cin.tie(0)
#define scanUnsigned(x) do{while((x=getchar())<'0'); for(x-='0'; '0'<=(_=getchar()); x=(x<<3)+(x<<1)+_-'0');}while(0)
char _;
using namespace std;
void PRINT (int x, int y) { for (int i = y-1; i >= 0; i--) { cout << READ(x,i); } cout << endl; }

vector < vector < pll > > graph(500001);
const int LG = (int)log2(500001)+2;
long long sz[500001] = {0}, depth[500001], cTreeDist[500001][LG], dp[500001], path[500001][LG];
bool v[500001] = {0};

ll getSz (int pos, int par) {
    sz[pos] = 1;
    REP (x, graph[pos].size()) {
        if (v[graph[pos][x].first] || graph[pos][x].first == par) {
            continue;
        }
        sz[pos] += getSz(graph[pos][x].first, pos);
    }
    return sz[pos];
}

ll findCentroid (int pos, int par, int tot) {
    REP (x, graph[pos].size()) {
        if (v[graph[pos][x].first] || graph[pos][x].first == par) {
            continue;
        }
        if (sz[graph[pos][x].first] > tot/2) {
            return findCentroid (graph[pos][x].first, pos, tot);
        }
    }
    return pos;
}

void buildDist (int pos, int par, ll c, ll lvl, int centroid) {
    cTreeDist[pos][lvl] = c;
    path[pos][lvl] = centroid;
    depth[pos] = lvl;
    REP (x, graph[pos].size()) {
        if (v[graph[pos][x].first] || graph[pos][x].first == par) {
            continue;
        }
        buildDist (graph[pos][x].first, pos, c + graph[pos][x].second, lvl, centroid);
    }
}

void build (int pos, int par, int lvl) {
    int tot = getSz (pos, -1);
    int centroid = findCentroid (pos, -1, tot);
    v[centroid] = true;
    buildDist (centroid, -1, (ll)0, lvl, centroid);
    REP (x, graph[centroid].size()) {
        if (v[graph[centroid][x].first]) {
            continue;
        }
        build(graph[centroid][x].first, centroid, lvl+1);
    }
}

void Init(int N, int A[], int B[], int D[]) {
    CL (dp, -1);
    REP (x, N-1) {
        graph[A[x]].pb(mp(B[x], D[x]));
        graph[B[x]].pb(mp(A[x], D[x]));
    }
    build (0, -1, 0);
}

long long Query(int S, int X[], int T, int Y[]) {
    ll ans = longLIMIT::max();
    vi idx;
    REP (x, S) {
        dp[X[x]] = 0;
        idx.pb (X[x]);
        int pos = depth[X[x]];
        while (pos >= 0) {
            if (dp[path[X[x]][pos]] == -1) {
                dp[path[X[x]][pos]] = cTreeDist[X[x]][pos];
                idx.pb (path[X[x]][pos]);
            } else {
                dp[path[X[x]][pos]] = min (dp[path[X[x]][pos]], cTreeDist[X[x]][pos]);
            }
            pos--;
        }
    }
    REP (x, T) {
        if (dp[Y[x]] != -1) {
            ans = min (ans, dp[Y[x]]);
        }
        int pos = depth[Y[x]];
        while (pos >= 0) {
            if (dp[path[Y[x]][pos]] != -1) {
                ans = min (ans, cTreeDist[Y[x]][pos] + dp[path[Y[x]][pos]]);
            }
            pos--;
        }
    }
    while (!idx.empty()) {
        dp[idx.back()] = -1;
        idx.pop_back();
    }
    return ans;
}

Compilation message

factories.cpp: In function 'long long int getSz(int, int)':
factories.cpp:27:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define REP(i,n) for(int i=0;i<(n);i++)
                               ^
factories.cpp:51:5: note: in expansion of macro 'REP'
     REP (x, graph[pos].size()) {
     ^~~
factories.cpp: In function 'long long int findCentroid(int, int, int)':
factories.cpp:27:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define REP(i,n) for(int i=0;i<(n);i++)
                               ^
factories.cpp:61:5: note: in expansion of macro 'REP'
     REP (x, graph[pos].size()) {
     ^~~
factories.cpp: In function 'void buildDist(int, int, long long int, long long int, int)':
factories.cpp:27:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define REP(i,n) for(int i=0;i<(n);i++)
                               ^
factories.cpp:76:5: note: in expansion of macro 'REP'
     REP (x, graph[pos].size()) {
     ^~~
factories.cpp: In function 'void build(int, int, int)':
factories.cpp:27:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define REP(i,n) for(int i=0;i<(n);i++)
                               ^
factories.cpp:89:5: note: in expansion of macro 'REP'
     REP (x, graph[centroid].size()) {
     ^~~
# Verdict Execution time Memory Grader output
1 Correct 27 ms 16632 KB Output is correct
2 Correct 478 ms 35768 KB Output is correct
3 Correct 549 ms 42864 KB Output is correct
4 Correct 418 ms 43052 KB Output is correct
5 Correct 497 ms 43324 KB Output is correct
6 Correct 386 ms 43324 KB Output is correct
7 Correct 465 ms 43324 KB Output is correct
8 Correct 456 ms 43324 KB Output is correct
9 Correct 504 ms 43328 KB Output is correct
10 Correct 337 ms 43328 KB Output is correct
11 Correct 494 ms 43328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 43328 KB Output is correct
2 Correct 3168 ms 236400 KB Output is correct
3 Correct 5082 ms 238516 KB Output is correct
4 Correct 1473 ms 238516 KB Output is correct
5 Correct 6611 ms 257268 KB Output is correct
6 Correct 5295 ms 257268 KB Output is correct
7 Correct 1654 ms 257268 KB Output is correct
8 Correct 1006 ms 257268 KB Output is correct
9 Correct 2076 ms 257268 KB Output is correct
10 Correct 1628 ms 257268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 16632 KB Output is correct
2 Correct 478 ms 35768 KB Output is correct
3 Correct 549 ms 42864 KB Output is correct
4 Correct 418 ms 43052 KB Output is correct
5 Correct 497 ms 43324 KB Output is correct
6 Correct 386 ms 43324 KB Output is correct
7 Correct 465 ms 43324 KB Output is correct
8 Correct 456 ms 43324 KB Output is correct
9 Correct 504 ms 43328 KB Output is correct
10 Correct 337 ms 43328 KB Output is correct
11 Correct 494 ms 43328 KB Output is correct
12 Correct 18 ms 43328 KB Output is correct
13 Correct 3168 ms 236400 KB Output is correct
14 Correct 5082 ms 238516 KB Output is correct
15 Correct 1473 ms 238516 KB Output is correct
16 Correct 6611 ms 257268 KB Output is correct
17 Correct 5295 ms 257268 KB Output is correct
18 Correct 1654 ms 257268 KB Output is correct
19 Correct 1006 ms 257268 KB Output is correct
20 Correct 2076 ms 257268 KB Output is correct
21 Correct 1628 ms 257268 KB Output is correct
22 Correct 3423 ms 257268 KB Output is correct
23 Correct 3521 ms 257268 KB Output is correct
24 Correct 5783 ms 257268 KB Output is correct
25 Correct 5536 ms 268980 KB Output is correct
26 Correct 5384 ms 289844 KB Output is correct
27 Correct 7217 ms 334664 KB Output is correct
28 Correct 2029 ms 336856 KB Output is correct
29 Correct 5546 ms 363332 KB Output is correct
30 Correct 5352 ms 387488 KB Output is correct
31 Correct 5500 ms 412144 KB Output is correct
32 Correct 1684 ms 412144 KB Output is correct
33 Correct 975 ms 412144 KB Output is correct
34 Correct 1279 ms 412144 KB Output is correct
35 Correct 1132 ms 412144 KB Output is correct
36 Correct 1729 ms 412144 KB Output is correct
37 Correct 1771 ms 412144 KB Output is correct