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 nm (2  ≤ n, m  ≤ 5·105n - 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 uiviwi (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

Input
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
Output
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(可撤销的并查集&最小生成树)的更多相关文章

  1. CodeForces892E 可撤销并查集/最小生成树

    http://codeforces.com/problemset/problem/892/E 题意:给出一个 n 个点 m 条边的无向图,每条边有边权,共 Q 次询问,每次给出 ki​ 条边,问这些边 ...

  2. 并查集 & 最小生成树详细讲解

    并查集 & 最小生成树 并查集 Disjoint Sets 什么是并查集?     并查集,在一些有N个元素的集合应用问题中,我们通常是在开始时让每个元素构成一个单元素的集合,然后按一定顺序将 ...

  3. Codeforces 891C Envy(MST + 并查集的撤销)

    题目链接  Envy 题意  给出一个连通的无向图和若干询问.每个询问为一个边集.求是否存在某一棵原图的最小生成树包含了这个边集. 考虑$kruskal$的整个过程, 当前面$k$条边已经完成操作的时 ...

  4. Codeforces Round #376 (Div. 2) C. Socks---并查集+贪心

    题目链接:http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,每只都有一个颜色,现在他的妈妈要去出差m天,然后让他每天穿第 L 和第 R 只 ...

  5. Codeforces 766D. Mahmoud and a Dictionary 并查集 二元敌对关系 点拆分

    D. Mahmoud and a Dictionary time limit per test:4 seconds memory limit per test:256 megabytes input: ...

  6. 洛谷P3247 [HNOI2016]最小公倍数(分块 带撤销加权并查集)

    题意 题目链接 给出一张带权无向图,每次询问\((u, v)\)之间是否存在一条路径满足\(max(a) = A, max(b) = B\) Sol 这题居然是分块..想不到想不到..做这题的心路历程 ...

  7. Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序

    https://codeforces.com/contest/1131/problem/D 题意 给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[] ...

  8. CodeForces Roads not only in Berland(并查集)

    H - Roads not only in Berland Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d ...

  9. codeforces div2 603 D. Secret Passwords(并查集)

    题目链接:https://codeforces.com/contest/1263/problem/D 题意:有n个小写字符串代表n个密码,加入存在两个密码有共同的字母,那么说这两个密码可以认为是同一个 ...

随机推荐

  1. python常用模块-2

    一 .time模块 表示时间的三种方式: 时间戳:数字(计算机能认识的) 时间字符串:t='2012-12-12' 结构化时间:time.struct_time(tm_year=2017, tm_mo ...

  2. 异常:Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005.

    异常:Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} ...

  3. iOS 52个技巧学习心得笔记 第一章 熟悉OC

    1 .简单了解OC2 .在类的头文件中尽量少引入其他头文件3 .多用字面量语法 少用与之等价的方法 4 .多用类型常量 少用 #define 预处理指令5 .用枚举表示状态,选项,状态码 .简单了解O ...

  4. 【Head First Servlets and JSP】笔记5:HttpServletResponse resp

    [HttpServletResponse resp] [由servlet处理响应] 1.一般可以用通过resp获得一个输出流(writer),然后通过输出流将HTML写入响应.例如: resp.set ...

  5. HA 脑裂原理

    HA 脑裂原理 “裂脑”,乃一个形象的术语,系HA系统危机情景. 引子:“裂脑”是治疗“癫痫”病的一种手术.医生们认为癫痫病发作是由于大脑“异常放电”所至.为了阻止“异常放电”波及整个大脑(左.右半脑 ...

  6. ACM训练小结-2018年6月14日

    于恢复性训练Day2情况如下:https://vjudge.net/contest/234651    先看A题,读懂题意,没有想明白,码完后连续多次WA,后找到错误AC.    对B题,发现其是一个 ...

  7. Django 详解<二> 之url和view

    Django URL(路由系统) RL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL模式以及要为该URL模式调用的视图函数之间的映射表:你就是以这种方式告诉Django,对 ...

  8. Java中的UDP协议编程

    一. UDP协议定义   UDP协议的全称是用户数据报,在网络中它与TCP协议一样用于处理数据包.在OSI模型中,在第四层——传输层,处于IP协议的上一层.UDP有不提供数据报分组.组装和不能对数据包 ...

  9. springboot 配置过滤器

    能配置例外 先写配置文件类 FilterConfig.java package com.ty.tyzxtj.config; import javax.servlet.Filter; import or ...

  10. Kafka详解二:如何配置Kafka集群

    问题导读1.Kafka有哪几种配制方法?2.如何启动一个Consumer实例来消费消息? Kafka集群配置比较简单,为了更好的让大家理解,在这里要分别介绍下面三种配置 单节点:一个broker的集群 ...