Codeforces 875F Royal Questions (看题解)
我还以为是什么板子题呢。。。
我们把儿子当做点, 公主当做边, 然后就是求边权值最大基环树森林。
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long
using namespace std; const int N = 2e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-; int n, m, ans, a[N], b[N], w[N], fa[N], id[N];
bool vis[N]; int getRoot(int x) {
return fa[x] == x ? x : fa[x] = getRoot(fa[x]);
} bool cmp(const int&a, const int& b) {
return w[a] > w[b];
} int main(){
scanf("%d%d", &n, &m);
for(int i = ; i <= m; i++)
scanf("%d%d%d", &a[i], &b[i], &w[i]);
for(int i = ; i <= m; i++) id[i] = i;
for(int i = ; i <= n; i++) fa[i] = i;
sort(id + , id + + m, cmp);
for(int i = ; i <= m; i++) {
int p = id[i];
int x = getRoot(a[p]);
int y = getRoot(b[p]);
if(x != y && (!vis[x] || !vis[y])) {
ans += w[p];
if(!vis[x]) fa[x] = y;
else fa[y] = x;
} else if(!vis[x]){
ans += w[p];
vis[x] = true;
}
}
printf("%d\n", ans);
return ;
}
/*
*/
Codeforces 875F Royal Questions (看题解)的更多相关文章
- Codeforces 269C Flawed Flow (看题解)
我好菜啊啊啊.. 循环以下操作 1.从队列中取出一个顶点, 把哪些没有用过的边全部用当前方向. 2.看有没有点的入度和 == 出度和, 如果有将当前的点加入队列. 现在有一个问题就是, 有没有可能队列 ...
- Codeforces 436E Cardboard Box (看题解)
Cardboard Box 贪了个半天贪不对, 我发现我根本就不会贪心. 我们先按b排序, 然后枚举选两颗心的b的最大值, 在这个之前的肯定都要选一个, 因为前面的要是一个都没选的话, 你可以把当前选 ...
- Codeforces 1045C Hyperspace Highways (看题解) 圆方树
学了一下圆方树, 好神奇的东西呀. #include<bits/stdc++.h> #define LL long long #define fi first #define se sec ...
- Codeforces 1137D Cooperative Game (看题解)
Cooperative Game 智商题, 感觉不太能推出来, 虽然看看证明过程是对的. #include<bits/stdc++.h> #define LL long long #def ...
- Codeforces 983C Elevator dp (看题解)
Elevator 怎么今天写啥题都不会写啊, 我是傻了吗.. 把电梯里面四个人的目标点当作状态, 然后暴力转移. #include<bits/stdc++.h> #define LL lo ...
- Codeforces 924D Contact ATC (看题解)
Contact ATC 我跑去列方程, 然后就gg了... 我们计每个飞机最早到达时间为L[ i ], 最晚到达时间为R[ i ], 对于面对面飞行的一对飞机, 只要他们的时间有交集则必定满足条件. ...
- Codeforces 830C Bamboo Partition (看题解)
Bamboo Partition 列公式, 整除分块, 想不到, 好菜啊. #include<bits/stdc++.h> #define LL long long #define fi ...
- Codeforces 750E New Year and Old Subsequence 线段树 + dp (看题解)
New Year and Old Subsequence 第一感觉是离线之后分治求dp, 但是感觉如果要把左边的dp值和右边的dp值合起来, 感觉很麻烦而且时间复杂度不怎么对.. 然后就gun取看题解 ...
- Codeforces 1017F The Neutral Zone (看题解)
这题一看就筛质数就好啦, 可是这怎么筛啊, 一看题解, 怎么会有这么骚的操作. #include<bits/stdc++.h> #define LL long long #define f ...
随机推荐
- linux备忘簿
1.ubuntu中按ctrl+s锁定屏幕,按ctrl+q解锁. 2.vim中撤销和恢复为u和ctlr+r 3.静态库和动态库编译命令: (1)得到hello.o g++ -c hello.cpp (2 ...
- js 测试性能
console.time('querySelector');for(var i=0; i<1000; i++){document.querySelector('body');}console.t ...
- mybatis:延迟加载时不要在get/set方法上面添加final关键字(原创)
1.mybatis-config.xml: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ...
- Expm 10_1 带负权值边的有向图中的最短路径问题
[问题描述] 对于一个带负权值边的有向图,实现Bellman-Ford算法,求出从指定顶点s到其余顶点的最短路径,并判断图中是否存在负环. package org.xiu68.exp.exp10; p ...
- 1)django-建立步骤和目录说明
一:前言 django是python最流行的WEB框架. 二:django安装 pip install django 三:django项目建立步骤 1.创建django工程 django-admin ...
- 技术的热门度曲线:GHC
全球最大的 IT 咨询公司高德纳(Gartner),有一个"技术热门度曲线"模型(Gartner Hype Cycle). 该模型认为,一门技术的发展要经历五个阶段. (1)启 ...
- 支付宝调用错误:Call to undefined function openssl_sign()
打开php.ini,找到这一行 ;extension=php_openssl.dll,将前面的“;”去掉:重启服务器
- Confluence 6 修改 Home 目录的位置
当 Confluence 第一次启动的时候,Confluence 将会读取 confluence-init.properties 文件并从这个文件中确定如何去查找 Home 目录. 希望修改 home ...
- ios蓝牙详解
最近这段时间在研究蓝牙,也研究了一段时间了现在在下面做个总结 1 其实蓝牙连接只要明白了整体原理,其实挺简单的 2 大部分情况下,手机作为中心管理者,而连接的设备被称为外设,外设的结构有点像一颗大树 ...
- LeetCode(106):从中序与后序遍历序列构造二叉树
Medium! 题目描述: 根据一棵树的中序遍历与后序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 中序遍历 inorder = [9,3,15,20,7] 后序遍历 posto ...