Pagini recente » Cod sursa (job #1502272) | Cod sursa (job #1979048) | Cod sursa (job #362330) | Cod sursa (job #1913929) | Cod sursa (job #1515818)
# 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;
class citeste{
char buffer[10000];
int poz=0;
bool digit (char &c)
{
return ('0' <=c && c<='9');
}
public:
void getbuffer()
{
fread(buffer,1,10000,stdin);
poz=0;
}
void getInt(int &numar)
{
numar=0;
while (!digit(buffer[poz]))
{
if (++poz==10000)
{
getbuffer();
}
}
while (digit(buffer[poz]))
{
numar=numar*10 + buffer[poz]- '0';
if (++poz==10000)
{
getbuffer();
}
}
}
}cin;
class vecin{
public:
int capat, cost;
vecin(int capat, int cost)
{
this->capat=capat;
this->cost=cost;
}
};
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];
void read()
{
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));
}
}
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 getAPM()
{
sort(candidati.begin(), candidati.end());
for(int i = 1; i <= n; ++i) father[i] = i;
for (int i=0;i<candidati.size();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;
}