Submission #153971

#TimeUsernameProblemLanguageResultExecution timeMemory
153971MercenaryFactories (JOI14_factories)C++14
Compilation error
0 ms0 KiB
#include "factories.h"
#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair

using namespace std;
const int maxn = 5e5 + 5;
const int logn = log2(maxn) + 1;
typedef pair<int,int> ii;
typedef long long ll;

int h[maxn] , P[maxn][logn];
int big[maxn] , vis[maxn] , n , sub[maxn];
ll dep[maxn] , cen[maxn];

vector<int> v[maxn] , now;
vector<ii> adj[maxn];

void DFS(int u , int v){
    now.pb(u);
    sub[u] = 1;
    big[u] = -1;
    for(auto& c : adj[u]){
        if(c.first != v && vis[c.first] == 0){
            DFS(c.first , u);
            sub[u] += sub[c.first];
            if(big[u] == -1 || sub[big[u]] < sub[c.first])big[u] = c.first;
        }
    }
}

int FindRoot(int u){
    now.clear();
    DFS(u , -1);
    int total = sub[u];
    while(big[u] != -1 && sub[big[u]] * 2 >= total)u = big[u];
    return u;
}

void Decompose(int u){
    u = FindRoot(u);
    for(int & c : now)v[c].pb(u);
    vis[u] = 1;
    for(auto& c : adj[u]){
        if(vis[c.first] == 0){
            Decompose(c.first);
        }
    }
}

int LCA(int u , int v){
    if(h[u] < h[v])swap(u , v);
    for(int i = 0 ; i < logn && h[u] != h[v]  ; ++i){
        if((h[u] - h[v]) & (1 << i))u = P[u][i];
    }
    if(u == v)return u;
    for(int i = logn - 1 ; i >= 0 ; --i){
        if(P[u][i] != P[v][i]){
            u = P[u][i];
            v = P[v][i];
        }
    }
    return P[u][0];
}

ll dis(int u , int v){
    return dep[u] + dep[v] - 2 * dep[LCA(u , v)];
}

vector<ll> predis[maxn];

void Init(int N, int A[], int B[], int D[]) {
    n = N;
    for(int i = 0 ; i < n - 1 ; ++i){
        adj[A[i]].pb(mp(B[i] , D[i]));
        adj[B[i]].pb(mp(A[i] , D[i]));
    }
    function<void(int,int)> DFS = [&](int u , int v){
        for(auto & c : adj[u]){
            if(c.first != v){
                h[c.first] = h[u] + 1;
                dep[c.first] = dep[u] + c.second;
                P[c.first][0] = u;
                for(int i = 1 ; i < logn && P[c.first][i - 1] != -1 ; ++i){
                    P[c.first][i] = P[P[c.first][i - 1]][i - 1];
                }
                DFS(c.first , u);
            }
        }
    };
    memset(P,-1,sizeof P);
    DFS(0 , -1);
    Decompose(0);
    for(int i = 0 ; i < n ; ++i)cen[i] = 1e18;
    for(int i = 0 ; i < n ; ++i){
        for(int & c : v[i]){
            predis[i].pb(dis(c , i));
        }
    }
}

long long Query(int S, int X[], int T, int Y[]) {
    ll total = 1e18;
    for(int i = 0 ; i < S ; ++i){
        for(int j = (int)v[X[i]].size() -1; j >= 0 ; --j){
            cen[v[X[i]][j]] = min(cen[v[X[i]][j]] , predis[X[i]][j]);
        }
    }
    for(int i = 0 ; i < T ; ++i){
        ll res = 1e18;
        for(int j = (int)v[Y[i]].size() -1; j >= 0 ; --j){
             res = min(res , predis[Y[i]][j] + cen[v[Y[i]][j]]);
        }
        total = min(total , res);
    }
    for(int i = 0 ; i < S ; ++i){
        for(int c : v[X[i]]){
            cen[c] = 1e18;
        }
    }
    return total;
}

#include "factories.h"

#include <stdio.h>
#include <stdlib.h>

#define MAX_N          500000
#define MAX_Q          100000
#define MAX_SUM_ST    1000000
#define MAX_VALUE  1000000000

static int N, Q;
static int A[MAX_N], B[MAX_N], D[MAX_N];
static int S[MAX_N];
static int T[MAX_N];
static int X[MAX_SUM_ST];
static int Y[MAX_SUM_ST];

static int Qx[MAX_N];
static int Qy[MAX_N];

int main() {
  freopen("sample-in-01.txt","r",stdin);

  int i, j, k;
  int STop, TTop;

  if (2 != scanf("%d%d", &N, &Q)) {
    fprintf(stderr, "error: cannot read N and Q.\n");
    exit(1);
  }
  if (!(2 <= N && N <= MAX_N)) {
    fprintf(stderr, "error: N is out of bounds.\n");
    exit(1);
  }
  if (!(1 <= Q && Q <= MAX_Q)) {
    fprintf(stderr, "error: Q is out of bounds.\n");
    exit(1);
  }
  for (i = 0; i < N - 1; ++i) {
    if (1 != scanf("%d", &A[i])) {
      fprintf(stderr, "error: cannot read A[%d].\n", i);
      exit(1);
    }
    if (!(0 <= A[i] && A[i] <= N - 1)) {
      fprintf(stderr, "error: A[%d] is out of bounds.\n", i);
      exit(1);
    }
    if (1 != scanf("%d", &B[i])) {
      fprintf(stderr, "error: cannot read B[%d].\n", i);
      exit(1);
    }
    if (!(0 <= B[i] && B[i] <= N - 1)) {
      fprintf(stderr, "error: B[%d] is out of bounds.\n", i);
      exit(1);
    }
    if (A[i] == B[i]) {
      fprintf(stderr, "error: B[%d] is equal to A[%d].\n", i, i);
      exit(1);
    }
    if (1 != scanf("%d", &D[i])) {
      fprintf(stderr, "error: cannot read D[%d].\n", i);
      exit(1);
    }
    if (!(1 <= D[i] && D[i] <= MAX_VALUE)) {
      fprintf(stderr, "error: D[%d] is out of bounds.\n", i);
      exit(1);
    }
  }

  STop = 0;
  TTop = 0;

  for (j = 0; j < Q; ++j) {
    if (2 != scanf("%d%d", &S[j], &T[j])) {
      fprintf(stderr, "error: cannot read L[%d] and R[%d].\n", j, j);
      exit(1);
    }

    if(STop + S[j] > MAX_SUM_ST) {
      fprintf(stderr, "error: S[0] + S[1] + ... + S[%d] is out of bounds.\n", j);
      exit(1);
    }

    if(TTop + T[j] > MAX_SUM_ST) {
      fprintf(stderr, "error: T[0] + T[1] + ... + T[%d] is out of bounds.\n", j);
      exit(1);
    }

    for (k = 0; k < S[j]; ++k, ++STop) {
      if (1 != scanf("%d", &X[STop])) {
        fprintf(stderr, "error: cannot read X[%d][%d].\n", j, k);
        exit(1);
      }

      if (!(0 <= X[STop] && X[STop] <= N - 1)) {
        fprintf(stderr, "error: cannot read X[%d][%d].\n", j, k);
        exit(1);
      }
    }

    for (k = 0; k < T[j]; ++k, ++TTop) {
      if (1 != scanf("%d", &Y[TTop])) {
        fprintf(stderr, "error: cannot read Y[%d][%d].\n", j, k);
        exit(1);
      }

      if (!(0 <= Y[TTop] && Y[TTop] <= N - 1)) {
        fprintf(stderr, "error: cannot read Y[%d][%d].\n", j, k);
        exit(1);
      }
    }
  }

  STop = 0;
  TTop = 0;
  Init(N, A, B, D);

  for (j = 0; j < Q; ++j) {
    for (k = 0; k < S[j]; k++) {
        Qx[k] = X[STop++];
    }
    for (k = 0; k < T[j]; k++) {
        Qy[k] = Y[TTop++];
    }

    printf("%lld\n", Query(S[j], Qx, T[j], Qy));
  }


  return 0;
}

Compilation message (stderr)

factories.cpp: In function 'int main()':
factories.cpp:145:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   freopen("sample-in-01.txt","r",stdin);
   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/tmp/ccNu8fCI.o: In function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'
/tmp/ccumw9qT.o:factories.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status