Mai intai trebuie sa te autentifici.
Cod sursa(job #2450708)
Utilizator | Data | 24 august 2019 12:18:47 | |
---|---|---|---|
Problema | Arbore partial de cost minim | Scor | 100 |
Compilator | cpp-64 | Status | done |
Runda | repost | Marime | 1.4 kb |
#include <fstream>
#include <algorithm>
#include <vector>
#define NMAX 200010
#define MMAX 400010
#define cost first
#define a second.first
#define b second.second
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
int n, m, ans, t[NMAX], h[NMAX];
pair <int, pair<int,int> > v[MMAX];
vector < pair<int,int> > sol;
void inters(int x, int y)
{
if(h[x] > h[y])
t[y] = x;
else
{
t[x] = y;
if(h[x] == h[y])
h[y]++;
}
}
int where(int x)
{
int radacina = x;
while(t[radacina] != radacina)
radacina = t[radacina];
int y = x;
while(y != radacina)
{
int tata = t[y];
t[y] = radacina;
y = tata;
}
return radacina;
}
int main()
{
f >> n >> m;
for(int i = 1; i <= n; ++i)
{
t[i] = i;
h[i] = 1;
}
for(int i = 1; i <= m; ++i)
f >> v[i].a >> v[i].b >> v[i].cost;
sort(v + 1, v + m + 1);
for(int i = 1; i <= m; ++i)
if( where(v[i].a) != where(v[i].b))
{
ans += v[i].cost;
inters(where(v[i].a) , where(v[i].b));
sol.push_back({v[i].a, v[i].b});
}
g << ans << '\n' << sol.size() << '\n';
for(int i = 0; i < sol.size(); ++i)
g << sol[i].first << ' ' << sol[i].second << '\n';
f.close();
g.close();
return 0;
}