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 ...
随机推荐
- 深度学习之Tensorflow 工程化项目实战 配套资源
https://www.aianaconda.com/index/CodeProject
- go语言从例子开始之Example19.接口
接口 是方法特征的命名集合. Example: package main import "fmt" //让方法,实现api这个接口. type api interface{ get ...
- java一个数组的内存图
- JDBC中如何进行事务处理?
Connection提供了事务处理的方法,通过调用setAutoCommit(false)可以设置手动提交事务:当事务完成后用commit()显式提交事务:如果在事务处理过程中发生异常则通过rollb ...
- excel导出简单示例(jxl jar包)
@param title excel文件名 @param excelTopName 表头中文名字(显示在第一行的中文表头内容) @param header 表头字段属性(根据该属性获取对应的属性值,表 ...
- ivew Upload 上传时附带的额外参数
<Upload action="/api/device/importData" :data="uploadData" :before-upload=&qu ...
- 使用springBoot完成阿里云短信验证
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot ...
- SCP-bzoj-1079
项目编号:bzoj-1079 项目等级:Safe 项目描述: 戳这里 特殊收容措施: DP.普通的状压状态数515,显然TLE+MLE,我们考虑把底数和幂换一换,压成155的状态数. 故状态设计为:f ...
- thinkphp生成二维码
/** * 生成二维码 * @param string $url url连接 * @param integer $size 尺寸 纯数字 */ function qrcode($url,$size=4 ...
- Python之-爬虫
1.得到页面的HTML代码 第一个参数是URL 第二三个参数可以不传送,数据和时间 2.request请求 HTTP是基于请求和应答的,客户端发出请求,服务端做出响应,所以urllib2创建一个req ...