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 ...
随机推荐
- ASN.1
ASN.1抽象语法标记(Abstract Syntax Notation One https://baike.baidu.com/item/ASN.1/498523?fr=aladdin
- 第11篇Kubernetes部署微服务电商平台
kubernetes部署sock-shop微服务电商平台: 准备条件 确保kubernetes可以访问:reg.yunwei.edu镜像库 需要准备镜像: 部署微服务 ...
- C# string.Format json格式字符串报错”输入字符串的格式不正确“
当我们在string.Format中传入Json字符串时,会报”输入字符串的格式不正确“,这是因为json的"{"符号的问题,最开始我是想着用转义一下"{",但 ...
- Eu
<parent> <artifactId>microservice-cloud-01</artifactId> <groupId>com.mengx ...
- 51nod1340地铁环线
经典题. 经典差分约束模型. 但是 显然这个总长是有上下界的. 直接二分总长,判断有没有负环 如果没有负环好办,有负环就不知道怎么偏了. 因为没有单调性! (如果所有没有单调性的函数图像,都知道往哪里 ...
- (转)Spring Boot干货系列:(三)启动原理解析
转:http://tengj.top/2017/03/09/springboot3/ 前言 前面几章我们见识了SpringBoot为我们做的自动配置,确实方便快捷,但是对于新手来说,如果不大懂Spri ...
- HTML5: HTML5 应用程序缓存
ylbtech-HTML5: HTML5 应用程序缓存 1.返回顶部 1. HTML5 应用程序缓存 使用 HTML5,通过创建 cache manifest 文件,可以轻松地创建 web 应用的离线 ...
- poi提取docx中的文字和图片
package com.fry.poiDemo.dao; import java.io.File; import java.io.FileInputStream; import java.io.Fil ...
- dubbo超时原理以及解决方案
dubbo超时原理以及解决方案 本篇主要记录dubbo中关于超时的常见问题,实现原理,解决的问题 超时问题 为了检查对dubbo超时的理解,尝试回答如下几个问题,如果回答不上来或者不确定那么说明此处需 ...
- Mosaic
Mosaic 是最古老的WEB浏览器,这是个套件FOR WINDOWS(包括TCP/IP,拨号,EMAIL等).