CodeForces - 891C: Envy(可撤销的并查集&最小生成树)
For a connected undirected weighted graph G, MST (minimum spanning tree) is a subgraph of G that contains all of G's vertices, is a tree, and sum of its edges is minimum possible.
You are given a graph G. If you run a MST algorithm on graph it would give you only one MST and it causes other edges to become jealous. You are given some queries, each query contains a set of edges of graph G, and you should determine whether there is a MST containing all these edges or not.
Input
The first line contains two integers n, m (2 ≤ n, m ≤ 5·105, n - 1 ≤ m) — the number of vertices and edges in the graph and the number of queries.
The i-th of the next m lines contains three integers ui, vi, wi (ui ≠ vi, 1 ≤ wi ≤ 5·105) — the endpoints and weight of the i-th edge. There can be more than one edges between two vertices. It's guaranteed that the given graph is connected.
The next line contains a single integer q (1 ≤ q ≤ 5·105) — the number of queries.
q lines follow, the i-th of them contains the i-th query. It starts with an integer ki (1 ≤ ki ≤ n - 1) — the size of edges subset and continues with ki distinct space-separated integers from 1 to m — the indices of the edges. It is guaranteed that the sum of ki for 1 ≤ i ≤ q does not exceed 5·105.
Output
For each query you should print "YES" (without quotes) if there's a MST containing these edges and "NO" (of course without quotes again) otherwise.
Example
- 5 7
1 2 2
1 3 2
2 3 1
2 4 1
3 4 1
3 5 2
4 5 2
4
2 3 4
3 3 4 5
2 1 7
2 1 2
- YES
NO
YES
NO
Note
This is the graph of sample:
Weight of minimum spanning tree on this graph is 6.
MST with edges (1, 3, 4, 6), contains all of edges from the first query, so answer on the first query is "YES".
Edges from the second query form a cycle of length 3, so there is no spanning tree including these three edges. Thus, answer is "NO".
题意:给定N点M点的无向图,M>=N-1,然后给出Q个询问,每个询问给出一个边集,回答这个边集是否存在于一个最小生成树里。
思路:如果是问一条边,那么我们可以得到最小生成树,然后如果其是树边,或者不超过形成的环的最大边,久说明可以。
但是这里不算问一条边,而是边集,我们如果按照同样的方法,取验证这些边是否ok,则会出错,因为边集内部也可能出现环。
我们已经最小生成树的最重要的结论:
我们按权值给边分类,同一类的边贡献的边的数量是一定的,而且任选其中一种方案,其连通性是相同的。
假设现在只有一个询问,询问的边集按边权排序,权值相同的一块处理,处理到边权为X的时候,[0,X)的部分已经构造号了,如果把询问里边权为X的加进去会产生环,则说明次询问为“NO”。处理完X,把询问中X部分的并查集撤销,然后把原图X连通。
用个时间轴即可用当前的并查集。
- #include<bits/stdc++.h>
- #define rep(i,a,b) for(int i=a;i<=b;i++)
- #define pii pair<int,int>
- #define pb push_back
- #define mp make_pair
- #define F first
- #define S second
- using namespace std;
- const int maxn=;
- vector<int>e[maxn];
- vector<pii>G[maxn];
- int u[maxn],v[maxn],w[maxn],,ans[maxn];
- int fa[maxn],t[maxn],tf[maxn],times;
- int find(int x){
- if(x!=fa[x]) fa[x]=find(fa[x]);
- return fa[x];
- }
- int find2(int x){ //时间轴,可撤销。
- if(t[x]!=times) tf[x]=fa[x],t[x]=times;
- if(x!=tf[x]) tf[x]=find2(tf[x]);
- return tf[x];
- }
- int main()
- {
- int N,M,Q,Mx=;
- scanf("%d%d",&N,&M);
- rep(i,,N) fa[i]=i;
- rep(i,,M){
- scanf("%d%d%d",&u[i],&v[i],&w[i]);
- Mx=max(Mx,w[i]);
- e[w[i]].pb(i);
- }
- scanf("%d",&Q);
- rep(i,,Q){
- int num; scanf("%d",&num);
- rep(j,,num){
- int x; scanf("%d",&x);
- G[w[x]].pb(mp(i,x));
- }
- }
- rep(i,,Mx){
- sort(G[i].begin(),G[i].end());
- int L=G[i].size(),Laxt=;
- rep(j,,L-){ //处理问题
- int id=G[i][j].F,ed=G[i][j].S;
- a=u[ed],b=v[ed];
- if(id!=Laxt) times++,Laxt=id;
- a=find2(a); b=find2(b);
- if(a!=b) tf[a]=b;
- else ans[id]=-;
- }
- L=e[i].size();
- rep(j,,L-){ //连通
- int ed=e[i][j],a=u[ed],b=v[ed];
- a=find(a); b=find(b);
- if(a!=b) fa[a]=b;
- }
- }
- rep(i,,Q) if(ans[i]==-) puts("NO"); else puts("YES");
- return ;
- }
CodeForces - 891C: Envy(可撤销的并查集&最小生成树)的更多相关文章
- CodeForces892E 可撤销并查集/最小生成树
http://codeforces.com/problemset/problem/892/E 题意:给出一个 n 个点 m 条边的无向图,每条边有边权,共 Q 次询问,每次给出 ki 条边,问这些边 ...
- 并查集 & 最小生成树详细讲解
并查集 & 最小生成树 并查集 Disjoint Sets 什么是并查集? 并查集,在一些有N个元素的集合应用问题中,我们通常是在开始时让每个元素构成一个单元素的集合,然后按一定顺序将 ...
- Codeforces 891C Envy(MST + 并查集的撤销)
题目链接 Envy 题意 给出一个连通的无向图和若干询问.每个询问为一个边集.求是否存在某一棵原图的最小生成树包含了这个边集. 考虑$kruskal$的整个过程, 当前面$k$条边已经完成操作的时 ...
- Codeforces Round #376 (Div. 2) C. Socks---并查集+贪心
题目链接:http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,每只都有一个颜色,现在他的妈妈要去出差m天,然后让他每天穿第 L 和第 R 只 ...
- Codeforces 766D. Mahmoud and a Dictionary 并查集 二元敌对关系 点拆分
D. Mahmoud and a Dictionary time limit per test:4 seconds memory limit per test:256 megabytes input: ...
- 洛谷P3247 [HNOI2016]最小公倍数(分块 带撤销加权并查集)
题意 题目链接 给出一张带权无向图,每次询问\((u, v)\)之间是否存在一条路径满足\(max(a) = A, max(b) = B\) Sol 这题居然是分块..想不到想不到..做这题的心路历程 ...
- Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序
https://codeforces.com/contest/1131/problem/D 题意 给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[] ...
- CodeForces Roads not only in Berland(并查集)
H - Roads not only in Berland Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d ...
- codeforces div2 603 D. Secret Passwords(并查集)
题目链接:https://codeforces.com/contest/1263/problem/D 题意:有n个小写字符串代表n个密码,加入存在两个密码有共同的字母,那么说这两个密码可以认为是同一个 ...
随机推荐
- Map中object转换成boolean类型
Ajax请求查询数据之后,返回的是map类型, resultMap.put("flag", flag); 在接收到数据之后判断时,转换出现异常,导致页面点击按钮之后,页面没有反应, ...
- 编译安装 nginx的http_stub_status_module监控其运行状态
步骤: 1 编译nginx,加上参数 --with-http_stub_status_module 以我自己的编译选项为例: #配置指令 ./configure --prefix=/usr/local ...
- STM32探秘 之FSMC
源:STM32探秘 之FSMC STM32 FSMC总线深入研究
- Hyperledger Fabric1.0环境搭建
一.准备CentOS系统,本文使用的是CentOS7.0 二.安装Docker 执行命令 yum -y install docker 验证是否安装成功 docker --version 三.安装Doc ...
- linux usb简介
参考书:<linux device drivers>.<usb 2.0规范> <usb3.1规范><usb白皮书> 以linux为例来说明usb系统. ...
- 安装MySQL5.7.18遇到的坑
最近才注意到MySQL的各个版本之间差别还挺大的,比如5.5.x版本的timestamp类型列只能有一个设置为default CURRENT_TIMESTAMP的,于是尝试了换成一个新版本是mysql ...
- 30道linux运维面试题(很精典)
https://zhangge.net/1986.html 1.linux 如何挂在 windows 下的共享目录 Shell 1 mount.cifs //192.168.1.3 ...
- HMM简单理解(来自quora&其他网上资料)
转载自quora: 连接:https://www.quora.com/What-is-a-simple-explanation-of-the-Hidden-Markov-Model-algorithm ...
- java深入探究13-js,ajax
链接:http://pan.baidu.com/s/1c2D0cAs 密码:uwm6 1.js 1)三种基本类型: var num=100; var str="哈哈"; var f ...
- Spark基本概念快速入门
Spark集群 一组计算机的集合,每个计算机节点作为独立的计算资源,又可以虚拟出多个具备计算能力的虚拟机,这些虚拟机是集群中的计算单元.Spark的核心模块专注于调度和管理虚拟机之上分布式计算任务 ...