赛后更新
没写出来,不更新了。。。
更新:sw算法,蜜汁T,可能姿势不够骚
更新:这是个假题。。。神tm的并查集。。。。题意是求让至少一个点脱离联通块至少需要的代价。。。。。判断联通输出边权和最小的一个点,如果不联通输出0
AC代码:
#include "iostream" #include "string.h" #include "stack" #include "queue" #include "string" #include "vector" #include "set" #include "map" #include "algorithm" #include "stdio.h" #include "math.h" #pragma comment(linker, "/STACK:102400000,102400000") #define ll long long #define endl (" ") #define bug(x) cout<<x<<" "<<"UUUUU"<<endl; #define mem(a,x) memset(a,x,sizeof(a)) #define mp(x,y) make_pair(x,y) #define pb(x) push_back(x) #define ft (frist) #define sd (second) #define lrt (rt<<1) #define rrt (rt<<1|1) using namespace std; const long long INF = 1e18+1LL; const int inf = 1e9+1e8; const int N=1e5+100; const ll mod=1e9+7; int pre[N],n,m,wi[N]; void init(int n){ for(int i=0; i<=n; ++i) pre[i]=i; } int finds(int x){ return pre[x]==x?x:finds(pre[x]); } void unions(int x, int y){ int fx=finds(x), fy=finds(y); pre[fy]=fx; } int main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); while(cin>>n>>m){ init(n);mem(wi,0); for(int i=1; i<=m; ++i){ int u,v,w; cin>>u>>v>>w; if(u==v) continue; wi[u]+=w,wi[v]+=w; unions(u,v); } sort(wi+1,wi+1+n); int flag=0; for(int i=1; i<=n; ++i){ if(finds(i)==i) flag++; } if(flag==1) cout<<wi[1]<<endl; else cout<<"0 "; } return 0; }