Mai intai trebuie sa te autentifici.
Cod sursa(job #2755541)
Utilizator | Data | 27 mai 2021 16:44:30 | |
---|---|---|---|
Problema | Arbore partial de cost minim | Scor | 10 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 1.38 kb |
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
#define inf 10000001
pair <int,pair<int,int> >E[200005];
queue<pair<int,int> >Q;
struct muchie
{
int st,dr,c;
} V[200005];
int n,m,x,y,c,L[200005],nr,k,t,ctot,i,T[200005],rang[200005];
bool sel[200005];
int multime(int x)
{
if(T[x]!=x)
T[x]=multime(T[x]);
return T[x];
}
void reuniune(int x,int y)
{
x=multime(x);
y=multime(y);
if(rang[x]>rang[y])
T[y]=x;
else T[x]=y;
if(rang[x]==rang[y])
rang[x]++;
}
void prim()
{
sort(E+1,E+m+1);
for(int i=1; i<=n; i++)
{
sel[i]=false;
L[i]=i;
}
nr=0;
i=1;
while(nr<n-1)
{
x=E[i].second.first;
y=E[i].second.second;
if(L[x]!=L[y])
{nr++;
Q.push(E[i].second);
ctot+=E[i].first;
k=L[x];
t=L[y];
if(multime(x)==multime(y))L[x]=t;
}
i++;
}
}
int main()
{
f>>n>>m;
for(int i=1; i<=m; i++)
{f>>x>>y>>c;
E[i]={c,{x,y}};}
prim();
g<<ctot<<'\n';
g<<Q.size()<<'\n';
while(!Q.empty())
{g<<Q.front().second<<" "<<Q.front().first<<'\n';
Q.pop();}
return 0;
}