Pagini recente » Cod sursa (job #553247) | Cod sursa (job #3126366) | Cod sursa (job #714680) | Cod sursa (job #837192) | Cod sursa (job #3286415)
#include<bits/stdc++.h>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
struct Trei
{
int x, y, c, v;
bool operator<(const Trei e) const
{
return c < e.c;
}
} a[400005];
int Total;
int n, m;
vector<pair<int,int>> sol;
int t[200005];
void Union(int x, int y)
{
t[y] = x;
}
int FindRoot(int x)
{
int rad, y;
rad = x;
while(t[rad] != 0)
rad = t[rad];
while(x != rad)
{
y = t[x];
t[x] = rad;
x = y;
}
return rad;
}
int main()
{
int i, x, y;
fin >> n >> m;
for(i=1;i<=m;i++)
fin >> a[i].x >> a[i].y >> a[i].c;
sort(a + 1, a + m + 1);
int nrc = n;
for(i=1;i<=m && nrc > 1;i++)
{
x = FindRoot(a[i].x);
y = FindRoot(a[i].y);
if(x != y)
{
Union(x, y);
a[i].v = 1;
Total += a[i].c;
sol.push_back({a[i].x, a[i].y});
nrc--;
}
}
fout << Total << "\n";
fout << sol.size() << "\n";
for(auto w : sol)
fout << w.first << " " << w.second << "\n";
return 0;
}