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很自豪,他认为搬陨石很容易,所以他将一些区域 ...
随机推荐
- GeneXus笔记本—获取当月的最后一天
首先获取当前日期 然后赋值为当前年月的第一天 然后加一个月 减去一天 就是当月最后一天 多用于筛选数据时的条件或者区间 我们先随便拉个页面 简单点就好 放入两个textblock 然后点击Even ...
- 关于Django中ORM数据库迁移的配置
Django中ORM数据库迁移配置 1,若想将模型转为mysql数据库中的表,需要在settings中配置: DATABASES = { 'default': { 'ENGINE': 'django. ...
- change all column to char
http://bluefrog-oracle.blogspot.com/2011/09/script-submitted-to-otn-to-altter.html Script to Alter v ...
- 2019HDU多校训练第二场 Longest Subarray
题意:给你一个串,问满足以下条件的子串中最长的是多长:对于每个数字,要么在这个子串没出现过,要么出现次数超过k次. 思路:对于这类问题,常常转化为数据结构的询问问题.我们考虑枚举右端点,对于当前右端点 ...
- 使用 jQuery 实现当前页面高亮显示的通栏导航条
index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> ...
- Qt error: C2236: 意外的标记“class”。是否忘记了“;”?
前阵子玩了一个比较大的程序,手脚,身子脑袋都分开写的那种,因此互相include .h比较多,那么问题来了,有些cpp没有include 的类却使用了起来 ,这时候IDE不会出这个类没有定义什么的,而 ...
- Devops、CI\CD、Jenkins
Devops DevOps对应用程序发布的影响 在很多企业中,应用程序发布是一项涉及多个团队.压力很大.风险很高的活动.然而在具备DevOps能力的组织中,应用程序发布的风险很低,原因如下 [2] : ...
- 【leetcode】909. Snakes and Ladders
题目如下: 解题思路:天坑题,不在于题目多难,而是要理解题意.题目中有两点要特别注意,一是“You choose a destination square S with number x+1, x+2 ...
- mac终端命令--常用快捷键
Ctrl + c 取消当前行输入的命令,进入下一行.或者中止一个错误的或者发疯的命令 Ctrl + a 光标移动到行首(Ahead of line),相当于通常的Home ...
- websocket 中使用Service层的方法
创建公共Utils 类 ApplicationContextRegister @Component @Lazy(false) public class ApplicationContextRegist ...