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个密码,加入存在两个密码有共同的字母,那么说这两个密码可以认为是同一个 ...
随机推荐
- UI组件之UIImage
UIImageView:图像视图,用于在应用程序中显示图片 UIImage:是将图片文件转换为程序中的图片对象 UIImageView是UIImage的载体 方法一:用此方法创建图片对象,会将图片ca ...
- 安装mysql到ubuntu
Ubuntu 16.04上安装MySQL步骤: 如果你使用的是Ubuntu 16.04以前的版本,可以看这里:Ubuntu 14.04/15.10升级到Ubuntu 16.04 LTS.一. 安装My ...
- PHP 数字转大写
<?php header("content-type:text/html;charset=utf-8"); function numToRmb($num){ $rmbNum ...
- linux alsa pcm(此pcm非硬件pcm接口)
转:https://blog.csdn.net/crycheng/article/details/7095899 CODEC :音频芯片的控制,比如静音.打开(关闭)ADC(DAC).设置ADC(DA ...
- Linux图像系统框架-理解X11与Qt的层次结构
转:http://blog.csdn.net/kjfureone/article/details/52848550 1. 前言 图形子系统是linux系统中比较复杂的子系统之一:对下,它要管理形态各异 ...
- 主攻ASP.NET MVC4.0之重生:ASP.NET MVC Web API
UserController代码: using GignSoft.Models; using System; using System.Collections.Generic; using Syste ...
- php数组函数-array_pop()
array_pop()函数删除数组中的最后一个元素(出栈). array_pop(array) array:必需.规定数组 返回值:返回数组的最后一个值.如果数组是空,或者不是一个数组,将返回NULL ...
- 教你在windows10环境下如何安装minepy并成功运行!
在学习使用sklearn做单机特征工程这篇文章时,发现在计算互信息时from minepy import MINE代码运行出错ModuleNotFoundError: No module named ...
- Docker Compose yml
Wordpress + Mysql version: '3' services: db: image: mysql:latest volumes: - db_data:/var/lib/mysql e ...
- linux 安装jdk1.7 环境
由于各Linux开发厂商的不同,因此不同开发厂商的Linux版本操作细节也不一样,今天就来说一下CentOS下JDK的安装: 方法一:手动解压JDK的压缩包,然后设置环境变量 1.在/usr/目录下创 ...