\(\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的更多相关文章

  1. 【CF687D】Dividing Kingdom II 线段树+并查集

    [CF687D]Dividing Kingdom II 题意:给你一张n个点m条边的无向图,边有边权$w_i$.有q个询问,每次给出l r,问你:如果只保留编号在[l,r]中的边,你需要将所有点分成两 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. codeforces 687D Dividing Kingdom II 带权并查集(dsu)

    题意:给你m条边,每条边有一个权值,每次询问只保留编号l到r的边,让你把这个图分成两部分 一个方案的耗费是当前符合条件的边的最大权值(符合条件的边指两段点都在一个部分),问你如何分,可以让耗费最小 分 ...

  6. codeforces泛做..

    前面说点什么.. 为了完成日常积累,傻逼呵呵的我决定来一发codeforces 挑水题 泛做.. 嗯对,就是泛做.. 主要就是把codeforces Div.1的ABCD都尝试一下吧0.0.. 挖坑0 ...

  7. Codeforces Round #158 (Div. 2)

    A. Adding Digits 枚举. B. Ancient Prophesy 字符串处理. C. Balls and Boxes 枚举起始位置\(i\),显然\(a_i \le a_j, 1 \l ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. ActiveMQ安装步骤及介绍

    1.什么是ActiveMQ? ActiveMQ是一个完全支持JMS1.1和J2EE1.4规范的JMS Provider实现. JMS相关概念 提供者:实现JMS规范的消息中间件服务器 客户端:发送或接 ...

  2. ContextLoaderListener vs DispatcherServlet

    In XML based Spring MVC configuration, you must have seen two declarations in web.xml file i.e. Cont ...

  3. Mysql查看编码方式

    查看数据库的字符集 show variables like 'character\_set\_%'; 输出: +--------------------------+--------+ | Varia ...

  4. hdu 6085 Rikka with Candies (set计数)

    Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...

  5. 【LeetCode 32】最长有效括号

    题目链接 [题解] 设dp[i]表示以第i个字符结尾的最长有效括号的长度. 显然只要考虑s[i]==')'的情况 则如果s[i-1]=='(',则dp[i] = dp[i-2]+2; 如果s[i-1] ...

  6. CF 1150 D Three Religions——序列自动机优化DP

    题目:http://codeforces.com/contest/1150/problem/D 老是想着枚举当前在给定字符串的哪个位置,以此来转移. 所以想对三个串分别建 trie 树,然后求出三个t ...

  7. redis配置文件详解-3

    redis3.0以上配置文件 #################################INCLUDES ################################### include ...

  8. SQL语句映射文件(2)增删改查、参数、缓存

    2.2 select 一个select 元素非常简单.例如: <!-- 查询学生,根据id --> <select id="getStudent" paramet ...

  9. python 找到列表中满足某些条件的元素

    a = [0, 1, 2, 3, 4, 0, 2, 3, 6, 7, 5] selected = [x for x in a if x in range(1, 5)]   # 找到a中属于[1,5)中 ...

  10. 78、tensorflow滑动平均模型,用来更新迭代的衰减系数

    ''' Created on 2017年4月21日 @author: weizhen ''' #4.滑动平均模型 import tensorflow as tf #定义一个变量用于计算滑动平均,这个变 ...