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 ...
随机推荐
- PRML读书笔记——Introduction
1.1. Example: Polynomial Curve Fitting 1. Movitate a number of concepts: (1) linear models: Function ...
- JAVA并行框架:Fork/Join
一.背景 虽然目前处理器核心数已经发展到很大数目,但是按任务并发处理并不能完全充分的利用处理器资源,因为一般的应用程序没有那么多的并发处理任务.基于这种现状,考虑把一个任务拆分成多个单元,每个单元分别 ...
- LaTeX插入图表方法 Lists of tables and figures
Lists of tables and figures A list of the tables and figures keep the information organized and prov ...
- Eclipse下Tomcat插件的安装
在Eclipse下安装Tomcat插件使开发,编译,发布变的相当的简单,下面就说一下安装的过程,很简单的: 1.先下载一个tomcat插件 地址:http://www.eclipsetotale.co ...
- SQL2005中的事务与锁定(五)- 转载
------------------------------------------------------------------------ -- Author : HappyFlyStone - ...
- Android 实例子源代码文件下载地址380个合集
android 城市列表特效 - 触摸查找源码 .rar: http://www.t00y.com/file/64337887 android 日记系统源码(数据库的基本操作) .rar: htt ...
- python 笔记
第一周2016/9/11 Python 2.0和3.0的区别 3.0 的模块名改了和之前的2.0 不一样 #!/usr/bin/env python # -*- coding:utf-8 -*- ...
- JAVA线程池中的Callable和Future
import java.util.Random; import java.util.concurrent.Callable; import java.util.concurrent.Completio ...
- CentOS下LAMP一键yum安装脚本
本脚本适用环境: 系统支持:CentOS/Redhat/Fedora 内存要求:≥64M 硬盘要求:2GB以上的剩余空间 服务器必须配置好软件源和可连接外网 必须具有系统 root 权限 建议使用干净 ...
- MariaDB链接超时优化
查看mysql server超时时间: MariaDB [(none)]> use xspeeder; MariaDB [xspeeder]> show global variables ...