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 ...
随机推荐
- element el-table 合计在横拉滚动条的下面,正确展示应该是滚动条在合计下面
<style lang="less"> .el-table{ overflow: auto; } .el-table .el-table__body-wrapper, ...
- 【leetcode】1005. Maximize Sum Of Array After K Negations
题目如下: Given an array A of integers, we must modify the array in the following way: we choose an i an ...
- Pydiction补全插件
Pydiction不需要安装,所有没有任何依赖包问题,Pydiction主要包含三个文件. python_pydiction.vim -- Vim plugin that autocompletes ...
- PHP curl_multi_strerror函数
curl_multi_setopt — 返回描述错误码的字符串文本. 说明 string curl_multi_strerror ( int $errornum ) 返回描述 CURLM 错误码的字符 ...
- python基础三(深浅拷贝)
1.赋值操作 list_1 = [1,2,3,['barry','Jerry']] list_2 = list_1 list_1[0] = 111 print(list_1) # [111, 2, 3 ...
- python selenium 自动化流程的一些总结与思考
首先要考虑的项目为什么要做自动化测试?(主要从自动化测试的优点着手) 其次是什么项目才适合做自动化测试? 自动化测试覆盖率要达到什么样的程度?因为都知道不可能达到100%的,不然脚本太臃肿,反而会失去 ...
- MySQL 小数处理函数 round 和 floor
一. 在mysql中,round函数用于数据的四舍五入,它有两种形式: 1.round(x,d) ,x指要处理的数,d是指保留几位小数 这里有个值得注意的地方是,d可以是负数,这时是指定小数点左边的 ...
- 公司-ofo:ofo
ylbtech-公司-ofo:ofo ofo小黄车是一个无桩共享单车出行平台,缔造了“无桩单车共享”模式,致力于解决城市出行问题.用户只需在微信公众号或App扫一扫车上的二维码或直接输入对应车牌号,即 ...
- 73、salesforce通过JAVA来Call在salesforce中已经写好的Restful处理接口
/** *使用salesforce通过REST方式作为webservice,需要以下几点 *1.类和方法需要global,方法需要静态 *2.类需要通过RestResource(UrlMapping= ...
- JS实现的ajax
function createXMLHttpRequest() { try { return new XMLHttpRequest(); } catch (e) { try { return new ...