Cod sursa(job #1566541)

Utilizator justsomedudePalade Thomas-Emanuel justsomedude Data 12 ianuarie 2016 11:43:40
Problema Lazy Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.42 kb
#include<iostream>
#include<fstream>
#include<algorithm>
#define nmax 200009
using namespace std;
 
ifstream fin ("lazy.in");
ofstream fout ("lazy.out");
 
struct lista{
    int x, y, i;
    long long int c1, c2;
};
 
lista a[nmax];
int n, m, t[nmax], c[nmax];
 
inline bool cmp(const lista nr1, const lista nr2)
{
   if (nr1.c1 ==  nr2.c1)
       return (nr1.c2 > nr1.c2);
   return (nr1.c1 < nr2.c1);
}
 
int Find(int x)
{
   int y, z;
   y = x;
 
   while (t[x]!=0)
      x = t[x];
 
   while (t[y]!=0)
   {
      z = t[y];
      t[y] = x;
      y = z;
   }
   return x;
}
 
void Union(int x, int y)
{
   if (c[x] > c[y])
   {
       t[y] = x;
       c[y] = 0;
       c[x] += c[y];
   }
   else
   {
       t[x] = y;
       c[x] = 0;
       c[y] += c[x];
   }
}
 
void Citire()
{
  int c1, c2, x, y;
  lista q;
  fin >> n >> m;
  for (int i=1; i<=m; i++)
  {
    fin >> x >> y >> c1 >> c2;
    q.x = x;
    q.y = y;
    q.c1 = c1;
    q.c2 = c2;
    q.i  = i;
    a[i] = q;
  }
}
 
void Rezolva()
{
   int i, j, x, y;
   sort (a+1, a+m+1, cmp);
   for (i=1; i<=n; i++)
      { t[i] = 0; c[i] = 1; }
 
   for (i=1; i<=m; i++)
   {
      x = Find(a[i].x); y = Find(a[i].y);
      if (x != y)
      {
          fout << a[i].i << "\n";
          Union(x,y);
      }
   }
}
 
int main ()
{
  Citire();
  Rezolva();
  fin.close();
  fout.close();
  return 0;
}