CF444E. DZY Loves Planting

题目链接

CF444E. DZY Loves Planting

题解

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

代码

#include 
#include 
#include 
#include 
#include 
#include 
#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)