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.
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;
}
- codeforces—Royal Questions(并查集维护最大基环外向树)
- [最小基环生成树] Codeforces875F .Royal Questions
- Codeforces Round #441 Div. 1 F. Royal Questions
- ZOJ 3649 Social Net 最大生成树 + 并查集维护
- Codeforces Round #376 (Div. 2) C. Socks (并查集维护)
- 【复赛模拟试题】指认坏人 基环外向树+最大独立集
- [ 杂题 ] Codeforces875F Royal Questions
- 【Codeforces875F】Royal Questions
- 基环外向树dp
- Codeforces Round #441 (Div. 1, by Moscow Team Olympiad) F. Royal Questions
- CodeForces 788B 浅谈并查集维护奇妙欧拉回路
- HDU 3367 Pseudoforest(最大生成树+并查集)
- 并查集&最小(最大)生成树
- 最大生成树&并查集练习
- bzoj4569 [Scoi2016]萌萌哒 (st表 维护 并查集)
- CodeForces 455C Civilization(并查集+树直径)
- 二维并查集——CodeForces
- codeforces 722C (并查集)
- leetcode 183. Customers Who Never Order(SQL,嵌套查询)38
- ORA-00443: background process "MMNL" did not start
- #include<stdio.h>
- Noip 提高组 2013 Day1 T3 货车运输 Kruskal+倍增
- [分块]51 Nod——1225 余数之和
- codeforces—Royal Questions(并查集维护最大基环外向树)
- HDOJ2602(01背包模板题 一维AC+二维TL)
- 如何正确删除VMware上面创建的虚拟机?
- 关于If的编程用法
- Android中的自绘View的那些事儿(一)
- 171020 C++学习笔记-6.1
- 从0开始入门机器学习|pycharm主题皮肤设置
- 接口只用于定义类型。
- Eclipse中堆内存的设置