#include<iostream>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#define MAXN 1000010
#define INF 10000010
using namespace std;
inline int read(){
int f = 1, x = 0;char ch = getchar();
while (ch > '9' || ch < '0'){if (ch == '-')f = -f;ch = getchar();}
while (ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}
return x * f;
}
struct Edge{
int to;
int next;
int w;
}edge[MAXN];
int n;
int m;
int head[MAXN];
int cnt=0;
int dis[MAXN];
int ans;
int visit[MAXN];
inline void add(int u,int v,int w){
cnt++;
edge[cnt].to=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt;
}
bool prim(){
dis[1]=0;
for(int i=1;i<=n;i++){
int u=-1;
int min=INF;
for(int j=1;j<=n;j++){
if(min>dis[j]&&!visit[j]){
u=j;
min=dis[j];
}
}
if(u==-1){
return false;
}
visit[u]=true;
ans+=dis[u];
for(int j=head[u];j;j=edge[j].next ){
int t=edge[j].to ;
if(!visit[t]&&dis[t]>edge[j].w) dis[t]=edge[j].w ;
}
}
return true;
}
int main(){
n=read();
fill(dis,dis+n+1,INF);
m=read();
for(int i=1;i<=m;i++){
int u,v,w;
u=read();
v=read();
w=read();
add(u,v,w);
add(v,u,w);
}
bool u=prim();
if(!u){
cout<<"orz"<<endl;
return 0;
}
cout<<ans<<endl;
return 0;
}
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#define MAXN 1000010
#define INF 10000010
using namespace std;
inline int read(){
int f = 1, x = 0;char ch = getchar();
while (ch > '9' || ch < '0'){if (ch == '-')f = -f;ch = getchar();}
while (ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}
return x * f;
}
struct Edge{
int to;
int next;
int w;
}edge[MAXN];
int n;
int m;
int head[MAXN];
int cnt=0;
int dis[MAXN];
int ans;
int visit[MAXN];
inline void add(int u,int v,int w){
cnt++;
edge[cnt].to=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt;
}
bool prim(){
dis[1]=0;
for(int i=1;i<=n;i++){
int u=-1;
int min=INF;
for(int j=1;j<=n;j++){
if(min>dis[j]&&!visit[j]){
u=j;
min=dis[j];
}
}
if(u==-1){
return false;
}
visit[u]=true;
ans+=dis[u];
for(int j=head[u];j;j=edge[j].next ){
int t=edge[j].to ;
if(!visit[t]&&dis[t]>edge[j].w) dis[t]=edge[j].w ;
}
}
return true;
}
int main(){
n=read();
fill(dis,dis+n+1,INF);
m=read();
for(int i=1;i<=m;i++){
int u,v,w;
u=read();
v=read();
w=read();
add(u,v,w);
add(v,u,w);
}
bool u=prim();
if(!u){
cout<<"orz"<<endl;
return 0;
}
cout<<ans<<endl;
return 0;
}
链式向前星