hdu 5735 Born Slippy 暴力
Born Slippy
题目连接:
http://acm.hdu.edu.cn/showproblem.php?pid=5735
Description
Professor Zhang has a rooted tree, whose vertices are conveniently labeled by 1,2,...,n. And the i-th vertex is assigned with weight wi.
For each s∈{1,2,...,n}, Professor Zhang wants find a sequence of vertices v1,v2,...,vm such that:
1. v1=s and vi is the ancestor of vi−1 (1<i≤m).
2. the value f(s)=wv1+∑i=2mwvi opt wvi−1 is maximum. Operation x opt y denotes bitwise AND, OR or XOR operation of two numbers.
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contains an integer n and a string opt (2≤n≤216,opt∈{AND,OR,XOR}) -- the number of vertices and the operation. The second line contains n integers w1,w2,...,wn (0≤wi<216). The thrid line contain n−1 integers f2,f3,...,fn (1≤fi<i), where fi is the father of vertex i.
There are about 300 test cases and the sum of n in all the test cases is no more than 106.
Output
For each test case, output an integer S=(∑i=1ni⋅f(i)) mod (109+7).
Sample Input
3
5 AND
5 4 3 2 1
1 2 2 4
5 XOR
5 4 3 2 1
1 2 2 4
5 OR
5 4 3 2 1
1 2 2 4
Sample Output
91
139
195
Hint
题意
给你一棵树,树上点有点权,对于每个点,你需要找到到根的那条链上的一个子序列。
使得f[i] = w[v[i]] + sigma w[v[i]] opt w[v[i+1]] 最大。
然后输出sigma(if[i])%mod
题解:
不会正解,n^2dp非常简单,很容易就能想到
然后感觉这道题的数据比较难造,就直接暴力了,然后一发就过了。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 70000;
const int mod = 1e9+7;
long long dp[maxn],w[maxn];
int n;
vector<int>E[maxn];
string opt;
set<pair<long long,int> > S;
set<pair<long long,int> >::iterator it;
long long solve(long long x,long long y){
if(opt=="XOR")return x^y;
if(opt=="AND")return x&y;
if(opt=="OR")return x|y;
}
void dfs(int x){
if(S.size()!=0){
int tot = 0;
for(it = S.begin();it!=S.end()&&tot<100;it++,tot++){
dp[x] = max(dp[x],-(it->first)+solve(w[it->second],w[x]));
}
}
S.insert(make_pair(-dp[x],x));
for(int i=0;i<E[x].size();i++){
int v = E[x][i];
dfs(v);
}
S.erase(make_pair(-dp[x],x));
}
void solve(){
scanf("%d",&n);
for(int i=1;i<=n;i++)E[i].clear(),dp[i]=0;
cin>>opt;
for(int i=1;i<=n;i++)
scanf("%d",&w[i]);
for(int i=2;i<=n;i++){
int a;scanf("%d",&a);
E[a].push_back(i);
}
dfs(1);
long long ans = 0;
for(int i=1;i<=n;i++){
ans = (ans+i*(dp[i]%mod+w[i]%mod)) % mod;
}
printf("%I64d\n",ans);
}
int main(){
int t;
scanf("%d",&t);
while(t--)solve();
return 0;
}
hdu 5735 Born Slippy 暴力的更多相关文章
- HDU 5735 Born Slippy(拆值DP+位运算)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5735 [题目大意] 给出一棵树,树上每个节点都有一个权值w,w不超过216,树的根为1,从一个点往 ...
- HDU 5735 - Born Slippy
题意: 一棵 n 个节点的根树,i 节点权重 wi 对每一个节点s,找到这样一个长 m 的标号序列 v : 1. vi是vi-1 的祖先 2. f[s] = w[vi] + ∑(i=2, m) (w[ ...
- hdu 5461 Largest Point 暴力
Largest Point Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...
- hdu 5762 Teacher Bo 暴力
Teacher Bo 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5762 Description Teacher BoBo is a geogra ...
- HDU 1333 基础数论 暴力
定义一种数位simth数,该数的各位之和等于其所有质因子所有位数字之和,现给出n求大于n的最小该种数,n最大不超过8位,那么直接暴力就可以了. /** @Date : 2017-09-08 14:12 ...
- HDU 4618 Palindrome Sub-Array 暴力
Palindrome Sub-Array 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4618 Description A palindrome s ...
- HDU 2089 不要62 | 暴力(其实是个DP)
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2089 题解: 暴力水过 #include<cstdio> #include<algor ...
- HDU 6115 Factory LCA,暴力
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6115 题意:中文题面 分析:直接维护LCA,然后暴力枚举集合维护答案即可. #include < ...
- HDU 5636 Shortest Path 暴力
Shortest Path 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5636 Description There is a path graph ...
随机推荐
- 【NOI】2017 蚯蚓排队(BZOJ 4943,LOJ 2303) 模拟+hash
[题目]#2303. 「NOI2017」蚯蚓排队 [题意]给定n条长度不超过6的蚯蚓,初始各自在一个队伍.m次操作:1.将i号蚯蚓和j号蚯蚓的队伍合并(保证i为队尾,j为队首).2.将i号蚯蚓和它后面 ...
- 20155235 2016-2017-2 《Java程序设计》第8周学习总结
20155235 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 第十四章 NIO与NIO2 认识NIO NIO概述 Channel架构与操作 Buffer架 ...
- vue实践中的狗血事件之:mock数据引发的血坑
在项目实践中,遇到了这么一档子事 开发环境下,很快乐,什么事儿都没有,于是想打包一下测一下自动登录的效果 好家伙,一开始登录没有效,改来改去,最后连路由都切换不了, 明明开发环境下好好的,为毛打包后就 ...
- Python 入门基础7 --文件操作
今日目录: 一.文件处理 1.什么是文件 2.为何用文件 3.如何用文件 4.文件操作 5.常用方法 6.文件内指针的移动 7.with的使用 一.文件处理 1. 什么是文件 文件是操作系统为用户/应 ...
- Madgwick IMU Filter
论文链接:http://202.114.96.204/cache/13/03/x-io.co.uk/35c82431852f2aa7d0feede9dc138626/madgwick_internal ...
- Maven从私服上下载所需jar包——(十四)
1.修改settings.xml 将下面代码添加到settings.xml中 <profile> <!--profile的id--> <id>dev</id& ...
- 安装odbc驱动
1.下载对应的驱动 (32位/64位) http://www.oracle.com/technetwork/database/database-technologies/instant-client/ ...
- vistual studio 去除 git 源代码 绑定
第一次碰到这个问题,想将源代码签入TFS管理.添加到源码管理后,默认添加到Git源码管理. 研究过后,发现: 1)删除框内文件 2)Vs->工具->选项->源代码管理->插件管 ...
- ERP渠道信息的修改和渠道联系记录(二十三)
用例图: 前端代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Cha ...
- 把任意exe程序注册成windows系统服务
某gae代理软件每次开机都需要手动启动,就算添加成开机启动项,在win8.1下权限的管理更加严格,开机时并不能成功启动软件(无人值守时开机),因此在网上搜索把exe注册成系统服务的办法,找到论坛两个帖 ...