Codeforces 1156D 带权并查集
题意:给你一颗树,树边的权值可能是0或1,问先走0边,再走1边,或者只走1边的路径有多少条?
思路:对于一个点,假设通过0边相连的点一共有x个(包括自己),通过1边相连的有y个(包括自己),那么对答案的贡献为x * y - 1,意思是以x个点为起点,以y个点为终点了路径条数,-1是因为自己到自己的不算路径,用带权并查集去统计x和y即可。
代码:
#include <bits/stdc++.h>
#define LL long long
#define db double
#define INF 0x3f3f3f3f
#define pii pair<int, int>
using namespace std;
const int maxn = 200010;
int a[maxn];
int f[2][maxn], sz[2][maxn];
int get(int pos, int x) {
if(x == f[pos][x]) return x;
return f[pos][x] = get(pos, f[pos][x]);
}
void merge(int pos, int x, int y) {
int x1 = get(pos, x), y1 = get(pos, y);
if(x1 == y1) return;
f[pos][x1] = y1;
sz[pos][y1] += sz[pos][x1];
}
int main() {
int n, x, y, z;
LL ans = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
f[0][i] = f[1][i] = i;
sz[0][i] = sz[1][i] = 1;
}
for (int i = 1; i < n; i++) {
scanf("%d%d%d", &x, &y, &z);
merge(z, x, y);
}
for (int i = 1; i <= n; i++) {
ans += ((long long)sz[0][get(0, i)] * sz[1][get(1, i)]) - 1;
}
printf("%lld\n", ans);
}
Codeforces 1156D 带权并查集的更多相关文章
- Codeforces Educational Codeforces Round 5 C. The Labyrinth 带权并查集
C. The Labyrinth 题目连接: http://www.codeforces.com/contest/616/problem/C Description You are given a r ...
- Codeforces Round #181 (Div. 2) B. Coach 带权并查集
B. Coach 题目连接: http://www.codeforces.com/contest/300/problem/A Description A programming coach has n ...
- Codeforces 1499G - Graph Coloring(带权并查集+欧拉回路)
Codeforces 题面传送门 & 洛谷题面传送门 一道非常神仙的题 %%%%%%%%%%%% 首先看到这样的设问,做题数量多一点的同学不难想到这个题.事实上对于此题而言,题面中那个&quo ...
- codeforces 687D Dividing Kingdom II 带权并查集(dsu)
题意:给你m条边,每条边有一个权值,每次询问只保留编号l到r的边,让你把这个图分成两部分 一个方案的耗费是当前符合条件的边的最大权值(符合条件的边指两段点都在一个部分),问你如何分,可以让耗费最小 分 ...
- CodeForces - 687D: Dividing Kingdom II (二分图&带权并查集)
Long time ago, there was a great kingdom and it was being ruled by The Great Arya and Pari The Great ...
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array 带权并查集
C. Destroying Array 题目连接: http://codeforces.com/contest/722/problem/C Description You are given an a ...
- D. The Door Problem 带权并查集
http://codeforces.com/contest/776/problem/D 注意到每扇门都有两个东西和它连接着,那么,如果第i扇门的状态是1,也就是已经打开了,那么连接它的两个按钮的状态应 ...
- POJ 1703 Find them, Catch them(带权并查集)
传送门 Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42463 Accep ...
- [NOIP摸你赛]Hzwer的陨石(带权并查集)
题目描述: 经过不懈的努力,Hzwer召唤了很多陨石.已知Hzwer的地图上共有n个区域,且一开始的时候第i个陨石掉在了第i个区域.有电力喷射背包的ndsf很自豪,他认为搬陨石很容易,所以他将一些区域 ...
随机推荐
- iOS 点击按钮截屏
@interface CaptureViewController () @property (nonatomic, strong) UIImageView *backgrounView; //控制器背 ...
- java改动后运行无变化
缓存没有及时清理,重新清理打包后运行正常
- 带你逐行阅读redux源码
带你逐行阅读redux源码 redux版本:2019-7-17最新版:v4.0.4 git 地址:https://github.com/reduxjs/redux/tree/v4.0.4 redux目 ...
- html+jquery+php实现文件上传全过程
本例子采用html+jquery+php实现上传功能 html部分 <!DOCTYPE html> <html> <head> <meta charset=& ...
- 生成器模式Builder
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11406502.html 1. 定义将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的 ...
- 【JavaWeb项目】一个众筹网站的开发(三)第一个网页
一.bootstrap 本项目采用bootstrap3 bootstrap中文网 https://www.bootcss.com/ 使用bootstrap三步: 1.导入jQuery 2.导入boot ...
- 【leetcode】640. Solve the Equation
题目如下: 解题思路:本题的思路就是解析字符串,然后是小学时候学的解方程的思想,以"2x+3x-6x+1=x+2",先把左右两边的x项和非x项进行合并,得到"-x+1=x ...
- 函数————count
count和count_if函数是计数函数,先来看一下count函数:count函数的功能是:统计容器中等于value元素的个数. count(first,last,value); first是容器的 ...
- paper 148: Face Databases
持续更新ing~ 1.人脸检测数据库: (1999年发布)CMU+MIT:180幅图像,共734个人脸.包含3个正面人脸测试子集和一个旋转人脸测试子集,其中正面人脸测试子集有130幅图像,共511个人 ...
- Linux CentOS-7.4-x86_64(原版) 百度网盘下载
因为CentOS-7-x86_64-DVD-1804.iso 镜像文件4.16G,超出了上传百度网盘的单个文件大小限制(4G), 所以这里先现将ISO镜像文件压缩成RAR包,然后上传网盘. 使用的话, ...