Pagini recente » Cod sursa (job #897052) | Cod sursa (job #2036242) | Cod sursa (job #488390) | Cod sursa (job #2837887) | Cod sursa (job #1516546)
# include <cstdio>
# include <vector>
# include <algorithm>
# define pb push_back
using namespace std;
FILE *f=freopen("radiatie.in","r",stdin);
FILE *g=freopen("radiatie.out","w",stdout);
const int NMAX= 15001;
const int bufferDim= 10000;
class inputReader {
private:
int pos;
char buffer[bufferDim];
public:
void getBuffer() {
pos= 0;
fread(buffer, 1, bufferDim, stdin);
}
bool digit(char c) {
return ('0' <= c && c <= '9');
}
void getInt(int &nr) {
while(!digit(buffer[pos]))
if(++pos == bufferDim)
getBuffer();
nr = 0;
while(digit(buffer[pos])) {
nr = nr * 10 + buffer[pos] - '0';
if(++pos == bufferDim)
getBuffer();
}
}
} cin;
class muchie{
public:
int a,b,cost;
muchie (int a, int b, int cost)
{
this->a=a;
this->b=b;
this->cost=cost;
}
bool operator < (const muchie &other) const
{
return this->cost < other.cost;
}
};
int n,m,q;
vector <muchie> candidati;
int father[NMAX];
int cost[NMAX];
int depth[NMAX];
bool rez[NMAX];
int group(int x)
{
if (x!=father[x])
x=group(father[x]);
return x;
}
void getDepth(int node) {
if(node == father[node])
return;
getDepth(father[node]);
depth[node] = depth[father[node]] + 1;
}
void read()
{
cin.getBuffer();
cin.getInt(n);
cin.getInt(m);
cin.getInt(q);
for (int i=1;i<=m;i++)
{
int x,y,c;
cin.getInt(x);
cin.getInt(y);
cin.getInt(c);
candidati.pb(muchie(x,y,c));
}
}
void getAPM()
{
sort(candidati.begin(), candidati.end());
for(int i = 1; i <= n; ++i) father[i] = i;
for (int i=0;i<m;i++)
{
muchie mc=candidati[i];
int x=group(mc.a);
int y=group(mc.b);
if (x!=y)
{
father[x]=y;
cost[x]=candidati[i].cost;
}
}
for(int i = 1; i <= m; ++i)
if(!depth[i])
getDepth(i);
}
int query(int x, int y)
{
int maxim = 0;
while(x != y) {
if(depth[x] < depth[y])
swap(x, y);
maxim = max(maxim, cost[x]);
x = father[x];
}
return maxim;
}
int main()
{
read();
getAPM();
for(; q; --q) {
int x, y;
cin.getInt(x); cin.getInt(y);
printf("%d\n", query(x, y));
}
return 0;
}