forked from andreimargeloiu/Competitive-programming-problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apm2.cpp
80 lines (79 loc) · 1.51 KB
/
apm2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# include <fstream>
# include <vector>
# include <algorithm>
using namespace std;
ifstream f("apm2.in");
ofstream g("apm2.out");
struct elem
{
int nod, cost;
}E;
struct elem2
{
int x, y, cost;
}a[100005];
vector <elem> v[10005];
int i,j,n,m,q,x,y,VV,ok;
int R[10005],cost[10005],ap[10005],H[10005],maxx[10005];
bool cmp (elem2 x, elem2 y)
{
if (x.cost>=y.cost) return 0;
else return 1;
}
int radacina (int x)
{
while (R[x]!=x)
x=R[x];
return x;
}
void APM ()
{
int Rx, Ry;
for (i=1; i<=m; ++i)
{
Rx=radacina(a[i].x);
Ry=radacina(a[i].y);
if (Rx!=Ry)
{
if (H[Rx]>H[Ry]) R[Ry]=Rx,H[Rx]+=H[Ry];
else R[Rx]=Ry,H[Ry]+=H[Rx];
//adaug muchia in graf
E.nod=a[i].y; E.cost=a[i].cost;
v[a[i].x].push_back(E);
E.nod=a[i].x; E.cost=a[i].cost;
v[a[i].y].push_back(E);
}
}
}
void DFS (int k, int o)
{
ap[k]=o;
if (k==y) ok=0;
for (int i=0; i<v[k].size() && ok; ++i)
{
if (ap[v[k][i].nod]!=o)
{
maxx[v[k][i].nod]=max(maxx[k], v[k][i].cost);
DFS (v[k][i].nod, o);
}
}
}
int main ()
{
f>>n>>m>>q;
//radacina inititala
for (i=1; i<=n; ++i)
R[i]=i,H[i]=1;
for (i=1; i<=m; ++i)
f>>a[i].x>>a[i].y>>a[i].cost;
sort (a+1,a+m+1,cmp);
APM ();
for (i=1; i<=q; ++i)
{
f>>x>>y;
maxx[x]=0; ok=1;
DFS (x,i);
g<<maxx[y]-1<<"\n";
}
return 0;
}