CodeForces - 875F Royal Questions (基环树)

最大化嫁妆:皇家婚礼配对
这是一道关于数学和图论的问题,国王需要为他的n个儿子找到嫁妆最多的m个公主进行匹配。每个公主可以选择两个王子,而每个王子只能选择一个公主。目标是最大化总嫁妆。输入包含王子和公主的数量,以及公主的嫁妆和选择的王子。输出是可能的最大嫁妆总数。问题链接和题目解析也给出了详细信息。

Royal Questions

 

In a medieval kingdom, the economic crisis is raging. Milk drops fall, Economic indicators are deteriorating every day, money from the treasury disappear. To remedy the situation, King Charles Sunnyface decided make his n sons-princes marry the brides with as big dowry as possible.

In search of candidates, the king asked neighboring kingdoms, and after a while several delegations arrived with m unmarried princesses. Receiving guests, Karl learned that the dowry of the i th princess is wi of golden coins.

Although the action takes place in the Middle Ages, progressive ideas are widespread in society, according to which no one can force a princess to marry a prince whom she does not like. Therefore, each princess has an opportunity to choose two princes, for each of which she is ready to become a wife. The princes were less fortunate, they will obey the will of their father in the matter of choosing a bride.

Knowing the value of the dowry and the preferences of each princess, Charles wants to play weddings in such a way that the total dowry of the brides of all his sons would be as great as possible. At the same time to marry all the princes or princesses is not necessary. Each prince can marry no more than one princess, and vice versa, each princess can marry no more than one prince.

Help the king to organize the marriage of his sons in the most profitable way for the treasury.

Input

The first line contains two integers nm (2 ≤ n ≤ 200 000, 1 ≤ m ≤ 200 000) — number of princes and princesses respectively.

Each of following m lines contains three integers aibiwi (1 ≤ ai, bi ≤ nai ≠ bi, 1 ≤ wi ≤ 10 000) — number of princes, which i-th princess is ready to marry and the value of her dowry.

Output

Print the only integer — the maximum number of gold coins that a king can get by playing the right weddings.

Examples

Input

2 3
1 2 5
1 2 1
2 1 10

Output

15

Input

3 2
1 2 10
3 2 20

Output

30

题目链接:http://codeforces.com/problemset/problem/875/F

题目大意:n个王子,m个公主,每个公主有两个心仪的王子a ,b,还有嫁妆w,现在国王想要通过卖子求荣获得最多的嫁妆,问最多可获得多少嫁妆。每个王子只能娶一位公主,每个公主也只能嫁一个王子,不要求所有王子公主都能匹配到。

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
#define ll long long
const int N=200005;
int n,m,fa[N],vis[N];
struct node
{
    int a,b,w;
}e[N];
bool cmp(node x,node y)
{
    return x.w>y.w;
}
int find(int x)
{
    if(fa[x]==x)return x;
    return fa[x]=find(fa[x]);
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
        scanf("%d%d%d",&e[i].a,&e[i].b,&e[i].w);
    sort(e+1,e+1+m,cmp);
    for(int i=1;i<=n;i++) fa[i]=i;
    int ans=0;
    for(int i=1;i<=m;i++)
    {
        int a=find(e[i].a),b=find(e[i].b);
        if(a!=b&&(!vis[a]||!vis[b]))
            vis[b]|=vis[a],fa[a]=b,ans+=e[i].w;
        else if(a==b&&!vis[a]) vis[a]=1,ans+=e[i].w;
    }
    printf("%d\n",ans);
    return 0;
}

 

引用\[1\]中提到了一种形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子。为了使cnt_white - cnt_black尽可能大,可以使用两次形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的链所代表的子。在第二次遍历中,需要维护一个sum变量,用于存储链所代表的子的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是链所代表的子的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的链所代表的子。在第二次遍历中,需要维护一个sum变量,用于存储链所代表的子的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值