Pagini recente » Cod sursa (job #1054753) | Cod sursa (job #158210) | Cod sursa (job #56409) | Cod sursa (job #2255058) | Cod sursa (job #3286411)
#include <fstream>
#include <stack>
#include <queue>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <set>
#include <cstring>
#include <map>
#include <string>
#include <bitset>
#include <unordered_map>
#include <unordered_set>
#define oo 2000000
#define MOD 1000000007
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
struct muchie {
int x, y, c;
bool operator<(const muchie m)const
{
return c<m.c;
}
}a[400005];
int n, m;
vector<pair<int,int>>rez;
int r[200000], t[200000],costmax;
void Union(int x, int y)
{
if (r[x] > r[y])
{
t[y] = x;
}
else
{
t[x] = y;
if (r[x] == r[y])
r[y]++;
}
}
int Find(int x)
{
if (t[x] == 0)
return x;
int y = Find(t[x]);
t[x] = y;
return y;
}
int main()
{
fin >> n >> m;
for (int i = 1; i <= m; i++)
{
fin >> a[i].x >> a[i].y >> a[i].c;
}
sort(a + 1, a + m + 1);
for (int i = 1; i <= m; i++)
{
int x = Find(a[i].x);
int y = Find(a[i].y);
if (x != y)
{
Union(x, y);
costmax += a[i].c;
rez.push_back({ a[i].x,a[i].y });
}
}
fout << costmax<<"\n";
fout << rez.size()<<"\n";
for (int i = 0; i < rez.size(); i++)
fout << rez[i].first << " " << rez[i].second << "\n";
}