20分求助
查看原帖
20分求助
531930
Southern_Dynasty楼主2022/7/14 12:35

RT.

#include<bits/stdc++.h>
#define gt getchar
#define pt putchar
#define y1 y233
#define int long long
typedef long long ll;
//typedef __int128 lll;
typedef unsigned long long ull;
const signed N=1e5+5;
const signed M=1e6+5; 
using namespace std;
inline int read(){
   	int x=0,f=1;char ch=gt();
   	while(!isdigit(ch)){if(ch=='-')f=-1;ch=gt();}
   	while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=gt();}
	return x*f;
}
inline void print(int x){
    if(x<0)pt('-'),x=-x;
    if(x>9)print(x/10);
    pt(x%10+48);
}
inline void println(int x){
	print(x);
	pt(10);
}
int n,m,h[N],tot,head[M<<2],cnt,ans1,ans2;
int fa[N],sz[N];
bool vis[N];
struct edge1{
	int s,t,w;
	friend bool operator<(const edge1 &a,const edge1 &b){
		if(h[a.t]!=h[b.t])return h[a.t]>h[b.t];
		return a.w<b.w;
	}
}e1[M<<2];
struct edge2{
	int to,w,nxt;
}e[M<<2];
inline void addedge(int f,int t,int w){
	e[++cnt].to=t;
	e[cnt].w=w;
	e[cnt].nxt=head[f];
	head[f]=cnt;
}
inline int find(int x){
	if(x!=fa[x])fa[x]=find(fa[x]);
	return fa[x];
}
inline void link(int x,int y){
	int xx=find(x),yy=find(y);
	if(xx==yy)return;
	if(sz[xx]>sz[yy])swap(xx,yy);
	fa[xx]=fa[yy],sz[yy]+=sz[xx];
}
inline void kruskal(){
	sort(e1+1,e1+tot+1);
	for(int i=1;i<=tot;++i){
		if(find(e1[i].s)!=find(e1[i].t)){
			link(e1[i].s,e1[i].t);
			ans2+=e1[i].w;
		}
	}
}
inline void bfs(){
	queue<int>q;
	q.push(1);
	vis[1]=ans1=1;
	while(q.size()){
		int u=q.front();
		q.pop();
		for(int i=head[u];i;i=e[i].nxt){
			int to=e[i].to;
			e1[++tot].s=u,e1[tot].t=to,e1[tot].w=e[i].w;
			if(!vis[to]){
				vis[to]=1,ans1++;
				q.push(to);
			}
		}
	}
}
signed main(){
	n=read(),m=read();
	for(int i=1;i<=n;++i)
		h[i]=read(),fa[i]=i,sz[i]=1;
	for(int i=1;i<=m;++i){
		int f=read(),t=read(),w=read();
		if(h[f]>=h[t])addedge(f,t,w);
		if(h[t]>=h[f])addedge(t,f,w);	
	}
	bfs();
	kruskal();
	printf("%d %d\n",ans1,ans2);
	return 0;
}
2022/7/14 12:35
加载中...