标签:long ref pos += stdin min new tar ==
并查集好合并不好拆开 可以考虑离线 先读入 从大到小排序 再依次合并
技巧:不好断开就倒着来合并 JSOI2008 P1197 也是该思想
#include<bits/stdc++.h> using namespace std; #define Max(x,y) (x)<(y)?(y):(x) #define Min(x,y) (x)<(y)?(x):(y) #define ll long long #define rg register const int N=300000+5,M=1000000+5,inf=0x3f3f3f3f,P=9999973; int n,m,f[N],sz[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; } int find(int x) {return f[x]==x?x:f[x]=find(f[x]);} void merge(int x,int y){ int fx=find(x),fy=find(y); if(fx==fy) return; sz[fx]+=sz[fy],f[fy]=fx; } struct edge{int u,v,w;}e[N<<1]; struct node{int k,v,pos,ans;}ask[N]; bool cmp1(edge a,edge b){return a.w>b.w;} bool cmp2(node a,node b){return a.k>b.k;} bool cmp3(node a,node b){return a.pos<b.pos;} int main(){ // freopen("in.txt","r",stdin); rd(n),rd(m),f[n]=n,sz[n]=1; for(int i=1;i<n;++i) rd(e[i].u),rd(e[i].v),rd(e[i].w),f[i]=i,sz[i]=1; for(int i=1;i<=m;++i) rd(ask[i].k),rd(ask[i].v),ask[i].pos=i; sort(e+1,e+n,cmp1); sort(ask+1,ask+m+1,cmp2); for(int i=1,nxte=1;i<=m;++i){ while(nxte<n&&e[nxte].w>=ask[i].k) merge(e[nxte].u,e[nxte].v),++nxte; ask[i].ans=sz[find(ask[i].v)]-1; } // for(int i=1;i<=n;++i) printf("%d ",sz[i]); sort(ask+1,ask+m+1,cmp3); for(int i=1;i<=m;++i) printf("%d\n",ask[i].ans); return 0; }
【luogu4185】 [USACO18JAN]MooTube [并查集]
标签:long ref pos += stdin min new tar ==
原文地址:https://www.cnblogs.com/lxyyyy/p/11220678.html