[CF891C the Envy] [Minimum Spanning Tree]

CF891C Envy

CF891C luogu

== is to look at the yyb

  1. For any edge weights, the number of edges of all weights is constant minimum spanning tree
  2. For any properly bordered embodiment, after all of the connectivity graph edges completion of the addition of less than a certain weight is the same

Ownership considered together by two sides of the same value can be obtained even out of the result thereof is fixed

Connected to the side face of the communication block

Each pretreatment less than \ (W_i \) is added to join the edge of the right side \ (W_i \) side if the formation of a ring group then this query fails

In regards to modify the restore == Every time you want to look for the inquiry posed

#include<bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<int,int>pii;
const int N=5e5+5,M=5e5+5,inf=0x3f3f3f3f;
int n,m,s,tt,f[N];
template<class t>void rd(t &x){
    x=0;int w=0;char ch=0;
    while(!isdigit(ch)) w|=ch=='-',ch=getchar();
    while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    x=w?-x:x;
}

struct edge{
    int u,v,w,id;
    bool operator<(const edge&X)const{return w<X.w;}
}e[N],q[N];
bool cmp(edge X,edge Y){return X.id<Y.id;}
int find(int x){return f[x]==x?x:f[x]=find(f[x]);}

bool kruskal(){
    int K;rd(K);
    for(int i=1,x;i<=K;++i) rd(x),q[i]=e[x];
    sort(q+1,q+K+1);
    for(int i=1,j=1;i<=K;i=++j){
        while(j<K&&q[j+1].w==q[j].w) ++j;
        for(int k=i;k<=j;++k) f[q[k].u]=q[k].u,f[q[k].v]=q[k].v;
        for(int k=i;k<=j;++k){
            if(find(q[k].u)==find(q[k].v)) return 0;
            f[f[q[k].u]]=f[q[k].v];
        }
    }
    return 1;
}


int main(){
    freopen("in.txt","r",stdin);
    rd(n),rd(m);
    for(int i=1,u,v,w;i<=m;++i) rd(u),rd(v),rd(w),e[i]=(edge){u,v,w,i};
    for(int i=1;i<=n;++i) f[i]=i;
    sort(e+1,e+m+1);
    for(int i=1,j=1;i<=m;i=++j){
        while(j<m&&e[j+1].w==e[j].w) ++j;
        for(int k=i;k<=j;++k) e[k].u=find(e[k].u),e[k].v=find(e[k].v);
        for(int k=i;k<=j;++k)
        if(find(e[k].u)!=find(e[k].v)) f[f[e[k].u]]=f[e[k].v];
    }
    sort(e+1,e+m+1,cmp);
    rd(m);while(m--) puts(kruskal()?"YES":"NO");
    return 0;
}

Guess you like

Origin www.cnblogs.com/lxyyyy/p/11540391.html