Pagini recente » Cod sursa (job #365289) | Cod sursa (job #2620203) | Cod sursa (job #2365073) | Cod sursa (job #3258922) | Cod sursa (job #2781935)
#include <iostream>
#include <fstream>
#include <set>
#include <algorithm>
using namespace std;
ifstream fin("politie.in");
ofstream fout("politie.out");
int n,m;
set<int>sol;
struct muchie
{
int x,y;
int t,c;
};
muchie a[400001];
int t[200001],h[200001],r[200001];
int Find(int x)
{
while(t[x]!=0)
x=t[x];
return x;
}
void Union(int x,int y)
{
if(h[x]>h[y])
t[y]=x;
else
{
t[x]=y;
if(h[x]==h[y])
h[y]++;
}
}
inline int cmp(muchie b1,muchie b2)
{
if(b1.t<b2.t)
return 1;
if(b1.t==b2.t and b1.c < b2.c)
return 1;
return 0;
}
int main()
{
int i,nr=0,v1,v2,x1,x2,ct=0,s=0,d,p;
fin>>n>>m >> d >> p;
for(i=1;i<=m;i++)
fin>>a[i].x>>a[i].y>>a[i].t>>a[i].c;
sort(a+1,a+m+1,cmp);
for(i=1;i<=n;i++)
h[i]=1;
i=1;
ct=0;
nr=0;
int bagat = 0;
while(nr<n-1)
{
v1=a[i].x;
v2=a[i].y;
x1=Find(v1);
x2=Find(v2);
if(x1!=x2)
{
nr++;
ct++;
ct++;
if(sol.size() < p)
sol.insert(-a[i].c);
Union(x1,x2);
}
i++;
}
i=1;
for ( auto it : sol) {
fout << -it << "\n";
}
return 0;
}