codeforces—Royal Questions(并查集维护最大基环外向树)

来源:互联网 发布:经知考试网网址 编辑:程序博客网 时间:2024/06/05 19:58

题面:

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 hisn 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 withm unmarried princesses. Receiving guests, Karl learned that the dowry of thei 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.

思维过程:将题意简化为给n个点,m条边,边上有权,求最大基环外向树。

defination of 基环外向树:n点n边,每点入度都为1。

题解:将边权按从大到小排序,并用并查集维护基环外向树。从大到小处理边,并用一个mark数组表示该店的入度(并查集维护保证先对子结点选择)。

代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn=200000+10;
struct edge{
    int u,v,w;
};
bool cmp(edge& a,edge& b){
    return a.w>b.w;
}
int fa[maxn];
bool mark[maxn];
void init(int n){
    for (int i=1; i<=n; i++) {
        fa[i]=i;
        mark[i]=0;
    }
}
int find(int x){
    return fa[x]==x?x:fa[x]=find(fa[x]);
}
edge e[maxn];
int main(){
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
    }
    init(n);
    sort(e+1,e+m+1,cmp);
    long long sum=0;
    for (int i=1;i<=m;i++) {
        int x,y;
        x=find(e[i].u);y=find(e[i].v);
        if(mark[x]&&mark[y])continue;
        else{
            if (x==y) {
                mark[x]=1;
            }
            else {
                fa[x]=y;
                mark[y]|=mark[x];
            }
        }
      sum+=e[i].w;
    }
    cout<<sum<<endl;
    return 0;
}

阅读全文
0 0
原创粉丝点击