Pagini recente » Cod sursa (job #1737526) | Cod sursa (job #740015) | Cod sursa (job #2415372) | Cod sursa (job #462883) | Cod sursa (job #1518632)
#include <fstream>
#include <vector>
#include <algorithm>
#define NMax 15010
using namespace std;
ifstream f("radiatie.in");
ofstream g("radiatie.out");
using namespace std;
int nodes, edges, queries, disTree[NMax], root;
vector< pair<int, int> > apm[NMax];
struct edge {
int ext1;
int ext2;
int cost;
} G[30010];
struct mnode {
int level;
int father;
int cost;
} arbStruct[NMax];
bool cmp(const edge &e1, const edge &e2)
{
return e1.cost < e2.cost;
}
void DFS(int node, int level)
{
arbStruct[node].level = level;
for (vector <pair <int, int> >::iterator it = apm[node].begin(); it != apm[node].end(); it++) {
DFS(it->first, level+1);
}
}
int findFather(int node)
{
while (disTree[node] > 0)
node = disTree[node];
return node;
}
int getMax(int a, int b)
{
if (a > b)
return a;
return b;
}
int main()
{
f>>nodes>>edges>>queries;
for (int i=1; i<=edges; i++)
f>>G[i].ext1>>G[i].ext2>>G[i].cost;
for (int i=1; i<=nodes; i++)
disTree[i] = -1;
sort (G+1, G+edges+1, cmp);
int i = 1, crtEdge = 0;
while (i <= nodes - 1)
{
crtEdge++;
int father1 = findFather(G[crtEdge].ext1);
int father2 = findFather(G[crtEdge].ext2);
if (father1 != father2) {
if (-father1 > -father2) {
disTree[father1] += disTree[father2];
disTree[father2] = father1;
root = father1;
apm[father1].push_back( make_pair(father2, G[crtEdge].cost));
// arbStruct[G[crtEdge].ext2].father = G[crtEdge].ext1;
arbStruct[father2].cost = G[crtEdge].cost;
}
else {
disTree[father2] += disTree[father1];
disTree[father1] = father2;
root = father2;
apm[father2].push_back( make_pair(father1, G[crtEdge].cost));
//arbStruct[G[crtEdge].ext1].father = G[crtEdge].ext2;
arbStruct[father1].cost = G[crtEdge].cost;
}
i++;
}
}
DFS(root, 1);
int x, y;
for (int i=1; i<=queries; i++) {
f>>x>>y;
int answ = -1;
while (arbStruct[x].level > arbStruct[y].level) {
answ = getMax(answ, arbStruct[x].cost);
x = disTree[x];
}
while (arbStruct[x].level < arbStruct[y].level) {
answ = getMax(answ, arbStruct[y].cost);
y = disTree[y];
}
while (x != y) {
answ = getMax(answ, arbStruct[x].cost);
answ = getMax(answ, arbStruct[y].cost);
x = disTree[x];
y = disTree[y];
}
g << answ << "\n";
}
return 0;
}