Pagini recente » Cod sursa (job #2509551) | Cod sursa (job #2576425) | Borderou de evaluare (job #1478412) | Cod sursa (job #1548999) | Cod sursa (job #1410450)
#include<cstdio>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<bitset>
#include<deque>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<unordered_map>
#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
using namespace std;
const int nmax = 200005;
const int mmax = 400005;
int n, m, i, cost, cnt, f[nmax];
pii sol[mmax];
struct edge
{
int x, y, z;
bool operator < (const edge &A) const
{
return z < A.z;
}
};
edge e[mmax];
int find(int x)
{
if(f[x] != x)
f[x] = find(f[x]);
return f[x];
}
void unite(int x, int y)
{
f[x] = y;
}
int main()
{
freopen("apm.in", "r", stdin);
freopen("apm.out", "w", stdout);
scanf("%d%d", &n, &m);
for(i = 1; i <= n; i++)
f[i] = i;
for(i = 1; i <= m; i++)
scanf("%d%d%d", &e[i].x, &e[i].y, &e[i].z);
sort(e + 1, e + m + 1);
for(i = 1; i <= m; i++)
{
int fx = find(e[i].x);
int fy = find(e[i].y);
if(fx != fy)
{
cost += e[i].z;
sol[++cnt] = mp(e[i].x, e[i].y);
unite(fx, fy);
}
}
printf("%d\n%d\n", cost, cnt);
for(i = 1; i <= cnt; i++)
printf("%d %d\n", sol[i].fi, sol[i].se);
return 0;
}