abc097d<并查集,排列>
1.abc306e <mutiset的使用>2.abc053d <简单贪心>3.abc042d <组合数>4.abc043d5.abc045d6.abc044d <根号分治>7.abc046d8.abc047d9.abc048d <博弈>10.abc049d <并查集>11.abc050d <???>12.abc051 <多源最短路>13.abc052d14.abc054d <dp, 背包>15.abc055d <枚举>16.abc056d <贪心 / 二分+DP+bitset>17.abc057d <贪心+组合计数>18.abc058d <公式化简>19.abc059d <博弈, 打表找规律>20.abc060d <dp, 背包>21.abc061d <单源最短路, spfa, 判断负环>22.abc062d <优先队列>23.abc063d <二分答案>24.abc064d <贪心/前缀和>25.abc309f <线段树 + 离散化 + 双指针>26.abc309e <dfs>27.abc065d <贪心+最小生成树> [lambda表达式]28.abc066d <组合>29.abc067d <博弈 + dfs>30.abc068d <思维 + 构造>31.abc069d <构造>32.abc070d <简单树上dfs>33.abc071d <递推>34.abc072d <贪心>35.abc073d <Floyed + 枚举排列>36.abc074d <Floyed 消除传递边>37.abc075d <暴力枚举 / 枚举+离散化+二维前缀和>38.abc076d <dp / 贪心>39.abc077d <思维 + 最短路 (将构造数字过程视为最短路)>40.abc078d <博弈>41.abc079d <Floyed>42.abc080d <区间重叠>43.abc081d <思维 构造>44.abc082d <bitset 状压dp>45.abc083d <思维 贪心>46.abc084d <素数筛 前缀和>47.abc085d <贪心>48.abc086d <二维前缀和 同余>49.abc087d <并查集 维护距离信息>50.abc088 <bfs 最短路>51.abc089 <前缀和>52.abc310d <dfs暴搜-分组方案数 / bitmask表示集合+dp>53.abc310e <公式递推(dp?)>54.abc310f <dp + bitmask>55.abc090d <枚举计数>56.abc312c <二分答案>57.abc312d <dp, 括号匹配方案数>58.abc312e <暴力>59.abc320f <dp >60.abc092d<构造,思维>61.abc094d<组合数>62.abc095d<思维>63.abc096d<素数筛,整除>
64.abc097d<并查集,排列>
65.abc098d<双指针,异或>66.abc099d<dfs,枚举排列方案>67.abc100d<枚举>68.abc101d<打表,数学>题目
D - Equals
给出
思路
- 将m中对换中能够相互关联的位置归为一组,这组位置之间可通过对换操作实现任意顺序;
- 因而对于一组内的数据,是需要求出组中涉及的原始位置集合
与原始排列数集合 的交集,将所有组求得的交集元素个数相加即为答案。 - 具体方法:使用并查集结组;使用set处理集合求交。
总结
- 意识到“组内位置之间可通过对换操作实现任意顺序”是关键。
代码
点击查看代码
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
using namespace std;
using LL = long long;
const int N = 1e5 + 10;
int p[N];
set<int> st[N];
int fa[N];
int find(int x)
{
if (fa[x] != x) fa[x] = find(fa[x]);
return fa[x];
}
void solv()
{
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i ++) fa[i] = i;
for (int i = 1; i <= n; i ++) cin >> p[i];
for (int i = 1; i <= m; i ++)
{
int x, y;
cin >> x >> y;
x = find(x);
y = find(y);
fa[y] = x;
}
for (int i = 1; i <= n; i ++)
{
int t = find(i);
st[t].insert(p[i]);
}
int ans = 0;
for (int i = 1; i <= n; i ++)
{
int t = find(i);
if (st[t].count(i)) ans ++;
}
cout << ans << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T = 1;
// cin >> T;
while (T--)
solv();
return 0;
}
本文来自博客园,作者:O2iginal,转载请注明原文链接:https://www.cnblogs.com/o2iginal/p/17962295
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律