hdu5977 Garden of Eden
都不好意思写题解了
跑了4000多ms
纪念下自己A的第二题
(我还有一道freetour II wa20多发没A。。。呜呜呜
#include<bits/stdc++.h>
using namespace std;
#define sz(X) ((int)X.size())
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define index Index
typedef long long ll;
const int N = 5e4+5;
const int INF = 0x3f3f3f3f;
const double pi = acos(-1.0);
int n,k,K;
ll ans;
int ty[N];
struct Node{
int to,nx;
}E[N<<1];
int head[N], tot, vis[N];
void add(int u,int v) {
E[tot].to = v; E[tot].nx = head[u]; head[u] = tot++;
}
/***************WeightRoot************/
int all, num, center;
int pp[N], nodes[N];
void findRoot(int x,int pre) {
nodes[x] = 1; pp[x] = 0;
for(int i = head[x]; ~i; i = E[i].nx) {
int y = E[i].to; if(y == pre || vis[y]) continue;
findRoot(y,x);
nodes[x] += nodes[y];
pp[x] = max(pp[x], nodes[y]);
}
pp[x] = max(pp[x], all-nodes[x]);
if(pp[x] < num) {
num = pp[x]; center = x;
}
}
int getRoot(int root,int sn) {
num = INF; all = sn; center = root;
findRoot(root, -1);
return center;
}
/****************treecdq**********/
ll has[1050];
ll dp[12][1050];
void getdp(int x, int pre, int num) {
has[num] ++;
for(int i = head[x]; ~i; i = E[i].nx) {
int y = E[i].to; if(y == pre || vis[y]) continue;
getdp(y,x, num|ty[y]);
}
}
ll Cal(int x, int chu) {
ll ret = 0;
memset(has,0,sizeof(has));
getdp(x,x,chu|ty[x]);
for(int i = 0; i <= K; ++i) dp[0][i] = has[i];
for(int i = 1; i <= k; ++i) {
for(int j = 0; j <= K; ++j) {
dp[i][j] = dp[i-1][j];
if(!(j&(1<<(i-1)))) dp[i][j] += dp[i-1][j^(1<<(i-1))];
}
}
for(int i = 0; i <= K; ++i) ret += has[i]* dp[k][i^K];
return ret;
}
void work(int x) {
vis[x] = 1;
ans += Cal(x,0);
for(int i = head[x]; ~i; i = E[i].nx) {
int y = E[i].to; if(vis[y]) continue;
ans -= Cal(y,ty[x]);
work(getRoot(y,nodes[y]));
}
}
int main(){
while(~scanf("%d %d",&n,&k)) {
K = (1<<k)-1;
memset(vis,0,sizeof(vis));
memset(head,-1,sizeof(head)); tot = 0;
for(int i = 1; i <= n; ++i) {
int a; scanf("%d",&a); a--;
ty[i] = 1<<a;
}
for(int i = 1; i < n; ++i) {
int a,b; scanf("%d %d",&a,&b);
add(a, b); add(b, a);
}
ans = 0;
work(getRoot(1,n));
printf("%lld\n", ans);
}
return 0;
}
hdu5977 Garden of Eden的更多相关文章
- HDU5977 Garden of Eden(树的点分治)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5977 Description When God made the first man, he ...
- hdu-5977 Garden of Eden(树分治)
题目链接: Garden of Eden Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/ ...
- HDU-5977 - Garden of Eden 点分治
HDU - 5977 题意: 给定一颗树,问树上有多少节点对,节点对间包括了所有K种苹果. 思路: 点分治,对于每个节点记录从根节点到这个节点包含的所有情况,类似状压,因为K<=10.然后处理每 ...
- HDU5977 Garden of Eden 【FMT】【树形DP】
题目大意:求有所有颜色的路径数. 题目分析:参考codeforces997C,先利用基的FMT的性质在$O(2^k)$做FMT,再利用只还原一位的特点在$O(2^k)$还原,不知道为什么网上都要点分治 ...
- uva10001 Garden of Eden
Cellular automata are mathematical idealizations of physical systems in which both space and time ar ...
- HDU 5977 Garden of Eden(点分治求点对路径颜色数为K)
Problem Description When God made the first man, he put him on a beautiful garden, the Garden of Ede ...
- Garden of Eden
Garden of Eden Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- (模板)hdoj5977 Garden of Eden(点分治)
题目链接:https://vjudge.net/problem/HDU-5977 题意:给一颗树,每个结点上有一个权值a[i],a[i]<=10,求有多少条路径满足这条路径上所有权值的结点都出现 ...
- HDU 5977 Garden of Eden
题解: 路径统计比较容易想到点分治和dp dp的话是f[i][j]表示以i为根,取了i,颜色数状态为j的方案数 但是转移这里如果暴力转移就是$(2^k)^2$了 于是用FWT优化集合或 另外http: ...
随机推荐
- wireshark抓包图解 TCP三次握手/四次挥手详解[转]
原文链接:http://www.seanyxie.com/wireshark%E6%8A%93%E5%8C%85%E5%9B%BE%E8%A7%A3-tcp%E4%B8%89%E6%AC%A1%E6% ...
- JMS基础篇(二)
简介 异构集成是消息发挥作用的一个领域,大型公司内部可能会遇到很多的平台,Java,.net或者公司自己的平台等. 传送消息还应该支持异步机制,以提高系统整体的性能.异步传输一条消息意味着,发送者不必 ...
- POJ Building roads [二分答案 2SAT]
睡觉啦 #include <iostream> #include <cstdio> #include <cstring> #include <algorith ...
- mysql 军规 (转载)
导语 来自一线的实战经验 每一条军规背后都是血淋淋教训 不要华丽,只要实用 若有一条让你受益,慰矣 主要针对数据库开发人员 总是在灾难发生后,才想起容灾的重要性 总是在吃过亏后,才记得曾经有人提醒过 ...
- Windows 定时任务对数据库进行操作
定时对数据库进行操作可以用mysql的event事件来完成,但是只有mysql5.1后的才支持,所以有一定的局限性,也可以通过其他的mysql管理软件实现,而我发现Windows本身就有个定时任务的功 ...
- POI导出多张图片到Excel
package com.sun.office.excel; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStr ...
- mysql 密码过期问题
问题描述: Your password has expired. To log in you must change it using a client that supports expired p ...
- MAC下secretCRT使用技巧(转)
1.打开secureCRT,按alt+b,可以调出快速启动栏,我相信secureCRT的忠实用户,都会保存一堆的sessions.2.按ctrl,可以同时选中多个session,再点击连接,可快速连接 ...
- linux 下创建GRE隧道
其他国家的互联网如同一个孤岛.要想访问国外网站异常的缓慢,甚至被和谐了.可以建立一条隧道来避免这种情况,下面说说GRE隧道如何建立. 1. GRE介绍 GRE隧道是一种IP-over-IP的隧道,是通 ...
- zabbix-agent 启动不起来
遇到一个问题 zabbix-agent 一直启动不起来 查看Zabbix Agent日志文件才究其原因. tail /var/log/zabbix/zabbix_agentd.logzabbix_a ...