51nod-1253: Kundu and Tree
【传送门:51nod-1253】
简要题意:
给出一棵n个点的树,树上的边要么为黑,要么为红
求出所有的三元组(a,b,c)的数量,满足a到b,b到c,c到a三条路径上分别有至少一条红边
题解:
显然黑边是没用的,那么我们将只有黑边相连的点分成若干的连通块
那么答案就很显然了,容斥一手
就是(所有三元组的数量)-(三个点都在一个连通块的数量)-(两个点在一个连通块,另一个不在的数量)
参考代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
LL Mod=1e9+;
struct node
{
int x,y,c,next;
}a[];int len,last[];
void ins(int x,int y,int c)
{
a[++len]=(node){x,y,c,last[x]};
last[x]=len;
}
int fa[];
int findfa(int x)
{
if(fa[x]!=x) fa[x]=findfa(fa[x]);
return fa[x];
}
int tot[];
void dfs(int x,int f,int rt)
{
tot[rt]++;
for(int k=last[x];k;k=a[k].next)
{
int y=a[k].y;
if(y==f||a[k].c==) continue;
dfs(y,x,rt);fa[y]=rt;
}
}
char st[];
int main()
{
int n;
scanf("%d",&n);
len=;memset(last,,sizeof(last));
for(int i=;i<n;i++)
{
int x,y;
scanf("%d%d%s",&x,&y,st+);
ins(x,y,st[]=='b');ins(y,x,st[]=='b');
}
for(int i=;i<=n;i++) fa[i]=i,tot[i]=;
for(int i=;i<=n;i++) if(fa[i]==i) dfs(i,,i);
LL ans=(LL)n*(n-)*(n-)/%Mod;
for(int i=;i<=n;i++)
{
int fx=findfa(i);
if(fx==i)
{
if(tot[i]>=) ans=(ans-(LL)tot[i]*(tot[i]-)*(tot[i]-)/%Mod+Mod)%Mod;
if(tot[i]>=) ans=(ans-(LL)tot[i]*(tot[i]-)/%Mod*(n-tot[i])%Mod+Mod)%Mod;
}
}
printf("%lld\n",ans);
return ;
}
51nod-1253: Kundu and Tree的更多相关文章
- 51nod 1253:Kundu and Tree(组合数学)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1253 所有的三元组的可能情况数有ans0=C(n,3).然后 ...
- 51nod_1253:Kundu and Tree(组合数学)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1253 全为红边的情况下,ans=C(n,3).假设被黑边相连 ...
- HackerRank "Kundu and Tree" !!
Learnt from here: http://www.cnblogs.com/lautsie/p/3798165.html Idea is: we union all pure black edg ...
- 51nod1253 Kundu and Tree
树包含N个点和N-1条边.树的边有2中颜色红色('r')和黑色('b').给出这N-1条边的颜色,求有多少节点的三元组(a,b,c)满足:节点a到节点b.节点b到节点c.节点c到节点a的路径上,每条路 ...
- 51Nod1253 Kundu and Tree 容斥原理
原文链接https://www.cnblogs.com/zhouzhendong/p/51Nod1253.html 题目传送门 - 51Nod1253 题意 树包含 N 个点和 N-1 条边.树的边有 ...
- 【51nod1253】Kundu and Tree(容斥+并查集)
点此看题面 大致题意: 给你一棵树,每条边为黑色或红色, 求有多少个三元组\((x,y,z)\),使得路径\((x,y),(x,z),(y,z)\)上都存在至少一条红色边. 容斥 我们可以借助容斥思想 ...
- NOIP前做题记录
鉴于某些原因(主要是懒)就不一题一题写了,代码直接去\(OJ\)上看吧 CodeChef Making Change 传送门 完全没看懂题解在讲什么(一定是因为题解公式打崩的原因才不是曲明英语太差呢- ...
- 10.23NOIP模拟题
叉叉题目描述现在有一个字符串,每个字母出现的次数均为偶数.接下来我们把第一次出现的字母 a 和第二次出现的 a 连一条线,第三次出现的和四次出现的字母 a 连一条线,第五次出现的和六次出现的字母 a ...
- 51nod 1576 Tree and permutation(树的重心+dfn序)
乍一看我不会. 先不考虑加点. 先考虑没有那个除\(2\). 考虑每一条边的贡献,假设某一条边把这棵树分成大小为x,y的两个部分. 那么这条边最多可以被使用\(min(x,y)*2\)次(因为有进有出 ...
随机推荐
- [SCOI 2007] 排列
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1072 [算法] 状压DP [代码] #include<bits/stdc++. ...
- SSH不允许进行DNS解析
ssh设置DNS不解析 sed -i "s/#UseDNS yes/UseDNS no/" /etc/ssh/sshd_config/etc/init.d/sshd restar ...
- 13.ubuntu下Qt5无法使用中文的问题解决
1.首先安装fcitx-frontend-qt5 sudo apt-get install fcitx-frontend-qt5 这个应该是默认安装的,然后查看fcitx-frontend-qt5 的 ...
- MS SQL 获取数据字典的经典sql语句
select [表名]=c.Name, [表说明]=isnull(f.[value],''), [列名]=a.Name, [列序号]=a.Column_id, [标识]=case when is_id ...
- 格式化日期字符串 FormatSettings使用
如果 你想要得到 YYYY-MM/DD 这样的字符串 你肯定说这太简单了 直接 ShowMessage(FormatDateTime('YYYY-MM/DD',now)); 运行结果 YYYY-MM ...
- Oprofile分析(android oprofile性能分析)
一.内核支持: make menuconfig 1.评测菜单中启用 Oprofile ,在 .config 文件中设置?CONFIG_PROFILING=y?和?CONFIG_OPROFILE=y 2 ...
- usaco 最少找零
Description 约翰在镇上买了 T 元钱的东西,正在研究如何付钱.假设有 N 种钞票,第 i 种钞票的面值为 Vi,约翰身上带着这样的钞票 Ci 张.商店老板罗伯是个土豪,所有种类的钞票都有无 ...
- Android FloatMenuSample
项目地址:fanOfDemo/FloatMenuSample crosg/FloatMenuSample transfer from yiming/FloatMenuSample GIF GRADLE ...
- Kattis - How Many Digits?
How Many Digits? Often times it is sufficient to know the rough size of a number, rather than its ex ...
- LeetCode Golang 单向链表相加 反向实现
LeetCode 两数之和, 反向实现 1 -> 2 -> 3 -> 4 + 3 -> 4 ------------------------- ...