hdu 5877/ 2016 ACM/ICPC Dalian Online 1010 Weak Pair
分析:树上的节点祖先与儿子的关系,一般就会想到dfs序。正解就是对树先进行dfs序排列,再将问题转化到树状数组统计个数。应该把节点按照权值从大到小排序,这样对于a[i],K/a[i]就是从小到大的顺序。这样更新树状数组就不会造成计算的混乱了。
多组数据没有每次先清除边我真是太智障了。
代码:
/*****************************************************/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <map>
#include <set>
#include <ctime>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define offcin ios::sync_with_stdio(false)
#define sigma_size 26
#define lson l,m,v<<1
#define rson m+1,r,v<<1|1
#define slch v<<1
#define srch v<<1|1
#define sgetmid int m = (l+r)>>1
#define LL long long
#define ull unsigned long long
#define mem(x,v) memset(x,v,sizeof(x))
#define lowbit(x) (x&-x)
#define bits(a) __builtin_popcount(a)
#define mk make_pair
#define pb push_back
#define fi first
#define se second
const int INF = 0x3f3f3f3f;
const LL INFF = 1e18;
const double pi = acos(-1.0);
const double inf = 1e18;
const double eps = 1e-9;
const LL mod = 1e9+7;
const int maxmat = 10;
const ull BASE = 31;
/*****************************************************/
const int maxn = 1e5 + 5;
int in[maxn], out[maxn], dfs_clock;
int a[maxn], outdgree[maxn];
int sum[maxn];
std::vector<int> G[maxn];
int N;
LL K;
struct Node {
int val, id;
bool operator <(const Node &rhs) const {
return val < rhs.val;
}
}node[maxn];
void init() {
mem(sum, 0);
mem(node, 0);
mem(in, 0);
mem(out, 0);
mem(outdgree, 0);
dfs_clock = 0;
}
void dfs(int u, int fa) {
if (in[u]) return;
in[u] = ++ dfs_clock;
for (int i = 0; i < G[u].size(); i ++) {
int v = G[u][i];
if (v == fa) continue;
dfs(v, u);
}
out[u] = dfs_clock;
}
void add(int x) {
while (x <= N) {
sum[x] ++;
x += lowbit(x);
}
}
int query(int x) {
int res = 0;
while (x) {
res += sum[x];
x -= lowbit(x);
}
return res;
}
int main(int argc, char const *argv[]) {
int T;
cin>>T;
while (T --) {
init();
scanf("%d%I64d", &N, &K);
for (int i = 1; i <= N; i ++) G[i].clear();
for (int i = 1; i <= N; i ++) {
scanf("%d", &node[i].val);
node[i].id = i;
}
for (int i = 0; i < N - 1; i ++) {
int u, v;
scanf("%d%d", &u, &v);
outdgree[v] ++;
G[u].pb(v);
G[v].pb(u);
}
sort(node + 1, node + N + 1);
int root = 1;
for (int i = 1; i <= N; i ++) if (!outdgree[i]) {
root = i;
break;
}
dfs(root, -1);
int pos = 1;
LL ans = 0;
for (int i = N; i >= 1; i --) {
LL tmp = K / (LL)node[i].val;
int id = node[i].id;
while (node[pos].val <= tmp && pos <= N)
add(in[node[pos ++].id]);
ans += query(out[id]) - query(in[id]);
}
cout<<ans<<endl;
}
return 0;
}
hdu 5877/ 2016 ACM/ICPC Dalian Online 1010 Weak Pair的更多相关文章
- hdu 5868 2016 ACM/ICPC Asia Regional Dalian Online 1001 (burnside引理 polya定理)
Different Circle Permutation Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K ...
- 2016 ACM/ICPC Asia Regional Dalian Online 1010 Weak Pair dfs序+分块
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submissio ...
- HDU 5874 Friends and Enemies 【构造】 (2016 ACM/ICPC Asia Regional Dalian Online)
Friends and Enemies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- HDU 5875 Function 【倍增】 (2016 ACM/ICPC Asia Regional Dalian Online)
Function Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- HDU 5873 Football Games 【模拟】 (2016 ACM/ICPC Asia Regional Dalian Online)
Football Games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- HDU 5876 Sparse Graph 【补图最短路 BFS】(2016 ACM/ICPC Asia Regional Dalian Online)
Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)To ...
- 2016 ACM/ICPC Asia Regional Dalian Online 1002/HDU 5869
Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K ( ...
- 2016 ACM/ICPC Asia Regional Dalian Online 1006 /HDU 5873
Football Games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- HDU 5889 Barricade 【BFS+最小割 网络流】(2016 ACM/ICPC Asia Regional Qingdao Online)
Barricade Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
随机推荐
- 并发案例--ScheduledExecutorService用法
InstanceFactory.getInstance(ScheduledExecutorService.class).schedule(new Callable<Object>() { ...
- 学习OpenCV——行人检测&人脸检测(总算运行出来了)
之前运行haar特征的adaboost算法人脸检测一直出错,加上今天的HOG&SVM行人检测程序,一直报错. 今天总算发现自己犯了多么白痴的错误——是因为外部依赖项lib文件没有添加完整,想一 ...
- Python快速建站系列-Part.Three-注册和登录
|版权声明:本文为博主原创文章,未经博主允许不得转载. 上一个Part已经给TSSS编好了一个简单的Web服务,网页的基础模版也写好了,那从这个Part开始就慢慢增加编写功能. 先写基础功能:注册和登 ...
- Unity操作
聚焦到游戏物体: Hierarchy界面选中需要聚焦的物体,双击或者使用快捷键“F”: 在Scene面板中选中物体,使用快捷键“F” 放大缩小物体: alt+鼠标右键:鼠标滑轮 从各个角度观察 ...
- Openstack安全规则说明
openstack的安全规则,主要是在网络控制器,nova-network中进行的端口限制,所以我们需要对规则进行定制. 定制通用安全组规则 通用安全组规则主要包括2个,1是支持ping的icmp协议 ...
- 遍历对象所有属性(json对象)
var response = { "status": 1, "message": "\u6210\u529f", "data&qu ...
- CentOS7安装mysql5.6.26
linux系统CentOS7 到http://mirrors.sohu.com/mysql/下载想要的mysql版本 这里用到的是 mysql-5.6.26-linux-glibc2.5-x86_64 ...
- Android 图片的放大缩小拖拉
package com.example.ImageView; import android.annotation.SuppressLint; import android.content.Contex ...
- "Unity测试系列"文章索引
对Unity各种API的细节进行测试 Common 一些Unity基础操作的性能测试 Animation/Animator Animation Play/Stop测试 关于Animation动画事件的 ...
- EventBus使用详解(二)——EventBus使用进阶
一.概述 前一篇给大家装简单演示了EventBus的onEventMainThread()函数的接收,其实EventBus还有另外有个不同的函数,他们分别是: 1.onEvent2.onEventMa ...