Cod sursa(job #1481928)

Utilizator hrazvanHarsan Razvan hrazvan Data 5 septembrie 2015 16:57:27
Problema Atac Scor 0
Compilator c Status done
Runda Arhiva de probleme Marime 2.39 kb
#include <stdio.h>
#define INF 2000000000
#define MAXN 32000
#define MAXLG 14
int ult[MAXN], next[2 * MAXN - 2], nod[2 * MAXN - 2], cost[2 * MAXN - 2], ad[MAXN];
int anc[MAXLG + 1][MAXN], min[MAXLG + 1][MAXN];
char vis[MAXN];

inline int min2(int a, int b){
  return a < b ? a : b;
}

void init(int x){
  vis[x] = 1;
  int poz = ult[x];
  while(poz != -1){
    if(!vis[nod[poz]]){
      ad[nod[poz]] = ad[x] + 1;
      anc[0][nod[poz]] = x;
      min[0][nod[poz]] = cost[poz];
      init(nod[poz]);
    }
    poz = next[poz];
  }
}

inline void calc(int n){
  int i, j;
  for(i = 1; i <= MAXLG; i++){
    for(j = 0; j < n; j++){
      if(anc[i - 1][j] != -1){
        anc[i][j] = anc[i - 1][anc[i - 1][j]];
        if(anc[i - 1][anc[i - 1][j]] != -1)
          min[i][j] = min2(min[i - 1][j], min[i - 1][anc[i - 1][j]]);
      }
      else
        anc[i][j] = -1;
    }
  }
}

inline int rezolv(int x, int y){
  if(x == y)
    return 0;
  int aux, i, move, rez = INF;
  if(ad[x] < ad[y]){
    aux = x;  x = y;  y = aux;
  }
  move = ad[x] - ad[y];
  for(i = MAXLG; i >= 0; i--){
    if((1 << i) <= move){
      rez = min2(rez, min[i][x]);
      x = anc[i][x];
      move -= (1 << x);
    }
  }
  for(i = MAXLG; i >= 0; i--){
    if((1 << i) <= ad[x]){
      if(anc[i][x] != anc[i][y]){
        rez = min2(rez, min2(min[i][x], min[i][y]));
        x = anc[i][x];
        y = anc[i][y];
      }
    }
  }
  if(x != y)
    rez = min2(rez, min2(min[0][x], min[0][y]));
  return rez;
}

int main(){
  FILE *in = fopen("atac.in", "r");
  int n, m, p, i, dr = 0, x, y;
  fscanf(in, "%d%d%d", &n, &m, &p);
  for(i = 0; i < n; i++)
    ult[i] = -1;
  for(i = 1; i < n; i++){
    fscanf(in, "%d%d", &x, &y);
    x--;
    nod[dr] = i;
    next[dr] = ult[x];
    cost[dr] = y;
    ult[x] = dr;
    dr++;
    nod[dr] = x;
    next[dr] = ult[i];
    cost[dr] = y;
    ult[i] = dr;
    dr++;
  }
  int a, b, c, d;
  fscanf(in, "%d%d%d%d%d%d", &x, &y, &a, &b, &c, &d);
  fclose(in);
  ad[0] = 0;
  anc[0][0] = -1;
  init(0);
  calc(n);
  FILE *out = fopen("atac.out", "w");
  int rez, x2, y2;
  for(i = 0; i < m; i++){
    rez = rezolv(x - 1, y - 1);
    if(m - i <= p)
      fprintf(out, "%d\n", rez);
    x2 = (1LL * x * a + 1LL * y * b) % n + 1;
    y2 = (1LL * y * c + 1LL * rez * d) % n + 1;
    x = x2;
    y = y2;
  }
  fclose(out);
  return 0;
}