「CF891C」Envy
传送门
Luogu
解题思路
考虑最小生成树的几个性质:
- 所有最小生成树中边权相等的边的条数相等
- 在任意一颗最小生成树中,边权相等的边所联通的点集一定
那么我们考虑把边权相等的边单独拿出来考虑。
每次把并查集恢复到加边前的状态,然后再判断这些边加进去会不会形成环即可。
PS. 恢复并查集时,可以考虑求出每一条边在 \(\text{Kruskal}\) 的过程中,将要被加入时两端点所在联通块。
细节注意事项
- 有点难实现,要有耐心
参考代码
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#define rg register
using namespace std;
template < typename T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while (!isdigit(c)) f |= (c == '-'), c = getchar();
while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
s = f ? -s : s;
}
const int _ = 500010;
int fa[_], siz[_];
inline void init(int n) { for (rg int i = 1; i <= n; ++i) fa[i] = i; }
inline int findd(int x) { return fa[x] == x ? x : fa[x] = findd(fa[x]); }
inline void merge(int x, int y) { fa[findd(x)] = findd(y); }
int n, m, q;
struct edge{ int x, y, val, id, tx, ty; }g[_], e[_];
inline bool cmp(const edge& x, const edge& y) { return x.val < y.val; }
inline bool Cmp(const edge& x, const edge& y) { return x.id < y.id; }
int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
read(n), read(m);
for (rg int i = 1; i <= m; ++i)
read(g[i].x), read(g[i].y), read(g[i].val), g[i].id = i;
init(n), sort(g + 1, g + m + 1, cmp);
for (rg int i = 1; i <= m; ) {
int j = i;
do {
g[j].tx = findd(g[j].x);
g[j].ty = findd(g[j].y);
++j;
} while (j <= m && g[j].val == g[i].val);
while (i < j) {
while (findd(g[i].x) == findd(g[i].y) && i < j) ++i;
if (i < j) merge(g[i].x, g[i].y);
}
}
init(n), sort(g + 1, g + m + 1, Cmp);
for (read(q); q--; ) {
int s; read(s);
for (rg int x, i = 1; i <= s; ++i)
read(x), e[i] = (edge) { g[x].tx, g[x].ty, g[x].val };
sort(e + 1, e + s + 1, cmp);
int flag = 1;
for (rg int i = 1; i <= s && flag; ) {
int j = i;
do {
if (findd(e[j].x) == findd(e[j].y)) { flag = 0; break; }
merge(e[j].x, e[j].y), ++j;
} while (j <= s && e[j].val == e[i].val);
while (i < j) fa[e[i].x] = e[i].x, fa[e[i].y] = e[i].y, ++i;
}
puts(flag ? "YES" : "NO");
}
return 0;
}
完结撒花 \(qwq\)
「CF891C」Envy的更多相关文章
- 「译」JUnit 5 系列:条件测试
原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...
- 「译」JUnit 5 系列:扩展模型(Extension Model)
原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...
- JavaScript OOP 之「创建对象」
工厂模式 工厂模式是软件工程领域一种广为人知的设计模式,这种模式抽象了创建具体对象的过程.工厂模式虽然解决了创建多个相似对象的问题,但却没有解决对象识别的问题. function createPers ...
- 「C++」理解智能指针
维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...
- 「JavaScript」四种跨域方式详解
超详细并且带 Demo 的 JavaScript 跨域指南来了! 本文基于你了解 JavaScript 的同源策略,并且了解使用跨域跨域的理由. 1. JSONP 首先要介绍的跨域方法必然是 JSON ...
- 「2014-5-31」Z-Stack - Modification of Zigbee Device Object for better network access management
写一份赏心悦目的工程文档,是很困难的事情.若想写得完善,不仅得用对工具(use the right tools),注重文笔,还得投入大把时间,真心是一件难度颇高的事情.但,若是真写好了,也是善莫大焉: ...
- 「2014-3-18」multi-pattern string match using aho-corasick
我是擅(倾)长(向)把一篇文章写成杂文的.毕竟,写博客记录生活点滴,比不得发 paper,要求字斟句酌八股结构到位:风格偏杂文一点,也是没人拒稿的.这么说来,arxiv 就好比是 paper 世界的博 ...
- 「2014-3-17」C pointer again …
记录一个比较基础的东东-- C 语言的指针,一直让人又爱又恨,爱它的人觉得它既灵活又强大,恨它的人觉得它太过于灵活太过于强大以至于容易将人绕晕.最早接触 C 语言,还是在刚进入大学的时候,算起来有好些 ...
- 「2014-3-13」Javascript Engine, Java VM, Python interpreter, PyPy – a glance
提要: url anchor (ajax) => javascript engine (1~4 articles) => java VM vs. python interpreter =& ...
随机推荐
- windows好用的cmd命令
1.如何查看本机ip局域网ip 在cmd中输入ipconfig 2.如何在不重启浏览器的情况下让刚修改的hosts生效, 因为服务器和浏览器都有DNS缓存,在cmd中执行ipconfig /flush ...
- Apache Kafka(二)- Kakfa 安装与启动
安装并启动Kafka 1.下载最新版Kafka(当前为kafka_2.12-2.3.0)并解压: > wget http://mirror.bit.edu.cn/apache/kafka/2.3 ...
- javaFx中Image的路径问题
网络图像文件前面加“http://”,而本地文件则要加“file:”.将源代码改为: Image image = new Image("file:image/qq.jpg"); I ...
- NOIP2016普及组解题报告
概述 \(NOIP2016\)普及组的前三题都比较简单,第四题也有很多的暴力分,相信参加了的各位\(OIer\)在\(2016\)年都取得了很好的成绩. 那么,我将会分析\(NOIP2016\)普及组 ...
- AC3 bit allocation
1.bit allocation overview bit allocation通过分析audio 信号的频谱envelop,使用masking effect来确定使用多少bit来表示频率系数的man ...
- codeforces Make The Fence Great Again(dp)
题目链接:http://codeforces.com/contest/1221/problem/D 题目要求ai ! = ai-1,草纸上推理一下可以发现每一个栅栏可以升高的高度无非就是 +0,+1, ...
- Thinkphp中验证码不显示解决办法
1.页面是否存在bom头, 2.入口文件中是否有define(‘APP_DEBUG’, TRUE); //是否开启调试模式,上线时请改为false
- windows系统下,gpu开发环境部署
1,安装python,使用anaconda或者直接用python.exe安装都可以.我用的是python3.6版的 对于相关的程序包,比如tensorflow或者opencv等,anaconda可以在 ...
- Jquery获取html参数, jquery.params.js 获取参数
================================ ©Copyright 蕃薯耀 2019年12月31日 http://fanshuyao.iteye.com/ /** * 使用:$.q ...
- 计算几何-Andrew法-凸包
This article is made by Jason-Cow.Welcome to reprint.But please post the article's address. 利用一下叉积和栈 ...