Pagini recente » Cod sursa (job #1559667) | Cod sursa (job #2519396) | Cod sursa (job #552800) | Cod sursa (job #1630884) | Cod sursa (job #783370)
Cod sursa(job #783370)
#include<fstream>
#include<algorithm>
using namespace std;
ifstream f("lazy.in");
ofstream g("lazy.out");
int N,M,i,j,nr,gx,gy;
int x[200002],y[200002],ind[200002],group[200002];
long long c1[200002],c2[200002];
struct comp
{bool operator()(const int &a, const int &b)const
{if(c1[a]==c1[b])
return(c2[a]>c2[b]);
else
return(c1[a]<c1[b]);}
};
inline int group_name(int s)
{if(group[s]==s)
return s;
group[s]=group_name(group[s]);
return group[s];
}
int unite_groups(int a, int b)
{group[group_name(b)]=group_name(a);}
int main()
{f>>N>>M;
for(i=1; i<=M; i++)
{f>>x[i]>>y[i]>>c1[i]>>c2[i];
ind[i]=i;}
for(i=1; i<=N; i++)
group[i]=i;
sort(ind+1,ind+M+1,comp());
for(i=1; i<=M; i++)
if(nr<N-1)
{gx=x[ind[i]]; gy=y[ind[i]];
if(group_name(gx)!=group_name(gy))
{unite_groups(gx,gy);
nr++;
g<<ind[i]<<endl;}
}
else
return 0;
return 0;}