首页 > 其他分享 >Luogu P2342 叠积木

Luogu P2342 叠积木

时间:2022-10-25 14:03:18浏览次数:92  
标签:积木 int Luogu ans fa P2342 siz include find


题目链接:​​传送门​

虽然看着很简单
还是计较了好久!
fa[]是这堆的最底下那块的编号
siz[]是底块所在堆的size
ans[]就是ans,这块底下有多少块
在find的时候沿路更新答案即可
find函数在这里意思是找最下面的那个块
下面还有解释

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <complex>
#include <algorithm>
#include <climits>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define
#define

using namespace std;
typedef long long ll;
int fa[A], p, a, b, siz[A], ans[A]; char opt;
int find(int x) {
if (fa[x] == x) return x;
int fx = find(fa[x]);
ans[x] += ans[fa[x]];
return fa[x] = fx;
}

int main(int argc, char const *argv[]) {
cin >> p;
for (int i = 1; i < A; i++) fa[i] = i, siz[i] = 1;
while (p --> 0) {
cin >> opt;
if (opt == 'M') {
cin >> a >> b;
int x = find(a), y = find(b); //换底块
fa[x] = y; //x这一堆的底块为y
ans[x] += siz[y]; //x这一堆下面加了siz[y]个块
siz[y] += siz[x]; //y为底块的一堆的siz加x的siz
siz[x] = 0; //x不再作为底块所以siz清零
}
else {
cin >> a; find(a); //这里必须要find一下更新答案
cout << ans[a] << endl;
}
}
}


标签:积木,int,Luogu,ans,fa,P2342,siz,include,find
From: https://blog.51cto.com/lyle/5794669

相关文章

  • Luogu P2150 [NOI2015]寿司晚宴
    题目链接:​​传送门​​太难了太难了题意就是问有多少种分案把一个到的排列分配为两组并使组间元素两两互质首先我们只需要考虑根号内的质因子对答案的影响,因为根号外的因......
  • Luogu P2515 [HAOI2010]软件安装
    题目链接:​​传送门​​很明显,如果图中有一个环那么这个环上的点必须都要选那我们一开始就直接缩点因为每个物品有价值有重量还有有重量限制所以是很明显的树上背包我......
  • Luogu P4421 [COCI2017-2018#1] Lozinke
    题目链接:​​传送门​​一开始直接AC自动机每个串暴力跳fail显然会T,44分#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<complex>#i......
  • Luogu P3182 [HAOI2016]放棋子
    题目链接:​​传送门​​题目说了每行有一个障碍两个障碍不在同一行也不在同一列那障碍放哪里就没关系了矩阵都不用输入或者这样理解:交换矩阵的某两行对答案是没有影响......
  • Luogu P1772 [ZJOI2006]物流运输
    题目链接:​​传送门​​很麻烦也很难想的一道题数据很小大胆yy详细解释在代码里#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<co......
  • Luogu P4149 [IOI2011]Race
    题目链接:​​传送门​​#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<complex>#include<algorithm>#include<climits>#include<......
  • Luogu P2859 [USACO06FEB]摊位预订Stall Reservations
    题目链接:​​传送门​​很明显的要贪心从左右端点考虑先排序保证单调性每次往后看有没有能接上的单调性才保证了这个往后看的复杂度于是就很好写了/***@Date:2019......
  • Luogu P3488 [POI2009]LYZ-Ice Skates
    题目链接:​​传送门​​号脚的人可以穿大小的鞋设为号脚的人的数量假设选择区间就要满足把提出来即维护一个全局最大子段和就好了#include<iostream>#include<cstdi......
  • Luogu P4105 [HEOI2014]南园满地堆轻絮
    题目链接:​​传送门​​明显的二分简单的check我的没有longlong会炸掉50分#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<comple......
  • Luogu P3313 [SDOI2014]旅行
    题目链接:​​传送门​​动态开点+树剖的模板吧。都很熟的话就挺好写的特别注意在dfs序上修改#include<iostream>#include<cstdio>#include<cstring>#include<cstdli......