Pagini recente » Cod sursa (job #1270530) | Profil StefanGtz | Cod sursa (job #866069) | Cod sursa (job #388345) | Cod sursa (job #1551849)
#include <fstream>
#include <numeric>
#include <vector>
#include <algorithm>
using namespace std;
class merge_find{
vector<int> tata, adanc;
public:
merge_find(const int n): tata(n), adanc(n, 1){
iota(begin(tata), end(tata), 0); }
int find(int x){
int y = x;
for( ; y != tata[y]; y = tata[y]);
for(int tmp; x != tata[x]; tmp = tata[x], tata[x] = y, x = tmp);
return y; }
bool merge(int a, int b){
a = find(a), b = find(b);
if(a == b){
return false; }
if(adanc[a] > adanc[b]){
swap(a, b); }
if(adanc[a] == adanc[b]){
++adanc[a]; }
tata[b] = a;
return true; } };
struct muc{
int a, b, c1, c2, ind; };
bool operator<(const muc& a, const muc& b){
return (a.c1 < b.c1) || (a.c1 == b.c1 && a.c2 < b.c2); }
istream& operator>>(istream& lhs, muc& rhs){
return lhs >> rhs.a >> rhs.b >> rhs.c1 >> rhs.c2; }
int main(){
ifstream f("lazy.in");
ofstream g("lazy.out");
int n, m;
f >> n >> m;
vector<muc> muchii(m);
for(int i = 1; i <= m; ++i){
f >> muchii[i-1].a >> muchii[i-1].b >> muchii[i-1].c1 >> muchii[i-1].c2;
muchii[i-1].ind = i;
--muchii[i-1].a, --muchii[i-1].b; }
sort(begin(muchii), end(muchii));
merge_find mf(n);
for(const auto x : muchii){
if(mf.merge(x.a, x.b)){
g << x.ind << '\n'; } }
return 0; }