Cod sursă (job #804514)

Utilizator avatar cimpoesufabian Cimpoesu Fabian George cimpoesufabian IP ascuns
Problemă Zapada (clasele 11 și 12) Compilator cpp-32 | 1,58 kb
Rundă Arhiva de probleme Status evaluat
Dată 15 ian. 2025 19:30:50 Scor 40
#include <bits/stdc++.h>
using namespace std;
ifstream fin("zapada.in");
ofstream fout("zapada.out");

int n, m, k, t[10001];
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)
{
    t[y] = 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});
        maxim = max(maxim, c);
    }
    for (i = 1 ; i <= maxim ; i++)
        if (C[i].size() > 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;
        fin >> x >> y >> c;
        C[c].push_back({x, y});
        suma = 0;
        for (j = 1 ; j <= maxim ; j++)
            if (C[j].size() > 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;
}