Pagini recente »
Cod sursă (job #804511)
Cod sursă (job
#804511)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("zapada.in");
ofstream fout("zapada.out");
int n, m, k, t[10001], r[10001], fr[100001];
vector <pair<int, int>> C[100001];
int Find(int x)
{
int y;
if (t[x] == 0)
return x;
y = Find(t[x]);
t[x] = y;
return y;
}
void Union(int x, int y)
{
if (r[x] > r[y])
{
t[y] = x;
r[x] += r[y];
}
else
{
t[x] = y;
r[y] += r[x];
}
}
int main()
{
int i, j, x, y, c, maxim = -1, suma = 0;
fin >> n >> m >> k;
for (i = 1 ; i <= m ; i++)
{
fin >> x >> y >> c;
C[c].push_back({x,y});
++fr[c];
maxim = max(maxim, c);
}
for (i = 1 ; i <= maxim ; i++)
if (fr[i] > 0)
{
for (auto next : C[i])
{
x = next.first;
y = next.second;
x = Find(x);
y = Find(y);
if (x != y)
{
Union(x, y);
suma += i;
}
}
}
fout << suma << "\n";
for (i = 1 ; i <= k ; i++)
{
for (j = 1 ; j <= n ; j++)
{
t[j] = 0;
r[j] = 1;
}
fin >> x >> y >> c;
C[c].push_back({x, y});
suma = 0;
for (j = 1 ; j <= maxim ; j++)
if (fr[j] > 0)
{
for (auto next : C[j])
{
x = next.first;
y = next.second;
x = Find(x);
y = Find(y);
if (x != y)
{
Union(x, y);
suma += j;
}
}
}
fout << suma << "\n";
}
return 0;
}