hoj 13792 Model Railroad

// 想了半天才发现是道生成树 然后想了好久 出了好多数据都他吗过了 但是a不了 后来发现给定数据 有的就不能构成一棵树 这点没有特判 如果我代码写的好一些 吧生成树写成函数 就他妈规避了这个问题草
#include <iostream> #include <algorithm> #include <stdio.h> #include <queue> #include <string.h> #include <vector> #include <map> #include <math.h> #define LL long long #define INF 2100000000 #define fi first #define se second #define PI 3.1415927 #define lowbit(x) (x&(-x)) using namespace std; const int maxn=(int)5e4 +30; const int MOD=(int)1e9+7; template<class T>inline void MAX(T &a,T b) { if(a<b)a=b; } template<class T>inline void MIN(T &a,T b) { if(a>b)a=b; } struct nod { int x,y,val; bool f; // nod(int x,int y,int val,int f):x(x),y(y),val(val),f(f){} bool operator <(const nod&a )const { return val<a.val; }; }; nod mp[250010]; int f[maxn]; int found(int x) { return x==f[x]? f[x]:f[x]=found(f[x]); } int n,m,l; int main() { #ifdef shuaishuai freopen("C:\Users\hasee\Desktop\a.txt","r",stdin); // freopen("C:\Users\hasee\Desktop\b.txt","w",stdout); // freopen("C:\Users\hasee\Desktop\picture.in","r",stdin); #endif scanf("%d%d%d",&n,&m,&l); LL support=0LL; for(int i=0; i<l; i++) { scanf("%d%d%d",&mp[i].x,&mp[i].y,&mp[i].val); support+=(LL)mp[i].val; mp[i].f=true; } for(int i=l; i<m; i++) { scanf("%d%d%d",&mp[i].x,&mp[i].y,&mp[i].val); mp[i].f=false; } sort(mp,mp+m); int cnt=1; LL need=0LL; for(int i=0; i<=n; i++)f[i]=i; for(int i=0; i<m; i++) { int x=mp[i].x,y=mp[i].y; int fx=found(x),fy=found(y); if(fx!=fy) { // printf("%d %d %d ",mp[i].x,mp[i].y,mp[i].val); f[fx]=fy; cnt++; if(mp[i].f) support-=(LL)mp[i].val; else need+=(LL)mp[i].val; if(cnt==n) break; } } //for(int i=1;i<=n;i++)printf("%d ",f[i]);printf(" "); if(support>=need&&m!=0&&cnt==n)puts("possible"); else puts("impossible"); return 0; }
原文地址:https://www.cnblogs.com/MeowMeowMeow/p/7277032.html