Pagini recente » Cod sursa (job #1552950) | Cod sursa (job #1886592) | Cod sursa (job #3275979) | Cod sursa (job #2326547) | Cod sursa (job #573684)
Cod sursa(job #573684)
#include<fstream>
#include<algorithm>
#include<vector>
#define dmax 200003
using namespace std;
ifstream in("lazy.in");
ofstream out("lazy.out");
int n,m,f[dmax],h[dmax];
struct muchie
{
int a;
int b;
long long c;
long long d;
int nr;
} mc[dmax];
int sf(muchie x, muchie y)
{
if(x.c != y.c)
return x.c < y.c;
return x.d > y.d;
}
int bun(int x, int y)
{
while(f[x])
x = f[x];
while(f[y])
y = f[y];
if(x == y)
return 0;
return 1;
}
void uneste(int x, int y)
{
while(f[x])
x = f[x];
while(f[y])
y = f[y];
if(h[x] < h[y])
f[x] = y;
if(h[x] > h[y])
f[y] = x;
if(h[x] == h[y])
{ f[y] = x;
h[x]++;
}
}
void kruskal()
{
int k=0,i;
for(i=1; k<n-1; i++)
if(bun(mc[i].a, mc[i].b) )
{ out<<mc[i].nr<<'\n';
k++;
uneste(mc[i].a, mc[i].b);
}
}
int main()
{
int i;
in>>n>>m;
for(i=1;i<=m;i++)
{ in>>mc[i].a>>mc[i].b>>mc[i].c>>mc[i].d;
mc[i].nr = i;
}
in.close();
sort( mc+1, mc+m+1, sf);
kruskal();
out.close();
return 0;
}