CF444E. DZY Loves Planting

题目链接

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); 
} 

转载于:https://www.cnblogs.com/sssy/p/9866131.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Process: com.example.dzy, PID: 26008 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dzy/com.example.dzy.NavigationActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference at com.example.dzy.Fragment_1.<init>(Fragment_1.java:44) at com.example.dzy.NavigationActivity.initTab(NavigationActivity.java:39) at com.example.dzy.NavigationActivity.onCreate(NavigationActivity.java:27) at android.app.Activity.performCreate(Activity.java:7802) at android.app.Activity.performCreate(Activity.java:7791) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
06-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值