codeforces1156D 0-1-Tree 并查集
题意:
给定一棵n个点的边权为0或1的树,一条合法的路径(x,y)(x≠y)满足,从x走到y,一旦经过边权为1的边,就不能再经过边权为0的边,求有多少边满足条件?
思路:
首先这道题,换根dp也可以过(树形dp,点这里)
那么如何并查集做呢,我们考虑一个点$u$,我们将与u通过0边相连的连通点的数量记做$siz0$,将与u通过1边相连的连通点的数量记做$siz1$,那么所有符合条件的0-1边将u作为转折点的,0-0边将u作为终点的,1-1边将u作为终点的边的数量就等于$siz0*siz1-1$,减去的1就是自己连到自己。
用并查集维护上面的信息,即$f[u][0]$表示u的0边祖先,$f[u][0]$表示u的1边祖先,然后$merge$即可。
- #pragma GCC optimize (2)
- #pragma G++ optimize (2)
- #pragma comment(linker, "/STACK:102400000,102400000")
- #include<bits/stdc++.h>
- #include<cstdio>
- #include<vector>
- #define rep(i,a,b) for(int i=a;i<=b;i++)
- #define dep(i,b,a) for(int i=b;i>=a;i--)
- #define clr(a,b) memset(a,b,sizeof(a))
- #define pb push_back
- #define pii pair<int,int >
- using namespace std;
- typedef long long ll;
- const int maxn=;
- ll rd()
- {
- ll x=,f=;char ch=getchar();
- while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
- while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
- return x*f;
- }
- int f[maxn][],siz[maxn][];
- int n,a,b,c;
- int find(int x,int y){
- if(!f[x][y]){
- return x;
- }
- return f[x][y]=find(f[x][y],y);
- }
- int main(){
- cin>>n;
- rep(i,,n-){
- scanf("%d%d%d",&a,&b,&c);
- int fx=find(a,c);
- int fy=find(b,c);
- if(fx!=fy){
- f[fx][c]=fy;
- }
- }
- rep(i,,n){
- siz[find(i,)][]++,siz[find(i,)][]++;
- }
- ll ans=;
- rep(i,,n){
- ans+=1ll*siz[find(i,)][]*siz[find(i,)][]-;
- }
- cout<<ans<<endl;
- }
codeforces1156D 0-1-Tree 并查集的更多相关文章
- Hdu.1325.Is It A Tree?(并查集)
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- Is It A Tree?(并查集)
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26002 Accepted: 8879 De ...
- CF109 C. Lucky Tree 并查集
Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal re ...
- HDU 5606 tree 并查集
tree 把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小. 开一个并查集,每次读到边权是0的边就合并.最后Ansi=size[findset(i)],size表示每个并 ...
- [Swust OJ 856]--Huge Tree(并查集)
题目链接:http://acm.swust.edu.cn/problem/856/ Time limit(ms): 1000 Memory limit(kb): 10000 Description T ...
- Codeforces Round #363 (Div. 2)D. Fix a Tree(并查集)
D. Fix a Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Is It A Tree?(并查集)(dfs也可以解决)
Is It A Tree? Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submi ...
- tree(并查集)
tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- 树上统计treecnt(dsu on tree 并查集 正难则反)
题目链接 dalao们怎么都写的线段树合并啊.. dsu跑的好慢. \(Description\) 给定一棵\(n(n\leq 10^5)\)个点的树. 定义\(Tree[L,R]\)表示为了使得\( ...
- hdu 1325 Is It A Tree? 并查集
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
随机推荐
- 用户Bug修补报告
用户Bug修补报告 虽然经过许多天的奋斗,我们的U-Help已经正式投入使用,不过在使用过程中遇到了大大小小的问题,我们通过努力修补了其中的相当一部分,以下是用户Bug修补报告. 7.31:发布bet ...
- 2018-12-25-win2d-图片水印
title author date CreateTime categories win2d 图片水印 lindexi 2018-12-25 10:37:52 +0800 2018-03-19 08:3 ...
- Ansible--04 ansible 流程控制
ansible 流程控制 playbook 条件语句 不管是 shell 还是各大编程预言中,流程控制,条件判断都是必不可少的,在我们使用 Ansible的过程中,条件判断的使用频率都非常高. 例如: ...
- jQuery HTML- 添加元素
添加内容 html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> & ...
- 【串线篇】spring泛型依赖注入原理
spring泛型依赖注入原理 不管三七二十一 servlet :加注解@servlet service:加注解@service dao:加注解@Repository 这相当于在容器中注册这些个类
- docker cassandra集群搭建
1.使用daocloud的镜像,创建docker集群 启用一个node docker run -d --name cassandra -p 9042:9042 daocloud.io/library/ ...
- 【leetcode】940. Distinct Subsequences II
题目如下: Given a string S, count the number of distinct, non-empty subsequences of S . Since the result ...
- 【leetcode】929. Unique Email Addresses
题目如下: Every email consists of a local name and a domain name, separated by the @ sign. For example, ...
- php abs()函数 语法
php abs()函数 语法 abs()函数怎么用? abs()函数的作用是返回一个数的绝对值.语法是abs(number),如果参数 number 是 float,则返回的类型也是 float,否则 ...
- ubuntu下安装apidoc
1.到http://nodejs.cn/download/下载nodejs 可以复制链接 使用wget下载更加快速 选择对应的操作系统位数 下载到/usr/local/src 此处强烈不建议编译安装 ...