Cod sursa(job #2279035)

Utilizator rares9301Sarmasag Rares rares9301 Data 8 noiembrie 2018 20:39:57
Problema Radiatie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.92 kb

#include <bits/stdc++.h>

 

 

 

using namespace std;

 

 

 

const int MAXN = 15000;

 

const int MAXM = 30000;

 

const int MAXL = 13;

 

 

 

vector<pair<int, int> >G[MAXN + 5];

 

 

 

struct Edge {

 

  int u, v, c;

 

  bool operator < (const Edge& other) const {

 

    return c < other.c;

 

  }

 

}E[MAXM + 5];

 

 

 

int t[MAXN + 5], h[MAXN + 5];

 

 

 

int f(int x) {

 

  if (t[x] == x)

 

    return x;

 

  int aux = f(t[x]);

 

  t[x] = aux;

 

  return aux;

 

}

 

 

 

int u(int x, int y) {

 

  x = f(x);

 

  y = f(y);

 

  if (x == y)

 

    return 0;

 

  if (h[x] < h[y])

 

    swap(x, y);

 

  if (h[x] == h[y])

 

    h[x]++;

 

  t[y] = x;

 

  return 1;

 

}

 

 

 

void addEdge(Edge e) {

 

  G[e.u].push_back({e.v, e.c});

 

  G[e.v].push_back({e.u, e.c});

 

}

 

 

 

int rmq[MAXL + 5][MAXN + 5];

 

int st[MAXL + 5][MAXN + 5];

 

int hh[MAXN + 5];

 

 

 

void dfs(int node, pair<int, int> f) {

 

  st[0][node] = f.first;

 

  rmq[0][node] = f.second;

 

  hh[node] = 1 + hh[f.first];

 

  for (int i = 1; i <= MAXL; ++i) {

 

    st[i][node] = st[i - 1][st[i - 1][node]];

 

    rmq[i][node] = max(rmq[i - 1][node], rmq[i - 1][st[i - 1][node]]);

 

  }

 

  for (auto it:G[node])

 

    if (it.first != f.first)

 

      dfs(it.first, {node, it.second});

 

}

 

 

 

int query(int x, int y) {

 

  int ans = 0;

 

  if (hh[x] < hh[y])

 

    swap(x, y);

 

  for (int i = MAXL; i >= 0; --i)

 

    if (hh[x] - (1 << i) >= hh[y]) {

 

      ans = max(ans, rmq[i][x]);

 

      x = st[i][x];

 

    }

 

  for (int i = MAXL; i >= 0; --i)

 

    if (hh[x] - (1 << i) > 0 && st[i][x] != st[i][y]) {

 

      ans = max(ans, max(rmq[i][x], rmq[i][y]));

 

      x = st[i][x];

 

      y = st[i][y];

 

    }

 

  if (x != y)

 

    ans = max(ans, max(rmq[0][x], rmq[0][y]));

 

  return ans;

 

}

 

 

 

int main() {

 

  freopen("radiatie.in", "r", stdin);

 

  freopen("radiatie.out", "w", stdout);

 

 

 

  int n, m, k;

 

  scanf("%d%d%d", &n, &m, &k);

 

  for (int i = 1; i <= m; ++i)

 

    scanf("%d%d%d", &E[i].u, &E[i].v, &E[i].c);

 

 

 

  sort(E + 1, E + m + 1);

 

 

 

  for (int i = 1; i <= n; ++i)

 

    t[i] = i;

 

 

 

  for (int i = 1; i <= m; ++i)

 

    if (u(E[i].u, E[i].v))

 

      addEdge(E[i]);

 

 

 

  dfs(1, {0, 0});

 

 

 

  for (int i = 1; i <= k; ++i) {

 

    int x, y;

 

    scanf("%d%d", &x, &y);

 

    printf("%d\n", query(x, y));

 

  }

 

 

 

  return 0;

 

}