Pagini recente » Cod sursa (job #1968264) | Cod sursa (job #1179410) | Cod sursa (job #903768) | Cod sursa (job #982659) | Cod sursa (job #2868523)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
struct muchie{
int x, y, c;
bool operator < (const muchie A) const
{
return A.c > c;
}
}M[400002];
int n, m, T[200002], sum, ans[400002], len;
int Find(int x)
{
while(T[x] != x)
x = T[x];
return x;
}
void Union(int x, int y)
{
T[x] = y;
}
void Afisare()
{
fout << sum << "\n" << len << "\n";
for(int i = 1; i <= len; i++)
fout << M[ans[i]].x << " " << M[ans[i]].y << "\n";
}
int main()
{
int i;
fin >> n >> m;
for(i = 1; i <= m; i++)
fin >> M[i].x >> M[i].y >> M[i].c;
sort(M + 1, M + m + 1);
for(i = 1; i <= n; i++)
T[i] = i;
for(i = 1; i <= m; i++)
{
int t1 = Find(M[i].x);
int t2 = Find(M[i].y);
if(t1 != t2)
{
Union(t1, t2);
sum += M[i].c;
ans[++len] = i;
}
}
Afisare();
return 0;
}