CF444E. DZY Loves Planting

題目連接

CF444E. DZY Loves Plantingnode

題解

能夠..二分網絡流
但是
考慮邊從小到大排序
考慮每條邊可否成爲答案
用並查集維護節點之間的聯通性
對於一條邊來講,若是這條邊能夠成爲答案
那麼當前已經合併的每一個點,都須要給它分配一個未被合併的點
這就很好斷定了網絡

代碼

#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); 
}
相關文章
相關標籤/搜索