zoukankan      html  css  js  c++  java
  • bzoj3376/poj1988[Usaco2004 Open]Cube Stacking 方块游戏

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3376

    题目大意:

    编号为1到n的n(1≤n≤30000)个方块正放在地上.每个构成一个立方柱.

    有P(1≤P≤100000)个指令.指令有两种:

    1.移动(M):将包含X的立方柱移动到包含Y的立方柱上.

    2.统计(C):统计名含X的立方柱中,在X下方的方块数目.

    题解:

    带权并查集

    存三个东西,x所在立方柱的最顶端fa[x],x所在立方柱的最底端d[x],x上面有多少个立方柱f[x](下面的图画错了不包含x qwq..画的时候一点感觉都没有)。


    那么要求的x下方的数目就可以用f[d[x]]-f[x]来表示。

    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    #define maxn 30100
    
    int fa[maxn],f[maxn],d[maxn];
    int ffind(int x)
    {
    	if (fa[x]!=x)
    	{
    		int y=fa[x];
    		fa[x]=ffind(fa[x]);
    		f[x]=f[x]+f[y];
    		d[x]=d[y];
    	}return fa[x];
    }
    int main()
    {
    	//freopen("cubes.in","r",stdin);
    	//freopen("cubes.out","w",stdout);
    	int q,i,x,y;char c;
    	scanf("%d",&q);
    	for (i=1;i<=30000;i++) fa[i]=d[i]=i,f[i]=0;
    	for (i=1;i<=q;i++)
    	{
    		scanf("
    %c%d",&c,&x);
    		if (c=='C') 
    		{
    			int ls=d[ffind(x)];
    			int oz=ffind(ls);//更新
    			printf("%d
    ",f[ls]-f[x]);
    		}else//把x放到y上
    		{
    			scanf("%d",&y);
    			int fx=ffind(x),fy=ffind(y);
    			fa[fy]=fx;int ls=ffind(d[fx]);//更新
    			f[fy]=f[d[fx]]+1;
    			d[fx]=d[fy];
    		}
    	}
    	return 0;
    }


  • 相关阅读:
    Python iter() 函数
    Python file() 函数
    Python bin() 函数
    QTP自动化测试-打开运行报告
    mysql-笔记-数据类型
    mysql-笔记--增删改查
    mysql-笔记-命名、索引规范
    Navicat for MySQL 安装和破解
    mysql client--笔记-修改密码-登录-查看数据库-创建数据库
    安装mysql
  • 原文地址:https://www.cnblogs.com/Euryale-Rose/p/6527838.html
Copyright © 2011-2022 走看看