Codeforces Round #441 Div. 1 F. Royal Questions
来源:互联网 发布:a字裙淘宝 编辑:程序博客网 时间:2024/06/03 21:32
链接
http://codeforces.com/contest/875/problem/F
题意
给n个白点,m个黑点,其中第i个黑点和两个白点有边权为
思路
贪心,之前自己写了一个按边贪心的,结果很容易就wa了 QAQ,看了题解发现要按点来贪心。
给黑点按权值从大到小排序,遍历每个黑点,一个黑点会和两个白点连边,不考虑这个黑点和哪个白点连边,而是把这两个白点缩成一个点,这样这个黑点就一定从两个白点中拿走了一个点,留下了一个点。如果拿到某个黑点只与一个缩点后的白点连边,那么这个黑点只能和这个白点连边,把这个白点标记为不能和其它黑点连边了就好。
代码
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int N = 2e5 + 5;struct Node { int u, v, w; bool operator<(const Node &r) { return w > r.w; }};int n, m;int pa[N];bool full[N];Node nodes[N];int findset(int x) { if (pa[x] == x) return x; pa[x] = findset(pa[x]); return pa[x];}int main() { while (~scanf("%d%d", &n, &m)) { for (int i = 0; i < m; ++i) scanf("%d%d%d", &nodes[i].u, &nodes[i].v, &nodes[i].w); sort(nodes, nodes + m); for (int i = 1; i <= n; ++i) pa[i] = i; for (int i = 1; i <= n; ++i) full[i] = false; int ans = 0, u, v; for (int i = 0; i < m; ++i) { u = findset(nodes[i].u); v = findset(nodes[i].v); if (u == v && !full[u]) { ans += nodes[i].w; full[u] = true; } else if (u != v && (!full[u] || !full[v])) { ans += nodes[i].w; pa[u] = v; full[v] |= full[u]; } } printf("%d\n", ans); }}
阅读全文
0 0
- Codeforces Round #441 Div. 1 F. Royal Questions
- Codeforces Round #441 (Div. 1, by Moscow Team Olympiad) F. Royal Questions
- Codeforces Round #441 Div. 2 E,F
- Codeforces Round #145 (Div. 1) 240F TorCoder
- Codeforces Round #277.5 (Div. 2) F
- Codeforces Round #279 (Div. 2) F
- Codeforces Round #279 (Div. 2) F
- Codeforces Round #279 (Div. 2) B F
- Codeforces Round #277.5 (Div. 2)F题
- Codeforces Round #322 (Div. 2) F
- Codeforces Round #377 (Div. 2) F
- Codeforces Round #386 (Div. 2) F
- Codeforces Round #442 (Div. 2) 877 F
- Codeforces Round #293 (Div. 2) E - Arthur and Questions
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) F
- Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) F
- [ 杂题 ] Codeforces875F Royal Questions
- 【Codeforces875F】Royal Questions
- 关于数据去重(distinct)
- PHP循环遍历数组的3种方法list()、each()和while总结
- Openshift 简介
- sql 语句查询今天、昨天、近7天、近30天、一个月内、上一月 数据
- Android Studio如何在模拟机中添加文件
- Codeforces Round #441 Div. 1 F. Royal Questions
- linux上tomcat服务器更新项目的步骤
- 安装pip
- Async Await初探
- pjax实现页面局部刷新跳转
- Hibernate关系映射级别注解(多对一单向外键关联)
- ConfigReader(四十九)—— ReadSkillCameraConfig
- hdoj 4825 Xor Sum
- [linux-shell]shell的case使用