码迷,mamicode.com
首页 > 其他好文 > 详细

CF444E. DZY Loves Planting

时间:2018-10-28 18:05:58      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:amp   tin   opera   print   pre   [1]   http   putchar   cti   

题目链接

CF444E. DZY Loves Planting

题解

可以..二分网络流
可是
考虑边从小到大排序
考虑每条边能否成为答案
用并查集维护节点之间的联通性
对于一条边来说,如果这条边可以成为答案
那么当前已经合并的每个点,都需要给它分配一个未被合并的点
这就很好判定了

代码

#include<ctime> 
#include<queue> 
#include<cstdio> 
#include<vector> 
#include<cstring> 
#include<algorithm> 
#define rep(a,b,c) for(int a = b;a <= c;++ a)
#define per(a,b,c) for(int a = b;a >= c;-- a) 
#define gc getchar()
#define pc putchar
using namespace std; 
inline int read() { 
    int x = 0,f = 1; 
    char c = gc; 
    while(c < '0' || c > '9') { if(c == '-')f = - 1 ;c = gc; } 
    while(c <= '9' && c >= '0') x = x * 10 + c - '0',c = gc; 
    return x * f; 
} 
#define LL long long
void print(int x) {
    if(x >= 10) print(x / 10); 
    pc(x % 10 + '0'); 
} 
const int maxn = 100007; 
struct node { 
    int u,v,w; 
    bool operator < (const node&p)const { 
    return w < p.w; 
    }   
 
} e[maxn << 1]; 
int a[maxn],sum = 0,sz[maxn],fa[maxn]; 
bool judge = true; 
int find(int x) { 
    if(fa[x] != x) fa[x] = find(fa[x]); 
    return fa[x]; 
} 
void merge(int x,int y) { 
    x = find(x),y = find(y); 
    sz[x] += sz[y]; 
    a[x] += a[y]; 
    fa[y] = x; 
    if(sz[x] > sum - a[x]) judge = 0; 
} 
int main() { 
    int n = read();  
    for(int i = 1;i < n;++ i) e[i].u = read(),e[i].v = read(),e[i].w=read(); 
    std::sort(e + 1,e + n); 
    for(int i = 1;i <= n;++ i) 
        a[i] = read(),sz[i] = 1,sum += a[i],fa[i] = i; 
    int ans = 0; 
    if(n == 1 && a[1] == 1) while(1){}; 
    for(int i = 1;i < n;++ i) { 
        if(judge) ans = e[i].w; 
        merge(e[i].u,e[i].v); 
    } 
    print(ans); 
} 

CF444E. DZY Loves Planting

标签:amp   tin   opera   print   pre   [1]   http   putchar   cti   

原文地址:https://www.cnblogs.com/sssy/p/9866131.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!