CF687D Dividing Kingdom II
\(\mathtt{CF 687D}\)
\(\mathcal{Description}\)
给你一个图有 \(n\) 个点 \((1 \leq n \leq 10^3)\) 和 \(m\) 条边 \((1 \leq m \leq \dfrac{n*(n-1)}{2})\) ,边有边权。给定 \(q\) 组询问,每次询问给定 \(l\) 和 \(r\),用编号为 \([l,r]\) 去构成图,使得两边端点在同一个部分的边的最大值最小。
\(\mathcal{Solution}\)
看到题第一反应是线段树,看看标签好像不太对劲的样子,考虑简单点的做法。
考虑如果构成的图是二分图的话,不可能存在一条边的两个端点在同一部分,所以可以得出构成的图一定不是二分图,于是题目可以转化成找奇环的最小变的最大值。
我们可以把边按权值从大到小排序,从最大的开始,不断加边,如果当前构成的图还是一个二分图,则继续加边,如果不是,就是最后我们要构成的图,所以就可以用带权二分图来做。
\(\mathcal{Code}\)
#include<bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
int n, m, q;
int fa[N], fa1[N];
struct Node {
int u, v, w, id;
} edge[N << 1];
inline int read() {
int x = 0, k = 1; char c = getchar();
for (; c < 48 || c > 57; c = getchar()) k ^= (c == '-');
for (; c >= 48 && c <= 57; c = getchar()) x = x * 10 + (c ^ 48);
return k ? x : -x;
}
inline bool cmp(Node x, Node y) {
return x.w > y.w;
}
int find(int x) {
return (fa[x] == x) ? x : (fa[x] = find(fa[x]));
}
inline void match(int x, int y) {
int fx = find(x), fy = find(y);
if (fx == fy)
return;
if (fa1[fx] < fa1[fy]) swap(fx, fy);
fa[fy] = fa[fx];
if (fa1[fx] == fa1[fy])
fa1[fx]++;
return;
}
inline int query(int l, int r) {
for (int i = 1; i <= m; i++) {
if (edge[i].id < l || edge[i].id > r)
continue;
if (find(edge[i].u) != find(edge[i].v)) {
match(edge[i].u, edge[i].v + n);
match(edge[i].u + n, edge[i].v);
}
else
return edge[i].w;
}
return -1;
}
int main() {
n = read(), m = read(), q = read();
for (int i = 1; i <= m; i++)
edge[i].u = read(), edge[i].v = read(), edge[i].w = read(), edge[i].id = i;
std::sort(edge + 1, edge + 1 + m, cmp);
for (int i = 1; i <= 2 * n; i++)
fa[i] = i, fa1[i] = 0;
for (int l = 0, r = 0, ans = -1; q--; ) {
l = read(), r = read();
printf("%d\n", query(l, r));
for (int i = 1; i <= 2 * n; i++)
fa[i] = i, fa1[i] = 0;
}
return 0;
}
CF687D Dividing Kingdom II的更多相关文章
- 【CF687D】Dividing Kingdom II 线段树+并查集
[CF687D]Dividing Kingdom II 题意:给你一张n个点m条边的无向图,边有边权$w_i$.有q个询问,每次给出l r,问你:如果只保留编号在[l,r]中的边,你需要将所有点分成两 ...
- Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 并查集求奇偶元环
D. Dividing Kingdom II Long time ago, there was a great kingdom and it was being ruled by The Grea ...
- Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 暴力并查集
D. Dividing Kingdom II 题目连接: http://www.codeforces.com/contest/687/problem/D Description Long time a ...
- CodeForces - 687D: Dividing Kingdom II (二分图&带权并查集)
Long time ago, there was a great kingdom and it was being ruled by The Great Arya and Pari The Great ...
- codeforces 687D Dividing Kingdom II 带权并查集(dsu)
题意:给你m条边,每条边有一个权值,每次询问只保留编号l到r的边,让你把这个图分成两部分 一个方案的耗费是当前符合条件的边的最大权值(符合条件的边指两段点都在一个部分),问你如何分,可以让耗费最小 分 ...
- codeforces泛做..
前面说点什么.. 为了完成日常积累,傻逼呵呵的我决定来一发codeforces 挑水题 泛做.. 嗯对,就是泛做.. 主要就是把codeforces Div.1的ABCD都尝试一下吧0.0.. 挖坑0 ...
- Codeforces Round #158 (Div. 2)
A. Adding Digits 枚举. B. Ancient Prophesy 字符串处理. C. Balls and Boxes 枚举起始位置\(i\),显然\(a_i \le a_j, 1 \l ...
- [ACM] hdu 1025 Constructing Roads In JGShining's Kingdom (最长递增子序列,lower_bound使用)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- HDU 1025 Constructing Roads In JGShining's Kingdom(二维LIS)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
随机推荐
- 前端学习(三十)es6的一些问题(笔记)
赋值表达式 document.onclick = document.onmouseover = fn; var a = b = c = d = 5; 不推荐 逗号表 ...
- epoll学习(二)
首先看程序一,这个程序想要实现的功能是当用户从控制台有任何输入操作时,输出”hello world!”. l 程序一 #include <unistd.h> #include <io ...
- 【LeetCode 30】串联所有单词的子串
题目链接 [题解] 开个字典树记录下所有的单词. 然后注意题目的已知条件 每个单词的长度都是一样的. 这就说明不会出现某个字符串是另外一个字符串的前缀的情况(除非相同). 所以可以贪心地匹配(遇到什么 ...
- <自动化测试>之<SeleniumIDE使用详解 >
最近在做些简单的自动化理解培训,以繁化简,就写了一节selenium ide的使用教程,在这里分享给刚入门的朋友 自动化插件工具介绍: 这是一款基于Firefox的自动化录制插件,UI界面化操作,无需 ...
- [bzoj3733]Iloczyn 题解(搜索剪枝)
3733: [Pa2013]Iloczyn Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 741 Solved: 217[Submit][Statu ...
- Angular项目中迭代生成的树,激活选中的节点,并将节点数据发送到父节点
从后台返回的数据,还有多层子节点,需要一个生成树的组件,如果直接在页面上写循环来拼接感觉会很麻烦,因为数据的层级结构不固定. 参考网上其他人的方法,整理如下: 1. 创建一个用于循环迭代的组件,在父组 ...
- Linux常用命令的使用方法
Linux 命令大全 Linux 命令大全 1.文件管理 cat chattr chgrp chmod chown cksum cmp diff diffstat file find git gitv ...
- ROM、PROM、EPROM、EEPROM、Flash ROM分别指什么?
ROM指的是“只读存储器”,即Read-Only Memory.这是一种线路最简单半导体电路,通过掩模工艺, 一次性制 造,其中的代码与数据将永久保存(除非坏掉),不能进行修改.这玩意一般在大批量生产 ...
- mysql常用内置函数-查询语句中不能使用strtotime()函数!
来自:http://yushine.iteye.com/blog/775407 FROM_UNIXTIME把 unix时间戳转换为标准时间 unix_timestamp把标准时间转换为 unix时间戳 ...
- HTML-参考手册: 功能排序
ylbtech-HTML-参考手册: 功能排序 1.返回顶部 1. 功能排序 New : HTML5 新标签 标签 描述 基础 <!DOCTYPE> 定义文档类型. <html ...