zoukankan      html  css  js  c++  java
  • 解题报告:luogu P2342 & P5092

    题目链接:P2342 [USACO04OPEN]Cube Stacking G
    双倍经验:P5092 [USACO04OPEN]Cube Stacking
    裸的加权并查集,直接维护下就好了,不过还是犯了(SB)错误,和(lhr)大佬一起沦为(18;;pts)
    在询问的时候同步更新一下(f[x])及相关内容,保证是最新的。
    复杂度应该是(O(nalpha(n)))的吧(唉,说好下个加权并查集讲经验的呢?算了,太水了,遇到难题时再说吧)。

    (Code):

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    using namespace std; 
    const int MAXN=30005;
    int num[MAXN],f[MAXN],dis[MAXN];
    void init(int maxx){for(int i=1;i<=maxx;i++) dis[i]=0,f[i]=i,num[i]=1;}
    int getf(int u)
    {
        if(f[u]==u) return u;
        else 
        {
            int now=f[u];
            f[u]=getf(f[u]);
            num[u]=num[f[u]];
            dis[u]+=dis[now];
        }
        return f[u];
    }
    void merge(int u,int v)
    {
    	int t1=getf(u),t2=getf(v);
    	if(t1!=t2)
    	{
    		f[t2]=t1;
    		dis[t2]=num[t1];
    		num[t1]+=num[t2];
    		num[t2]=num[t1];
    	}
    	return;
    }
    int p;
    char c; 
    int x,y;
    int main()
    {
    	scanf("%d",&p);
    	init(30000);
    	for(int i=1;i<=p;i++)
    	{
    		cin>>c;
    		if(c=='M')
    		{
    			scanf("%d%d",&x,&y);
    			merge(y,x);
    		}
    		else
    		{
    			scanf("%d",&x);
    			getf(x);//kel,注意就好了
    			printf("%d
    ",dis[x]);
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    378. Kth Smallest Element in a Sorted Matrix
    295. Find Median from Data Stream
    857. Minimum Cost to Hire K Workers
    373. Find K Pairs with Smallest Sums
    767. Reorganize String
    无序列表
    有序列表
    缩写
    设计者详细信息
    强调
  • 原文地址:https://www.cnblogs.com/tlx-blog/p/12493598.html
Copyright © 2011-2022 走看看