当前位置:网站首页>并查集学习
并查集学习
2022-08-10 08:46:00 【Aidan 347】
Example
input
1 5 5 3 1 2 2 2 3 3 3 4 1 4 5 1 5 1 3 3 2 1output
2 6 10
思路:离线并查集
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <map>
#include <set>
using namespace std;
#define mst(x, y) memset(x, y, sizeof x);
#define X first
#define Y second
#define int long long
#define FAST ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
const int N = 200010, INF = 0x3f3f3f3f, EPS = 1e-8;
typedef pair<int, int> PII;
int n, m, Q, t;
int ans[N];
int p[N];
int cnt[N];
struct Node
{
int l, r, w;
} node[N];
struct QUE
{
int id, c;
} que[N];
bool cmp1(Node a, Node b)
{
return a.w > b.w;
}
bool cmp2(QUE a, QUE b)
{
return a.c > b.c;
}
int find(int x) // 并查集
{
if (p[x] != x)
p[x] = find(p[x]);
return p[x];
}
signed main()
{
FAST;
cin >> t;
while (t--)
{
cin >> n >> m >> Q;
for (int i = 1; i <= n; i++)
{
p[i] = i;
cnt[i] = 1;
}
for (int i = 1, a, b, c; i <= m; i++)
{
cin >> a >> b >> c;
node[i] = {a, b, c};
}
for (int i = 1, a; i <= Q; i++)
{
cin >> a;
que[i] = {i, a};
}
sort(node + 1, node + 1 + m, cmp1);
sort(que + 1, que + 1 + Q, cmp2);
int j = 1, sum = 0;
for (int i = 1; i <= Q; i++)
{
int q = que[i].c, id = que[i].id;
while (j <= m && node[j].w >= q)
{
int pa = find(node[j].l), pb = find(node[j].r);
if (pa != pb)
{
sum += cnt[pa] * cnt[pb];
p[pa] = pb;
cnt[pb] += cnt[pa];
}
j++;
}
ans[id] = sum;
}
for (int i = 1; i <= Q; i++)
{
cout << ans[i] << endl;
}
}
system("pause");
return 0;
}
边栏推荐
- CV+Deep Learning——网络架构Pytorch复现系列——classification(三:MobileNet,ShuffleNet)
- ShardingSphere入门
- Spotify expresses its architectural design using the C4 model
- ABAP Data Types 和XSD Type 映射关系以及XSD Type属性
- 【微信小程序】一文读懂页面导航
- J9 Number Theory: Macro Analysis of DAO Characteristics
- Solve the problem that the win10win7win8 system cannot find the specified module and cannot register the desert plug-in
- The precise effect of network integration promotion outsourcing in the era of Internet of Things-Shenzhen Win-Win World News
- Ask next CDC mysql to Doris. Don't show the specific number of lines, how to do?
- A File Online Query Display and Download Function Realized by Delphi
猜你喜欢
UGUI—事件,iTween插件
Spotify expresses its architectural design using the C4 model
【微服务架构】为故障设计微服务架构
ARM体系结构2:处理器内核和汇编指令集
J9数字论:关于DAO 特点的宏观分析
The implementation of the seemingly useless component (text gradient) in NaiveUI is so simple
短视频同城流量宣传小魔推有何优势?如何给实体商家带来销量?
【OAuth2】二十、OAuth2扩展协议 PKCE
【搜索引擎】Solr:提高批量索引的性能
解决win10win7win8系统找不到指定的模块,注册不了大漠插件的问题
随机推荐
JS reduce
Spotify使用C4模型表达其架构设计
CV-人脸识别-2018:ArcFace
I don't want to do accounting anymore, Die changed to a new one, moved forward bravely, and finally successfully passed the career change test to double his monthly salary~
Day36 LeetCode
js reads excel time format conversion
PTA 习题2.1 简单计算器
本地生活商家如何通过短视频赛道,提升销量曝光量?
js reduce
ARM Architecture 3: Addressing and Exception Handling of ARM Instructions
UGUI - Events, iTween Plugin
[In-depth study of 4G/5G/6G topic-56]: L3 signaling control-5-radio bearer management
对称加密与非对称加密的区别
phpstudy starts automatically
iwemeta元宇宙:一个娃娃卖9999元,泡泡玛特认为一点也不贵
DAY26: GetShell project
Rust learning: 6.4_ enumeration of composite types
Process management (dynamic)
dayjs-----time format
【API架构】REST API 行业辩论:OData vs GraphQL vs ORDS