Cod sursa(job #2504571)

Utilizator lucametehauDart Monkey lucametehau Data 5 decembrie 2019 10:48:18
Problema Radiatie Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.49 kb
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

ifstream cin ("radiatie.in");
ofstream cout ("radiatie.out");

struct Edge {
  int x, y, z;
  bool operator < (const Edge &other) const {
    return z < other.z;
  }
};

int n, m, k, x, y, z;

vector <Edge> edge;
int t[15005], rang[15005], cost[15005];

int find(int x) {
  int nod = x, y;
  while(t[nod] != nod)
    nod = t[nod];
  while(t[x] != x) {
    y = t[x];
    t[x] = nod;
    x = y;
  }
  return nod;
}

void join(int x, int y, int z) {
  if(rang[x] > rang[y])
    t[y] = x, cost[y] = z;
  else
    t[x] = y, cost[x] = z;
  if(rang[x] == rang[y])
    rang[y]++;
}

int main() {
  cin >> n >> m >> k;
  for(int i = 0; i < m; i++) {
    cin >> x >> y >> z;
    edge.push_back({x, y, z});
  }
  for(int i = 1; i <= n; i++)
    t[i] = i, rang[i] = 1;
  sort(edge.begin(), edge.end());
  for(int i = 0; i < m; i++) {
    if(find(edge[i].x) != find(edge[i].y))
      join(edge[i].x, edge[i].y, edge[i].z);
  }
  for(int i = 1; i <= k; i++) {
    cin >> x >> y;
    int ans = 0;
    while(x != y) {
      if(rang[x] < rang[y]) {
        ans = max(ans, cost[x]);
        x = t[x];
      } else if(rang[x] > rang[y]) {
        ans = max(ans, cost[y]);
        y = t[y];
      } else {
        if(x == t[x])
          ans = max(ans, cost[y]), y = t[y];
        else
          ans = max(ans, cost[x]), x = t[x];
      }
    }
    cout << ans << "\n";
  }
  return 0;
}