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" !!的更多相关文章

  1. HackerRank "Self Balancing Tree"

    Something to learn: Rotation ops in AVL tree does not require recursion. https://github.com/andreima ...

  2. *[hackerrank]Cut the tree

    https://www.hackerrank.com/contests/w2/challenges/cut-the-tree 树分成两部分,求两部分差最小.一开始想多了,后来其实求一下总和,求一下分部 ...

  3. 【HackerRank】Utopian tree

    The Utopian tree goes through 2 cycles of growth every year. The first growth cycle of the tree occu ...

  4. 51nod1253 Kundu and Tree

    树包含N个点和N-1条边.树的边有2中颜色红色('r')和黑色('b').给出这N-1条边的颜色,求有多少节点的三元组(a,b,c)满足:节点a到节点b.节点b到节点c.节点c到节点a的路径上,每条路 ...

  5. 51nod_1253:Kundu and Tree(组合数学)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1253 全为红边的情况下,ans=C(n,3).假设被黑边相连 ...

  6. 51Nod1253 Kundu and Tree 容斥原理

    原文链接https://www.cnblogs.com/zhouzhendong/p/51Nod1253.html 题目传送门 - 51Nod1253 题意 树包含 N 个点和 N-1 条边.树的边有 ...

  7. 51nod-1253: Kundu and Tree

    [传送门:51nod-1253] 简要题意: 给出一棵n个点的树,树上的边要么为黑,要么为红 求出所有的三元组(a,b,c)的数量,满足a到b,b到c,c到a三条路径上分别有至少一条红边 题解: 显然 ...

  8. 【51nod1253】Kundu and Tree(容斥+并查集)

    点此看题面 大致题意: 给你一棵树,每条边为黑色或红色, 求有多少个三元组\((x,y,z)\),使得路径\((x,y),(x,z),(y,z)\)上都存在至少一条红色边. 容斥 我们可以借助容斥思想 ...

  9. 51nod 1253:Kundu and Tree(组合数学)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1253 所有的三元组的可能情况数有ans0=C(n,3).然后 ...

随机推荐

  1. JS操作DOM常用API总结

    <JS高程>中的DOM部分写的有些繁琐,还没勇气整理,直到看到了这篇博文 Javascript操作DOM常用API总结,顿时有了一种居高临下,一览全局的感觉.不过有时间还是得自己把书里面的 ...

  2. 用中文把玩Google开源的Deep-Learning项目word2vec

    google最近新开放出word2vec项目,该项目使用deep-learning技术将term表示为向量,由此计算term之间的相似度,对term聚类等,该项目也支持phrase的自动识别,以及与t ...

  3. test Windows Live Writer

    1, 下载Live Writer http://windows.microsoft.com/zh-cn/windows-live/essentials-other#essentials=overvie ...

  4. VIM键盘快捷键映射

    http://www.jianshu.com/p/216811be226b

  5. RG100A-AA 中大校园网上网及远程配置

    由于无线网卡用得不爽,wifi经常断,所以想整个路由器,造福群众.在朋友介绍下购得一台已经刷好 Openwrt 的上海贝尔RG100A-AA路由器,根据下面的简单步骤,就能连接上校园网. 一.准备工作 ...

  6. iOS学习笔记---oc语言第六天

    Block .数组高级 block本质上就是匿名函数(没有名称的函数) block语法和函数指针很相似 回顾函数 函数:C语⾔中,实现某一类功能的代码段. 完整的函数包含两部分:函数声明.函数定义 函 ...

  7. C++ Primer :第十章 :泛型算法之再探迭代器以及其他算法

    除了为每个容器定义的迭代器之外,标准库在头文件<iterator>还定义了额外集中迭代器, 包括: 插入迭代器,这些迭代器被绑定到一个容器上,可以向容器插入元素. 流迭代器,    这些迭 ...

  8. hive学习笔记_hive的表创建

    创建hive表注意事项 一.表分隔符必须与读取的数据文件一致,比如例子的分隔符为 '\t'(制表符),hive下默认分隔符是制表符. 二.最好指定分区作为数据之间的区分. 三.创建完表可以desc+表 ...

  9. 单例模式-ios

    #import <Foundation/Foundation.h> @interface UserContext : NSObject <NSCopying> @propert ...

  10. Mako

    from:  http://www.yeolar.com/note/2012/08/26/mako-usage/ Python模板库Mako的用法(选译自官方文档) Yeolar 2012-08-26 ...