Pagini recente » Cod sursa (job #913272) | Cod sursa (job #905648) | Cod sursa (job #921822) | Cod sursa (job #984334) | Cod sursa (job #1733471)
#include <bits/stdc++.h>
#define Nmax 200002
FILE *fout = freopen("apm.out", "w", stdout);
using namespace std;
int N, M, parent[Nmax], S, sol[Nmax], r[Nmax];
struct edge
{
int x, y, c;
}e[Nmax * 2];
class InputReader {
public:
InputReader() {}
InputReader(const char *file_name) {
input_file = fopen(file_name, "r");
cursor = 0;
fread(buffer, SIZE, 1, input_file);
}
inline InputReader &operator >>(int &n) {
while((buffer[cursor] < '0' || buffer[cursor] > '9')&&buffer[cursor]!='-') {
advance();
}
n =0;
semn= 1;
if(buffer[cursor]=='-') semn=-1,advance();
while('0' <= buffer[cursor] && buffer[cursor] <= '9') {
n = n * 10 + buffer[cursor] - '0';
advance();
}
n*=semn;
return *this;
}
private:
FILE *input_file;
static const int SIZE = 1 << 17;
int cursor,semn;
char buffer[SIZE];
inline void advance() {
++ cursor;
if(cursor == SIZE) {
cursor = 0;
fread(buffer, SIZE, 1, input_file);
}
}
}f("apm.in");
void read()
{
f >> N >> M;
for(int i = 0; i < M; ++ i)
{
f >> e[i].x >> e[i].y >> e[i].c;
}
}
bool cmp(const edge a, const edge b)
{
return a.c < b.c;
}
inline int FIND(int x)
{
if(r[x] == 0) return x;
r[x] = FIND(r[x]);
return r[x];
}
void UNION(int x, int y)
{
int rx = FIND(x), ry = FIND(y);
r[rx] = ry;
}
int main()
{
int i;
read();
sort(e, e + M, cmp);
for(i = 0; i < M; ++ i)
{
if(FIND(e[i].x) != FIND(e[i].y))
{
S += e[i].c;
UNION(e[i].x, e[i].y);
sol[++ sol[0]] = i;
}
}
printf("%d\n%d\n", S, N - 1);
for(i = 1; i < N; ++ i)
printf("%d %d\n", e[sol[i]].x, e[sol[i]].y);
return 0;
}