Pagini recente » Cod sursa (job #319000) | Cod sursa (job #1304286) | Cod sursa (job #631522) | Cod sursa (job #3201959) | Cod sursa (job #761902)
Cod sursa(job #761902)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
#define nmax 200004
#define ll long long
int n, m, rez[nmax];
typedef struct{
int x, y, poz;
ll c1, c2;
}camp;
camp v[nmax];
int t[nmax];
string s;
int poz;
ifstream f("lazy.in");
ofstream g("lazy.out");
bool cmp(camp a, camp b){
if (a.c1 == b.c1){
return a.c2 > b.c2;
}
return a.c1 < b.c1;
}
void citeste_buf(int &nr){
for(; (s[poz]<'0' || s[poz]>'9')&&s[poz]!='-'; poz++);
int semn = 1;
if (s[poz] == '-') semn = -1, ++poz;
for(; s[poz]>='0'&&s[poz]<='9';poz++){
nr = nr * 10 + (s[poz]-'0');
}
nr = nr * semn;
}
void citeste_buf2(ll &nr){
for(; (s[poz]<'0' || s[poz]>'9')&&s[poz]!='-'; poz++);
int semn = 1;
if (s[poz] == '-') semn = -1, ++poz;
for(; s[poz]>='0'&&s[poz]<='9';poz++){
nr = nr * 10 + (s[poz]-'0');
}
nr = nr * semn;
}
void citeste(){
//f >> n >> m;
getline(f,s);
poz = 0;
citeste_buf(n);
citeste_buf(m);
for(int i=1; i<=m; i++){
//f >> v[i].x >> v[i].y >> v[i].c1 >> v[i].c2;
getline(f,s);
poz = 0;
citeste_buf(v[i].x);
citeste_buf(v[i].y);
citeste_buf2(v[i].c1);
citeste_buf2(v[i].c2);
//scanf("%d%d%lld%lld\n", &v[i].x, &v[i].y, &v[i].c1, &v[i].c2);
v[i].poz = i;
}
sort(v+1, v+m+1, cmp);
}
inline int radacina(int x){
int cpy = x;
for(;t[x]!=0;x=t[x]);
for(;cpy!=x;){
int temp = cpy;
t[cpy] = x;
cpy = t[temp];
}
return x;
}
void uneste(int x, int y){
t[x] = y;
}
void rezolva(){
for(int i=1; i<=m; i++){
int X = radacina(v[i].x);
int Y = radacina(v[i].y);
if (X == Y) continue;
uneste(X,Y);
rez[++rez[0]] = v[i].poz;
}
for(int i=1; i<n; i++) g << rez[i] << "\n";
}
int main(){
citeste();
rezolva();
f.close();
g.close();
return 0;
}