Cod sursa(job #1711183)

Utilizator PopoviciRobertPopovici Robert PopoviciRobert Data 30 mai 2016 19:34:19
Problema Politie Scor 100
Compilator cpp Status done
Runda Arhiva ICPC Marime 1.51 kb
#include <cstdio>
#include <algorithm>
#include <set>
using namespace std;
#define MAXM 500000
#define MAXN 250000
#define MAXBUF 4000000
FILE*fi,*fout;
set <int> S;
struct Muchie{
    int x,y,tip,grad;
};
Muchie V[MAXM];
int sef[MAXM+1];
bool cmp(Muchie a,Muchie b){
    if(a.tip==b.tip)
       return a.grad<b.grad;
    return a.tip<b.tip;
}
int find_sef(int nod){
     if(sef[nod]==0)
        return nod;
     return sef[nod]=find_sef(sef[nod]);
}
int G[MAXN+1],pos=0;
char buf[MAXBUF];
inline char nextch(){
    if(pos==MAXBUF){
        fread(buf,1,MAXBUF,fi);
        pos=0;
    }
    return buf[pos++];
}
inline int getnr(){
    char a=nextch();
    while(a<'0'||a>'9')
       a=nextch();
    int nr=0;
    while(a>='0'&&a<='9'){
        nr=nr*10+a-'0';
        a=nextch();
    }
    return nr;
}
int main(){
   int n,m,d,p,i,con;
   fi=fopen("politie.in" ,"r");
   fout=fopen("politie.out" ,"w");
   n=getnr();
   m=getnr();
   d=getnr();
   p=getnr();
   for(i=0;i<m;i++){
      V[i].x=getnr();
      V[i].y=getnr();
      V[i].tip=getnr();
      V[i].grad=getnr();
   }
   sort(V,V+m,cmp);
   con=i=0;
   while(con<n-1){
       if(find_sef(V[i].x)!=find_sef(V[i].y)){
           G[con]=V[i].grad;
           con++;
           sef[find_sef(V[i].x)]=find_sef(V[i].y);
       }
       i++;
   }
   sort(G,G+con);
   i=con-1;
   con=0;
   while(con<p){
      if(S.find(G[i])==S.end()){
          fprintf(fout,"%d\n" ,G[i]);
          S.insert(G[i]);
          con++;
      }
      i--;
   }
   fclose(fi);
   fclose(fout);
   return 0;
}