Cod sursa(job #2278513)

Utilizator bogdanmarin69Bogdan Marin bogdanmarin69 Data 8 noiembrie 2018 09:50:50
Problema Arbore partial de cost minim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.29 kb
#include <fstream>
#include <vector>
using namespace std;
class Reader{
    public:
        Reader() {}
        Reader(const char *file_name){
            input_file.open(file_name,std::ios::in | std::ios::binary);
            input_file.sync_with_stdio(false);
            index&=0;
            input_file.read(buffer,SIZE);}
        inline Reader &operator >>(int &n){
            for (;buffer[index]<'0' or buffer[index]>'9';inc());
            n&=0;
            sign&=0;
            sign|=(buffer[index-1]=='-');
            for (;'0'<=buffer[index] and buffer[index]<='9';inc())
                n=(n<<1)+(n<<3)+buffer[index]-'0';
            n^=((n^-n)&-sign);
            return *this;}
        ~Reader(){
            input_file.close();}
    private:
        std::fstream input_file;
        static const int SIZE=0x400000;
        char buffer[SIZE];
        int index,sign;
        inline void inc(){
            if(++index==SIZE)
                index=0,input_file.read(buffer,SIZE);}
};

class Writer{
    public:
        Writer() {};
        Writer(const char *file_name){
            output_file.open(file_name,ios::out | ios::binary);
            output_file.sync_with_stdio(false);
            index=0;}
        inline Writer &operator <<(int target){
            aux=0;
            n=target;
            if (target<0)
                sign=-1;
            else
                sign=1;
            if (!n)
                nr[aux++]='0';
            for (;n;n/=10)
                nr[aux++]=sign*(n%10)+'0';
            if (sign==-1){
                buffer[index]='-';inc();}
            for(;aux;inc())
                buffer[index]=nr[--aux];
            return *this;}
        inline Writer &operator <<(const char *target){
            aux=0;
            while (target[aux])
                buffer[index]=target[aux++],inc();
            return *this;}
        ~Writer(){
            output_file.write(buffer,index);output_file.close();}
    private:
        fstream output_file;
        static const int SIZE=0x200000;
        int index=0,aux,n,sign;
        char buffer[SIZE],nr[24];
        inline void inc(){
            if(++index==SIZE)
                index=0,output_file.write(buffer,SIZE);}
};

Reader cin ("apm.in");
Writer cout ("apm.out");

const int MAX = 2e5 + 1, DELTA = 1000;
int n, m, tata[MAX], cost;
vector<pair<int, int> > lst[2 * DELTA + 1], sol;
int find_tata(int x) {
    if(tata[x] == x) {
        return x;
    }
    int ans = find_tata(tata[x]);
    tata[x] = ans;
    return ans;
}
int main()
{
    ios::sync_with_stdio(false);
    cin >> n >> m;
    for(int i = 1; i <= m; ++i) {
        int u, v, w;
        cin >> u >> v >> w;
        lst[w + DELTA].push_back({u, v});
    }
    for(int i = 1; i <= n; ++i) {
        tata[i] = i;
    }
    for(int i = 0; i <= 2 * DELTA; ++i) {
        for(auto it : lst[i]) {
            int tu = find_tata(it.first);
            int tv = find_tata(it.second);
            if(tu != tv) {
                cost = cost + i - DELTA;
                sol.push_back(it);
                tata[tu] = tv;
            }
        }
    }
    cout << cost << "\n" << n - 1 << "\n";
    for(auto it : sol) {
        cout << it.first << " " << it.second << "\n";
    }
    return 0;
}