2017 Wuhan University Programming Contest (Online Round) B Color 树形dp求染色方法数
/**
题目:Color
链接:https://oj.ejq.me/problem/23
题意:给定一颗树,将树上的点最多染成m种颜色,有些节点不可以染成某些颜色。相邻节点颜色不同。求染色方法数。
思路:树形dp,定义dp[i][j]表示以i为根,i节点染色为j时候的子树的染色方法数。 */
#include<bits/stdc++.h>
#define LL long long
using namespace std;
typedef long long ll;
const int maxn = 2e4+;
ll dp[maxn][];///dp[i][j]表示以i为根,i节点染色为j时候的子树的染色方法数。
ll mod = 1e7+;
vector<int> G[maxn];///图
vector<int> color[maxn];///节点i可以染的颜色;
ll cnt[maxn];///记录节点i的子树的方法数。可以减少一层循环。优化时间。
int n, m;
void init()
{
for(int i = ; i <= n; i++){
G[i].clear();
color[i].clear();
}
memset(cnt, , sizeof cnt);
memset(dp, , sizeof dp);
}
void dfs(int r,int f)
{
int lenr = G[r].size();
int lenc = color[r].size();
for(int j = ; j < lenr; j++){
int &v = G[r][j];
if(v==f) continue;
dfs(v,r);
}
for(int i = ; i < lenc; i++){
ll res = ;
int &c = color[r][i];
for(int j = ; j < lenr; j++){
int &v = G[r][j];
if(v==f) continue;
res = res*(cnt[v]-dp[v][c]+mod)%mod;
}
dp[r][c] = res;
cnt[r] = (cnt[r]+dp[r][c])%mod;
}
}
int main()
{
while(scanf("%d%d",&n,&m)==)
{
init();
int x, y;
for(int i = ; i < n; i++){
scanf("%d%d",&x,&y);
G[x].push_back(y);
G[y].push_back(x);
}
for(int i = ; i <= n; i++){
for(int j = ; j <= m; j++){
scanf("%d",&x);
if(x){
color[i].push_back(j);
}
}
} dfs(,); printf("%lld\n",cnt[]);
/* for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
printf("%d ",dp[i][j]);
}
cout<<endl;
}*/ }
return ;
}
2017 Wuhan University Programming Contest (Online Round) B Color 树形dp求染色方法数的更多相关文章
- 2017 Wuhan University Programming Contest (Online Round) Lost in WHU 矩阵快速幂 一个无向图,求从1出发到达n最多经过T条边的方法数,边可以重复经过,到达n之后不可以再离开。
/** 题目:Lost in WHU 链接:https://oj.ejq.me/problem/26 题意:一个无向图,求从1出发到达n最多经过T条边的方法数,边可以重复经过,到达n之后不可以再离开. ...
- 2017 Wuhan University Programming Contest (Online Round) C. Divide by Six 分析+模拟
/** 题目:C. Divide by Six 链接:https://oj.ejq.me/problem/24 题意:给定一个数,这个数位数达到1e5,可能存在前导0.问为了使这个数是6的倍数,且没有 ...
- 2017 Wuhan University Programming Contest (Online Round) D. Events,线段树区间更新+最值查询!
D. Events 线段树区间更新查询区间历史最小值,看似很简单的题意写了两天才写出来. 题意:n个数,Q次操作,每次操作对一个区间[l,r]的数同时加上C,然后输出这段区间的历史最小值. 思路:在线 ...
- zoj 4020 The 18th Zhejiang University Programming Contest Sponsored by TuSimple - G Traffic Light(广搜)
题目链接:The 18th Zhejiang University Programming Contest Sponsored by TuSimple - G Traffic Light 题解: 题意 ...
- The 16th Zhejiang University Programming Contest-
Handshakes Time Limit: 2 Seconds Memory Limit: 65536 KB Last week, n students participated in t ...
- 2017 Bangladesh National High School Programming Contest ( National Round, Senior Group ), NHSPC 2017 题解
[题目链接] A. Charm Is Not Always Enough 模拟一下就可以了. #include <bits/stdc++.h> using namespace std; i ...
- The 18th Zhejiang University Programming Contest Sponsored by TuSimple
Pretty Matrix Time Limit: 1 Second Memory Limit: 65536 KB DreamGrid's birthday is coming. As hi ...
- 写完代码就去吃饺子|The 10th Henan Polytechnic University Programming Contest
河南理工大学第十届校赛 很久没有组队打比赛了,好吧应该说很久没有写题了, 三个人一起玩果然比一个人玩有趣多了... 前100分钟过了4题,中途挂机100分钟也不知道什么原因,可能是因为到饭点太饿了?, ...
- The 15th Zhejiang University Programming Contest
a ZOJ 3860 求和大家不一样的那个数,签到,map水之 #include<cstdio> #include<map> using namespace std; map ...
随机推荐
- ethtool 命令输出的注意点--网卡参数
http://blog.csdn.net/msdnchina/article/details/70339689
- 第十五章 MySQL 数据库
学习要点:1.Web 数据库概述2.MySQL 的操作3.MySQL 常用函数4.SQL 语句详解5.phpMyadmin 一.Web数据库概述 现在,我们已经熟悉了PHP 的基础知识,这是我们想暂时 ...
- unity GPU bound or CPU bound
unity判断GPU CPUbound android 用unity profiler 里面的cpu时间 xcode有直接的显示
- D3.js系列——初步使用、选择元素与绑定数据
D3 的全称是(Data-Driven Documents),顾名思义可以知道是一个被数据驱动的文档.听名字有点抽象,说简单一点,其实就是一个 JavaScript 的函数库,使用它主要是用来做数据可 ...
- 项目打jar包,怎么把第三放jar包一起打入
<plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> < ...
- mybatis 一对多
person package com.kerwin.mybatis.pojo; import java.util.List; public class Person { private int id; ...
- Bitnami 2015
WordPress WordPress is one of the world's most popular web publishing platforms for building blogs a ...
- redis学习笔记——应用场景
最近在看redis入门指南,现在就自己的学习情况说说自己的理解. 字符串类型(String) 字符串类型是Redis中最基本的类型,能存储任意形式的字符串,包括二进制数据.如一张照片也可以用字符串类型 ...
- DirectShow控制台输出和保存视频设备名称
#include "windows.h" #include "TCHAR.h" #include <dshow.h> #include <ve ...
- paypal - 支付所遇到的问题,编码等等
1.ipn支付方式编码问题,paypal默认编码为 gb2312,需要登录paypal后台,进入用户信息设置>销售工具>更多销售工具>paypal按钮编码设置>更多选项,全部修 ...