HackerRank "Kundu and Tree" !!
Learnt from here: http://www.cnblogs.com/lautsie/p/3798165.html
Idea is: we union all pure black edges so we get 1+ pure black edge groups. Then we can simply pick only 1 vertex from each pure-black group to form a triangle. Then it will be all combination problem.
Only with some data type tuning to make it pass all tests:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <unordered_map>
using namespace std; unordered_map<int, int> parent;
unordered_map<int, long long> num; #define M 1000000007 int find(int i)
{
while(i != parent[i]) i = parent[i];
return i;
}
void merge(int i, int j) // merge j to i
{
int pi = find(i);
int pj = find(j);
if (pi == pj) return; num[pi] += num[pj];
parent[pj] = pi;
} int main()
{
// Get input
int n; cin >> n;
for (int i = ; i <= n; i++)
{
parent[i] = i;
num[i] = ;
}
// union all black edges
for (int i = ; i < (n-); i++)
{
int a, b;
cin >> a >> b;
char c;
cin >> c;
if (c == 'b')
{
merge(a, b);
}
} // Now we have grouped all connected pure black edges // Idea is, we pick 1 from 1 black set to make sure there's no pure
// black edges in one triangle. and we pick three
long long p1 = ;
long long p2 = ;
long long p3 = ;
for(int i = ; i <= n; i ++)
{
if(i == parent[i])
{
long long x = num[i]; p1 += x;
p2 += (x * x);
p3 += (x * x * x);
}
} long long ret = p1 * p1 * p1 - * p2 * p1 + * p3;
cout << ((ret / ) % M) << endl; return ;
}
HackerRank "Kundu and Tree" !!的更多相关文章
- HackerRank "Self Balancing Tree"
Something to learn: Rotation ops in AVL tree does not require recursion. https://github.com/andreima ...
- *[hackerrank]Cut the tree
https://www.hackerrank.com/contests/w2/challenges/cut-the-tree 树分成两部分,求两部分差最小.一开始想多了,后来其实求一下总和,求一下分部 ...
- 【HackerRank】Utopian tree
The Utopian tree goes through 2 cycles of growth every year. The first growth cycle of the tree occu ...
- 51nod1253 Kundu and Tree
树包含N个点和N-1条边.树的边有2中颜色红色('r')和黑色('b').给出这N-1条边的颜色,求有多少节点的三元组(a,b,c)满足:节点a到节点b.节点b到节点c.节点c到节点a的路径上,每条路 ...
- 51nod_1253:Kundu and Tree(组合数学)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1253 全为红边的情况下,ans=C(n,3).假设被黑边相连 ...
- 51Nod1253 Kundu and Tree 容斥原理
原文链接https://www.cnblogs.com/zhouzhendong/p/51Nod1253.html 题目传送门 - 51Nod1253 题意 树包含 N 个点和 N-1 条边.树的边有 ...
- 51nod-1253: Kundu and Tree
[传送门:51nod-1253] 简要题意: 给出一棵n个点的树,树上的边要么为黑,要么为红 求出所有的三元组(a,b,c)的数量,满足a到b,b到c,c到a三条路径上分别有至少一条红边 题解: 显然 ...
- 【51nod1253】Kundu and Tree(容斥+并查集)
点此看题面 大致题意: 给你一棵树,每条边为黑色或红色, 求有多少个三元组\((x,y,z)\),使得路径\((x,y),(x,z),(y,z)\)上都存在至少一条红色边. 容斥 我们可以借助容斥思想 ...
- 51nod 1253:Kundu and Tree(组合数学)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1253 所有的三元组的可能情况数有ans0=C(n,3).然后 ...
随机推荐
- PHP语言基础(标记、注释、变量、数组、常量、函数)
PHP标记风格 1.xml风格(标准风格推荐使用) 代码如下: <?php echo"这是xml风格的标记"; ?> xml风格的标记是常用的标记,也是推荐使用的 ...
- RelativeLayout用到的一些重要的属性
第一类:属性值为true或false android:layout_centerHrizontal 水平居中 ...
- Fresco好案例
http://www.open-open.com/lib/view/open1436094840774.html
- HTML ---滚动条样式代码及<marquee>标签的用法;
html中滚动条属性设置 scrollbar属性.样式详解 1.overflow内容溢出时的设置(设定被设定对象是否显示滚动条) overflow-x水平方向内容溢出时的设置 overfl ...
- js部分---类型,变量;
<script type="text/javascript">1.注释:用“//或者/**/”2.数据类型: (1)整型 int (2)小数类型 单精度float 双精 ...
- HDU-4089 Activation (概率DP求概率)
题目大意:一款新游戏注册账号时,有n个用户在排队.每处理一个用户的信息时,可能会出现下面四种情况: 1.处理失败,重新处理,处理信息仍然在队头,发生的概率为p1: 2.处理错误,处理信息到队尾重新排队 ...
- Redis GEO ,GEOHASH,Spatial_index
https://matt.sh/redis-geo http://antirez.com/latest/0 http://invece.org/ https://github.com/davidmot ...
- WCF Restful JQuery 跨域解决方法
<?xml version="1.0"?> <!-- For more information on how to configure your ASP.NET ...
- Web前端开发笔试&面试_05_other 2016104399MS
1.数据传送的方式,get post 的区别是? 2.你要怎么绑定页码(比如给你第三页,)? 3.数据流是如何实现,用for 循环? 4.轮播怎么实现?用原生JS实现. 5.布局,B是固定宽度,A的内 ...
- #linux包之psmisc之fuser命令
概述 [root@localhost ~]# rpm -qf /sbin/fuserpsmisc-22.6-15.el6_0.1.x86_64 先说 fuser的作用,fuser能识别出正在对某个文件 ...