原题链接:

Kingdom Division

  1. 由于树的层次可能很深,所以这里不能使用递归版的DFS。我使用了BFS。
  2. BFS确定各结点的父结点和它的孩子数。
  3. 用逆拓扑排序确定结点的计算顺序。
  4. same[u][] 表示u结点颜色为0孩子结点颜色全为1时组合数。 diff[u][] 表示u结点颜色为0时可行组合数。本结点颜色为0,子结点颜色为1,孙结点颜色全为0是无效组合。反之亦然。由于这里颜色0、1相互对称, same[u][]=same[u][]; diff[u][]=diff[u][]; 。
  5. 为排除无效组合,计算 diff[u][] 时没有乘以 same[u][] 。
  6. 本结点为0,孩子结点全为1,是无效组合,因此 diff[u][] 最后还要减去 same[u][] 。
#include <cmath>
#include <cstdio>
#include <cassert>
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int MOD = 1e9+;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n = ; assert( == scanf("%d", &n));
vector<vector<int>> adjList(n + );
for(int i = ; i < n - ; i++){
int u, v; assert( == scanf("%d %d", &u, &v));
adjList[u].push_back(v);
adjList[v].push_back(u);
}
vector<vector<long long>> diff(n + , vector<long long>(, ));
vector<vector<long long>> same(n + , vector<long long>(, ));
vector<int> parent(n + , );
vector<int> child_cnt(n + , );
queue<int> traversal_array;
vector<bool> visited(n + , false);
traversal_array.push();
visited[] = true;
while(!traversal_array.empty()){
int tmp = traversal_array.front();
traversal_array.pop();
for(auto vit: adjList[tmp]){
if(!visited[vit]){
visited[vit] = true;
child_cnt[tmp]++;
parent[vit] = tmp;
traversal_array.push(vit);
}
}
} for(int c_i = ; c_i <= n; c_i++){
if(!child_cnt[c_i]){
traversal_array.push(c_i);
}
} while(!traversal_array.empty()){
int tmp = traversal_array.front();
traversal_array.pop();
child_cnt[parent[tmp]]--;
if(child_cnt[parent[tmp]] == ){
traversal_array.push(parent[tmp]);
}
for(auto vit: adjList[tmp]){
if(vit != parent[tmp]){
same[tmp][] = same[tmp][] * diff[vit][] % MOD;
same[tmp][] = same[tmp][] * diff[vit][] % MOD;
}
}
for(auto vit: adjList[tmp]){
if(vit != parent[tmp]){
diff[tmp][] = diff[tmp][] * (diff[vit][] + diff[vit][] + same[vit][]) % MOD;
diff[tmp][] = diff[tmp][] * (diff[vit][] + diff[vit][] + same[vit][]) % MOD;
}
}
diff[tmp][] = (diff[tmp][] - same[tmp][] + MOD) % MOD;
diff[tmp][] = (diff[tmp][] - same[tmp][] + MOD) % MOD;
}
printf("%lld\n", (diff[][] + diff[][])%MOD);
return ;
}

Hacker Rank: Kingdom Division 不完全报告的更多相关文章

  1. [Gym - 100517K] Kingdom Division 2 二分

    大致题意: 给出一个凸包,以及凸包内的两个点p1,p2,求有多少条经过凸包顶点的直线能够将凸包分割为两部分,且给出的两点分别属于不同的部分 枚举凸包的顶点,二分求出p1,p2线段左边的最大坐标L以及右 ...

  2. 「BZOJ1385」「Baltic2000」Division expression 解题报告

    Division expression Description 除法表达式有如下的形式: \(X_1/X_2/X_3.../X_k\) 其中Xi是正整数且\(X_i \le 1000000000(1 ...

  3. Hacker Rank: Two Strings - thinking in C# 15+ ways

    March 18, 2016 Problem statement: https://www.hackerrank.com/challenges/two-strings/submissions/code ...

  4. LightOj1385 - Kingdom Division(数学几何题)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1385 题意:下图中已知面积 a b c 求 d; 如果d的面积不确定,输出-1. 连接 ...

  5. Hackrank Kingdom Division 树形DP

    题目链接:传送门 题意: 给你一棵树,n个点 每个点可以染成红色和蓝色 但是红色的点与其相邻的点中必须有红色节点,蓝色也是 问你有多少种染色的方案 题解: 树形dp 先转化为有根树,取1为根 设定dp ...

  6. interview material

    Articles Recommended: Steve Yegge – Get That Job at Google [web] Carlos Bueno – Get That Job at Face ...

  7. Programming Series 1.0 — C Programming

    In the growing world of technology, C programming has kind of lost its way. Today, we have a million ...

  8. JavaScript函数式编程究竟是什么?

    摘要: 理解函数式编程. 作者:前端小智 原文:JS中函数式编程基本原理简介 Fundebug经授权转载,版权归原作者所有. 在长时间学习和使用面向对象编程之后,咱们退一步来考虑系统复杂性. 在做了一 ...

  9. Facebook Hacker Cup 2014 Qualification Round 竞赛试题 Square Detector 解题报告

    Facebook Hacker Cup 2014 Qualification Round比赛Square Detector题的解题报告.单击这里打开题目链接(国内访问需要那个,你懂的). 原题如下: ...

随机推荐

  1. const的用法,特别是用在函数前面与后面的区别!

    const的用法,特别是用在函数后面 在普通的非 const成员函数中,this的类型是一个指向类类型的 const指针.可以改变this所指向的值,但不能改变 this所保存的地址. 在 const ...

  2. (1)pygame_第一个窗口程序

    ####可以使用python自带的IDLE交互式开发,也可以借助其他的编辑器,我这里采用的pycharm编辑器 1.导入我们所需要的模块 import pygame,sys   --导入我们需要的模块 ...

  3. EXTENDED LIGHTS OUT poj1222 高斯消元法

    EXTENDED LIGHTS OUT Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6443   Accepted: 42 ...

  4. hdu2157矩阵快速幂

    How many ways?? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  5. Average of Levels in Binary Tree

    Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...

  6. Ubuntu 16.04源码编译安装nginx 1.10.0

    一.下载相关的依赖库 pcre 下载地址 http://120.52.73.43/jaist.dl.sourceforge.net/project/pcre/pcre/8.38/pcre-8.38.t ...

  7. PHP抓取网页图片

    <?php set_time_limit(0);//抓取不受时间限制 if($_POST['Submit']=="开始抓取"){ $URL=$_POST['link']; g ...

  8. Druid源码阅读之连接池

    概述 Druid是阿里巴巴开源的一个数据库连接池 源码地址.下面简单分析一下连接池是怎么实现的 怎么开始阅读 如果使用过Druid连接池的都只要在Spring配置中配置jdbc的时候配置Driver是 ...

  9. c# 【MVC】WebApi设置返回Json

    public static HttpResponseMessage toJson(Object obj) { String str; if (obj is String || obj is Char) ...

  10. WPF 快捷方式

    http://www.cnblogs.com/gnielee/archive/2010/07/16/wpf-custom-hotkey-command.html