Pagini recente » Cod sursa (job #1181218) | Cod sursa (job #2003771) | Cod sursa (job #840717) | Cod sursa (job #1294146) | Cod sursa (job #2516680)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <list>
using namespace std;
struct muc{
int a, b;
int v;
bool operator<(const muc &rhs){
return v < rhs.v;
}
};
ifstream fin("radiatie.in");
ofstream fout("radiatie.out");
int n, m, q;
vector<muc> muci;
int tre[15041];
void nuke(){
for(int i = 1; i <= n; ++i){
tre[i] = i;
}
}
int dad(int a){
if(a != tre[a]){
tre[a] = dad(tre[a]);
}
return tre[a];
}
void doit(int a, int b){
tre[dad(a)] = dad(b);
}
bool chit(int a, int b){
return dad(a)==dad(b);
}
void yes(){
nuke();
sort(muci.begin(), muci.end());
vector<muc> wuci;
for(auto x : muci){
if(!chit(x.a, x.b)){
doit(x.a, x.b);
wuci.push_back(x);
}
}
muci = wuci;
}
int sol[15041];
list<muc> liszt;
void no(){
nuke();
for(auto x : muci){
doit(x.a, x.b);
for(auto it = liszt.begin(); it != liszt.end(); ++it){
muc y = *it;
if(chit(y.a, y.b)){
sol[y.v] = x.v;
auto bye = it;
--it;
liszt.erase(bye);
}
}
}
}
void maybe(){
for(int i = 0; i < q; ++i){
fout << sol[i] << "\n";
}
}
int main(){
fin >> n >> m >> q;
for(int i = 0; i < m; ++i){
muc x;
fin >> x.a >> x.b >> x.v;
muci.push_back(x);
}
yes();
for(int i = 0; i < q; ++i){
muc x;
fin >> x.a >> x.b;
x.v = i;
liszt.push_back(x);
}
no();
maybe();
return 0;
}