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. JDK1.8(JRE)和eclipse-jee不匹配解决放

    想要用eclipse-jee的话,需要jdk1.8一下版本才能用. 1.需要下载jdk1.7 2.把jdk1.7安装(不需要设置环境变量). 3.在项目上右击选择properties 4.选择Java ...

  2. PAT 天梯赛 L1-034. 点赞 【MAP】

    题目链接 https://www.patest.cn/contests/gplt/L1-034 AC代码 #include <cstdio> #include <cstring> ...

  3. 【转】dmesg 时间转换

    dmesg 时间转换 dmesg 输出的格式不易查看,可以通过命令进行转换. 记录如下: 时间查看: date -d "1970-01-01 UTC `echo "$(date + ...

  4. DEC VT100 terminal

  5. Cookie用法简介

    java操作Cookie---javax.servlet.http.Cookie 1.增加一个Cookie Cookie cookie = new Cookie("username" ...

  6. Linux静默安装weblogic

    本实验安装weblogic10系列版本 #创建weblogic用户组. [root@admin /]# groupadd weblogic[root@admin /]# useradd -g webl ...

  7. Cocos2d-x项目移植到WP8系列之八:CCLabelTTF显示中文不换行

    原文链接: http://www.cnblogs.com/zouzf/p/3985330.html 在wp8平台上,CCLabeTTF显示中文不会自动换行,看了下源码,原来底层的实现是根据text的空 ...

  8. Cocos2d-x项目移植到WP8系列之二:开篇

    原文链接: http://www.cnblogs.com/zouzf/p/3970130.html 开发环境一笔带过吧,主板和CPU要支持虚拟化技术,要开启才行,装个64位win8.1系统,win8不 ...

  9. 谈Swift中的访问控制

    访问控制(Access Control) 访问控制可以限定其他源文件或模块中的代码对你的代码的访问级别.这个特性可以让我们隐藏代码的一些实现细节,并且可以指定一些代码和访问和使用的优先接口. 你可以明 ...

  10. Android LCD(三):LCD接口篇【转】

    本文转载自:http://blog.csdn.net/xubin341719/article/details/9177085 关键词:Android LCD控制器 Framebuffer PWM  平 ...