我还以为是什么板子题呢。。。

我们把儿子当做点, 公主当做边, 然后就是求边权值最大基环树森林。

#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 (看题解)的更多相关文章

  1. Codeforces 269C Flawed Flow (看题解)

    我好菜啊啊啊.. 循环以下操作 1.从队列中取出一个顶点, 把哪些没有用过的边全部用当前方向. 2.看有没有点的入度和 == 出度和, 如果有将当前的点加入队列. 现在有一个问题就是, 有没有可能队列 ...

  2. Codeforces 436E Cardboard Box (看题解)

    Cardboard Box 贪了个半天贪不对, 我发现我根本就不会贪心. 我们先按b排序, 然后枚举选两颗心的b的最大值, 在这个之前的肯定都要选一个, 因为前面的要是一个都没选的话, 你可以把当前选 ...

  3. Codeforces 1045C Hyperspace Highways (看题解) 圆方树

    学了一下圆方树, 好神奇的东西呀. #include<bits/stdc++.h> #define LL long long #define fi first #define se sec ...

  4. Codeforces 1137D Cooperative Game (看题解)

    Cooperative Game 智商题, 感觉不太能推出来, 虽然看看证明过程是对的. #include<bits/stdc++.h> #define LL long long #def ...

  5. Codeforces 983C Elevator dp (看题解)

    Elevator 怎么今天写啥题都不会写啊, 我是傻了吗.. 把电梯里面四个人的目标点当作状态, 然后暴力转移. #include<bits/stdc++.h> #define LL lo ...

  6. Codeforces 924D Contact ATC (看题解)

    Contact ATC 我跑去列方程, 然后就gg了... 我们计每个飞机最早到达时间为L[ i ], 最晚到达时间为R[ i ], 对于面对面飞行的一对飞机, 只要他们的时间有交集则必定满足条件. ...

  7. Codeforces 830C Bamboo Partition (看题解)

    Bamboo Partition 列公式, 整除分块, 想不到, 好菜啊. #include<bits/stdc++.h> #define LL long long #define fi ...

  8. Codeforces 750E New Year and Old Subsequence 线段树 + dp (看题解)

    New Year and Old Subsequence 第一感觉是离线之后分治求dp, 但是感觉如果要把左边的dp值和右边的dp值合起来, 感觉很麻烦而且时间复杂度不怎么对.. 然后就gun取看题解 ...

  9. Codeforces 1017F The Neutral Zone (看题解)

    这题一看就筛质数就好啦, 可是这怎么筛啊, 一看题解, 怎么会有这么骚的操作. #include<bits/stdc++.h> #define LL long long #define f ...

随机推荐

  1. java结合testng,利用txt做数据源的数据驱动实例

    import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import java.io.Buffe ...

  2. Windws Server 2008 R2 WEB环境配置之IIS7/IIS7.5+FastCGI+PHP 5.6.4+MYSQL+phpMyAdmin

    本篇为WEB环境配置的汇总篇,其中PHP以FASTCGI方式来运行,这种方式性能更高.经过配置后,我们的服务器将同时可以运行PHP和.NET的程序,属称全能服务器.所有配置可以根据自身实际需要进行增减 ...

  3. 在线HTTP POST/GET接口测试工具 - aTool在线工具

    百度搜索标题或直接访问网址如下 网址:http://www.atool.org/httptest.php 很好用的在线http get/post 测试工具

  4. cdh5.15集群添加spark2.3服务(parcels安装)

    背景: 机器系统:redhat7.3:已经配置了http服务 集群在安装的时候没有启动spark服务,因为cdh5.15自带的spark不是2.0,所以直接进行spark2.3的安装 参考文档:htt ...

  5. 判断iOS版本号

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 4.0) {  }

  6. 在Amazon FreeRTOS V10中使用运行时统计信息

    在MCU on Eclipse网站上看到Erich Styger在8月2日发的博文,一篇关于在Amazon FreeRTOS V10中使用运行时统计信息的文章,本人觉得很有启发,特将其翻译过来以备参考 ...

  7. socket-WebSocket-HttpListener-TcpListener服务端客户端的具体使用案例

    /// <summary> /// 启动服务监听的ip和端口的主线程 /// </summary> /// <param name="tunnelPort&qu ...

  8. Confluence 6 home 目录

    Confluence Home 目录是存储 Confluence 信息,查找索引,页面附件的地方.其实 Home 目录也可以定义为数据目录. 找到 Home 目录 Confluence 的 home ...

  9. Confluence 6 推荐的更新通知设置和禁用

    你可以设置默认的发送选项(发送 / 不发送)和默认的发送时间(每天或每周). 如何配置推荐更新电子邮件通知: 在屏幕的右上角单击 控制台按钮 ,然后选择 General Configuration 链 ...

  10. C# Parallel并发执行相关问题

    1.Parallel并发执行 using System;using System.Collections.Generic;using System.Linq;using System.Text;usi ...