Cod sursa(job #1710417)

Utilizator alexpetrescuAlexandru Petrescu alexpetrescu Data 28 mai 2016 22:15:11
Problema Politie Scor 100
Compilator cpp Status done
Runda Arhiva ICPC Marime 1.14 kb
#include <cstdio>
#include <algorithm>
#define MAXN 250000
#define MAXM 500000
struct mycreation{
    int x, y, a, b;
}v[MAXM+1];
int t[MAXN+1], ans[MAXN+1];
int find(int x){
    if(t[x]==x) return x;
    else return t[x]=find(t[x]);
}
bool cmp(const mycreation p, const mycreation q){
    if(p.a==q.a) return p.b<q.b;
    else return p.a<q.a;
}
int main(){
    int n, m, r, e, i, last, j;
    FILE *fin, *fout;
    fin=fopen("politie.in", "r");
    fout=fopen("politie.out", "w");
    fscanf(fin, "%d%d%d%d", &n, &m, &r, &e);
    for(i=1; i<=n; i++){
        t[i]=i;
    }
    for(i=1; i<=m; i++){
        fscanf(fin, "%d%d%d%d", &v[i].x, &v[i].y, &v[i].a, &v[i].b);
    }
    std::sort(v+1, v+m+1, cmp);
    for(i=1; i<=m; i++){
        if(find(v[i].x)!=find(v[i].y)){
            ans[++ans[0]]=v[i].b;
            t[find(v[i].x)]=find(v[i].y);
        }
    }
    std::sort(ans+1, ans+ans[0]+1);
    last=0;
    for(i=ans[0], j=0; j<e; i--){
        if(last!=ans[i]){
            last=ans[i];
            fprintf(fout, "%d\n", ans[i]);
            j++;
        }
    }
    fclose(fin);
    fclose(fout);
    return 0;
}