Cod sursa(job #2085418)

Utilizator bleo16783FMI Bleotiu Cristian bleo16783 Data 10 decembrie 2017 10:37:01
Problema Arbore partial de cost minim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.16 kb
#include <fstream>
#include<algorithm>
#include<iostream>
using namespace std;
#define N 200200
#define s second
#define f first
#define mp make_pair
ifstream F("apm.in");
ofstream G("apm.out");
int S,I;
const int D=89999;
int O[N],d,cn;
char p[D+1],w[D+2];
inline void L()
{
    w[I]='\n';
    if(++I>D)I=0,G<<w;
}
inline void R(int&x)
{
    x=0;
    S=1;
    while(p[I]<40)if(++I>D)I=0,F.read(p,D+1);
    if(p[I]<47)
    {
        S=-1;
        if(++I>D)I=0,F.read(p,D+1);
    }
    while(p[I]>47)
    {
        x=x*10+p[I]-48;
        if(++I>D)I=0,F.read(p,D+1);
    }
    x*=S;
}
int t[10]= {1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000};
int c,i;
inline void W(int x)
{
    for(i=4; i>-1; --i)
    {
        c=x/t[i],x-=c*t[i],w[I]=48+c;
        if(++I>D)I=0,G<<w;
    }
    w[I]=' ';
    if(++I>D)I=0,G<<w;
}
inline void E(int x)
{
    if(x<0)
    {
        w[I]='-';
        if(++I>D)I=0,G<<w;
        x*=-1;
    }
    if(x>t[3]-1)for(i=4; x>t[i]-1; ++i);
    else for(i=0; x>t[i]-1; ++i);
    --i;
    for(; i>-1; --i)
    {
        c=x/t[i],x-=c*t[i],w[I]=48+c;
        if(++I>D)I=0,G<<w;
    }
    w[I++]='\n';
}
inline void T()
{
    w[I]=0;
    G<<w;
}
int x,y,z,n,m,j,l,g[N],A,o=-1,h[N],s[2*N];
inline void Y(int&x)
{
    while(x>g[x])z=g[g[x]],g[x]=z,x=z;
}
pair<int,pair<int,int> >v[2*N];
int main()
{
    //F.sync_with_stdio(false);
    //G.sync_with_stdio(false);
    R(n);
    /*cn=0;d=10;
    while(d<n)
    {
        for(j=d/10;j<d;++j)O[j]=cn;
        d*=10;
        ++cn;
    }
    for(j=d/10;j<n;++j)O[j]=cn;
    O[n]=cn;*/
    R(m);
    j=0;
    for(; j<m; ++j)R(v[j].s.f),R(v[j].s.s),R(v[j].f);
    ++n;
    for(j=1; j<n; ++j)h[j]=1,g[j]=j;
    --n;
    sort(v,v+m);
    for(j=0; h[1]<n; ++j)
    {
        x=v[j].s.f;
        y=v[j].s.s;
        Y(x);
        Y(y);
        if(x!=y)
        {
            s[++o]=j;
            A+=v[j].f;
            if(x>y)h[y]+=h[x],h[x]=0,g[x]=y;
            else h[x]+=h[y],h[y]=0,g[y]=x;
        }
    }
    I=0;
    E(A);
    W(++o);
    L();
    for(j=0; j<o; ++j)W(v[s[j]].s.f),W(v[s[j]].s.s),L();
    T();
}