Codeforces 892E - Envy 【最小生成树】

E. Envy

For a connected undirected weighted graph G, MST (minimumspanning tree) is a subgraph of G that contains all of G's vertices, isa tree, and sum of its edges is minimum possible.

You are given agraph 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 ofedges of graph G, and you shoulddetermine 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. Itstarts 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 — theindices 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 queryis "YES".

Edges from the second query form a cycleof length 3, so there is nospanning tree including these three edges. Thus, answer is "NO".

【题意】

给你一系列边和其权值,保证利用这些边能够成最小生成树。再给出一系列询问,每个询问中有一些边,问这些边能否在一颗最小生成树中同时出现。

【思路】

首先我们明确一点,即对于一条权值为w的边来说,如果在所有权值小于w的边全部加入图后这条边的两个端点还没有联通的话,那么这条边能出现在最小生成树中。

而且对于每一组询问,权值相同的边会相互影响,而权值不相同的边的判断是相互独立的。

综上,我们得到一种做法:

先对给出边按权值排序记为A,再对询问中的边按权值(按询问)排序,记为B。

然后从小到大枚举B中的边权w,先把小于w的所有边加入到图G1中,然后把询问中边权为w的边一条条判断是否联通,若联通做标记,不连通的话在图G2中加入这条边。

值得注意的是,上一个操作第二步若边权等于w的边在多个询问中时,我们每次换到一个询问,应把G2还原为图G1,因为不同询问相同边权是不相互影响的。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值