\(\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;
}

最新文章

  1. HDU 5879 Cure -2016 ICPC 青岛赛区网络赛
  2. ActiveMQ 入门使用实例
  3. flash memory
  4. java基础之数据类型转换
  5. 服務器提交協議衝突 (The server committed a protocol violation.)
  6. mssql的holdlock锁跟索引的关系
  7. sql Sever 修改表中的列名
  8. 深入理解javascript new的机制
  9. [转载] 谷歌技术&quot;三宝&quot;之谷歌文件系统
  10. 十二个 ASP.NET Core 例子——配置操作
  11. 安装 R 包报错 clang: error: unsupported option &#39;-fopenmp&#39; 的解决方法
  12. 【iCore4 双核心板_ARM】例程二十六:LWIP_MODBUS_TCP实验——电源监控
  13. 获取从库Seconds_Behind_Master监控主从同步
  14. Vue 使用 vuelidate 实现表单验证
  15. ASP.NET MVC View中的标签(tag)
  16. 音频——H5 audio
  17. linux常用命令集锦
  18. easyui更换主题之后出现validatebox的验证提示信息显示跑偏的解决方案
  19. HttpServletRequest request方法详解
  20. python实现堆栈、队列

热门文章

  1. CNN基础二:使用预训练网络提取图像特征
  2. Windows7有“系统保留”分区时,安装系统要注意的两点
  3. PHP浮点精度问题
  4. Math.random()详解
  5. MySQL慢SQL语句常见诱因
  6. python-列表list- 元组(tuple)- 集合(set)-字典(dict)-实例代码
  7. POJ 1426 Find The Multiple (dfs??!!)
  8. Alpha冲刺阶段博客目录
  9. teradata在虚拟机安装客户端sql Assistant
  10. vue2 的 过渡(动画)效果