- 可撤销并查集,采用启发式按秩合并
struct Undo_Dsu
{
stack <pii> st ;
int fa[N] , siz[N] ;
void init()
{
while(!st.empty()) st.pop() ;
for (int i = 1 ; i <= n ; i ++) fa[i] = i , siz[i] = 1 ;
}
int findroot(int x)
{
return x == fa[x] ? x : findroot(fa[x]) ;
}
bool merge(int u , int v)
{
int fax = findroot(u) , fay = findroot(v);
if(fax == fay) return 0 ;
if (siz[fax] > siz[fay]) swap(fax, fay) , swap(u , v) ;
fa[fax] = fay , siz[fay] += siz[fax] ;
st.push({fax , fay}) ;
return 1 ;
}
void undo() //撤销一次最新的合并
{
pii no = st.top();
fa[no.fi] = no.fi ;
siz[no.se] -= siz[no.fi] ;
st.pop() ;
}
} dsu ;
-
考虑弱化本题:
如果本题询问的不是一个集合,仅仅只是一条边,那么可以直接LCA维护两点间的最大值做判断。 -
回到本题:
- 为什么边集就不行了呢?因为边集中可能会构成环
考虑到克鲁斯卡尔的思想,每一类边权的数量是固定的。于是我们可以将边按权值大小分类,我们将所有询问离线,对于每一类大小的边的询问单独考虑。 - 如果每一类边权中相同的编号的询问构成了环,则该询问对应的集合一定不合法(利用并查集判环即可),对于相同编号的询问并查集合并完之后需要撤销回到没有合并的状态,以应对同一类边权其他编号询问的判断
//#define LOCAL
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mem(a, b) memset(a,b,sizeof(a))
#define sz(a) (int)a.size()
#define INF 0x3f3f3f3f
#define DNF 0x7f
#define DBG printf("this is a input\n")
#define fi first
#define se second
#define mk(a, b) make_pair(a,b)
#define pb push_back
#define LF putchar('\n')
#define SP putchar(' ')
#define p_queue priority_queue
#define CLOSE ios::sync_with_stdio(0); cin.tie(0)
template<typename T>
void read(T &x) {x = 0;char ch = getchar();ll f = 1;while(!isdigit(ch)){if(ch == '-')f *= -1;ch = getchar();}while(isdigit(ch)){x = x * 10 + ch - 48; ch = getchar();}x *= f;}
template<typename T, typename... Args>
void read(T &first, Args& ... args) {read(first);read(args...);}
template<typename T>
void write(T arg) {T x = arg;if(x < 0) {putchar('-'); x =- x;}if(x > 9) {write(x / 10);}putchar(x % 10 + '0');}
template<typename T, typename ... Ts>
void write(T arg, Ts ... args) {write(arg);if(sizeof...(args) != 0) {putchar(' ');write(args ...);}}
using namespace std;
ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a % b);
}
ll lcm(ll a, ll b) {
return a / gcd(a, b) * b;
}
const int N = 1e6 + 5;
int n , m , q, cnt, vis[N], ans[N];
struct node {
int u , v , w;
}edge[N];
struct Query
{
int x , id;
bool operator < (const Query & no) const {
if (edge[x].w != edge[no.x].w) return edge[x].w < edge[no.x].w;
return id < no.id;
}
}qu[N];
struct Undo_Dsu
{
stack <pii> st ;
int fa[N] , siz[N] ;
void init()
{
while(!st.empty()) st.pop() ;
for (int i = 1 ; i <= n ; i ++) fa[i] = i , siz[i] = 1 ;
}
int findroot(int x)
{
return x == fa[x] ? x : findroot(fa[x]) ;
}
bool merge(int u , int v)
{
int fax = findroot(u) , fay = findroot(v);
if(fax == fay) return 0 ;
if (siz[fax] > siz[fay]) swap(fax, fay) , swap(u , v) ;
fa[fax] = fay , siz[fay] += siz[fax] ;
st.push({fax , fay}) ;
return 1 ;
}
void undo() //撤销一次最新的合并
{
pii no = st.top();
fa[no.fi] = no.fi ;
siz[no.se] -= siz[no.fi] ;
st.pop() ;
}
} dsu ;
int main ()
{
scanf ("%d %d",&n, &m);
for (int i = 1 ; i <= m ; i ++)
scanf ("%d %d %d",&edge[i].u, &edge[i].v, &edge[i].w);
scanf ("%d",&q);
for (int i = 1 ; i <= q ; i ++)
{
int k ;
scanf ("%d",&k);
for (int j = 1 ; j <= k ; j ++)
{
cnt ++;
scanf ("%d",&qu[cnt].x);
qu[cnt].id = i;
vis[qu[cnt].x] = 1;
}
}
for (int i = 1 ; i <= m ; i ++)
if (!vis[i])
cnt ++, qu[cnt].x = i, qu[cnt].id = q + 1;
sort (qu+1,qu+1+cnt);
dsu.init();
for (int i = 1 ; i <= cnt ; i ++)
{
int last = i;
while (last + 1 <= cnt && edge[qu[last+1].x].w == edge[qu[last].x].w) last ++;
for (int j = i ; j <= last ; j ++)
{
int k = j;
int num = 0;
while (k + 1 <= last && qu[k + 1].id == qu[k].id) k ++;
for (int p = j ; p <= k ; p ++)
{
if (!dsu.merge(edge[qu[p].x].u , edge[qu[p].x].v))
ans[qu[p].id] = 1;
else
num ++;
}
for (int p = 1 ; p <= num ; p ++)
dsu.undo();
j = k;
}
for (int j = i ; j <= last ; j ++)
dsu.merge (edge[qu[j].x].u, edge[qu[j].x].v);
i = last;
}
for (int i = 1 ; i <= q ; i ++)
if (ans[i])
puts("NO");
else
puts("YES");
}