Pagini recente » Cod sursa (job #369941) | Cod sursa (job #1713286) | Cod sursa (job #1807358) | Cod sursa (job #680625) | Cod sursa (job #2507627)
#include <bits/stdc++.h>
#define MAX 131072
using namespace std;
const int NMAX = 15010;
FILE *IN;
struct edges{
int x, y, cost;
}edge[NMAX];
int N, M, K;
int comp[NMAX], ans[2 * NMAX];
bool use[2 * NMAX];
vector <int> bucket[140];
pair <int, int> query[2 * NMAX];
int pos, sign;
char f[MAX];
inline void Read(int &nr){
sign = 0;
nr = 0;
while(f[pos] < '0' || f[pos] > '9'){
if(f[pos] == '-') sign = 1;
pos++;
if(pos == MAX)
fread(f, MAX, 1, IN), pos = 0;
}
while(f[pos] >= '0' && f[pos] <= '9'){
nr = 10 * nr + f[pos++] - '0';
if(pos == MAX)
fread(f, MAX, 1, IN), pos = 0;
}
if(sign) nr =- nr;
}
inline bool cmp(edges a, edges b){
return a.cost < b.cost;
}
void read(){
Read(N); Read(M); Read(K);
for(int i = 1; i <= M; i++){
Read(edge[i].x);
Read(edge[i].y);
Read(edge[i].cost);
}
sort(edge + 1, edge + M + 1, cmp);
for(int i = 1; i <= K; i++){
Read(query[i].first);
Read(query[i].second);
}
}
inline int Find(int x){
int root = x, aux;
while(comp[root] != root)
root = comp[root];
while(x != root){
aux = comp[x];
comp[x] = root;
x = aux;
}
return x;
}
inline void Union(int x, int y){
comp[Find(x)] = Find(y);
}
void create_buckets(){
int lim = sqrt(M) + 1, cnt = 1;
for(int i = 1; i <= lim; i++){
int k = i * lim;
for(int j = cnt; j <= min(k, M); j++, cnt++)
Union(edge[j].x, edge[j].y);
for(int j = 1; j <= K; j++){
if(Find(query[j].first) == Find(query[j].second) && !use[j]){
use[j] = true;
bucket[i].push_back(j);
}
}
}
}
int main(){
IN = fopen("radiatie.in", "r");
freopen("radiatie.out", "w", stdout);
read();
for(int i = 1; i <= N; i++)
comp[i] = i;
create_buckets();
for(int i = 1; i <= N; i++)
comp[i] = i;
for(int i = 1; i <= K; i++)
use[i] = false;
int lim = sqrt(M) + 1, cnt = 1;
for(int i = 1; i <= lim; i++){
int k = i * lim;
for(int j = cnt; j <= min(k, M); j++, cnt++){
Union(edge[j].x, edge[j].y);
for(int l = 0; l < bucket[i].size(); l++)
if(Find(query[bucket[i][l]].first) == Find(query[bucket[i][l]].second) && !use[bucket[i][l]])
ans[bucket[i][l]] = edge[j].cost, use[bucket[i][l]] = true;
}
}
for(int i = 1; i <= K; i++)
printf("%d\n", ans[i]);
return 0;
}