题意:

给出一棵树,每个节点上有个权值。要找到一对字典序最小的点对\((u, v)(u < v)\),使得路径\(u \to v\)上所有节点权值的乘积模\(10^6 + 3\)的值为\(k\)。

分析:

比较经典的树分治。

对于分治过程中的一棵子树,我们统计两种情况:

  • 一端为重心的路径中,到某个顶点乘积为\(k\)的路径。
  • 两端在不同子树且过重心的路径中,乘积为\(k\)。

其他的递归到子树中去。

这里要预处理乘法逆元。

子树合并的时候,需要用到一个小技巧性的hash,参考九野的博客

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#define MP make_pair
using namespace std; typedef long long LL;
typedef pair<int, int> PII;
const int MOD = 1000000 + 3;
const int maxn = 100000 + 10;
const int INF = 0x3f3f3f3f; void read(int& x) {
x = 0;
char c = ' ';
while(c < '0' || c > '9') c = getchar();
while('0' <= c && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
} LL pow_mod(LL a, LL n) {
LL ans = 1;
while(n) {
if(n & 1) ans = ans * a % MOD;
a = a * a % MOD;
n >>= 1;
}
return ans;
} int mul_mod(int a, int b) { return (LL)a * b % MOD; } int inverse(int x) { return pow_mod(x, MOD - 2); } int n, k;
int a[maxn], inv[MOD]; struct Edge
{
int v, nxt;
Edge() {}
Edge(int v, int nxt): v(v), nxt(nxt) {}
}; int ecnt, head[maxn];
Edge edges[maxn * 2]; void AddEdge(int u, int v) {
edges[ecnt] = Edge(v, head[u]);
head[u] = ecnt++;
} PII ans; bool del[maxn];
int fa[maxn], sz[maxn]; void dfs(int u) {
sz[u] = 1;
for(int i = head[u]; ~i; i = edges[i].nxt) {
int v = edges[i].v;
if(del[v] || v == fa[u]) continue;
fa[v] = u;
dfs(v);
sz[u] += sz[v];
}
} PII findCenter(int u, int t) {
PII ans(INF, u);
int m = 0;
for(int i = head[u]; ~i; i = edges[i].nxt) {
int v = edges[i].v;
if(del[v] || v == fa[u]) continue;
ans = min(ans, findCenter(v, t));
m = max(m, sz[v]);
}
m = max(m, t - sz[u]);
ans = min(ans, MP(m, u));
return ans;
} int tot, path[maxn], num[maxn];
int has[MOD], id[MOD], cnt; void getproduct(int u, int p, LL prod) {
path[++tot] = prod; num[tot] = u;
for(int i = head[u]; ~i; i = edges[i].nxt) {
int v = edges[i].v;
if(del[v] || v == p) continue;
getproduct(v, u, mul_mod(prod, a[v]));
}
} PII getpair(int a, int b) {
if(a < b) return MP(a, b);
else return MP(b, a);
} void solve(int u) {
fa[u] = 0;
dfs(u);
int s = findCenter(u, sz[u]).second;
del[s] = true; for(int i = head[s]; ~i; i = edges[i].nxt) {
int v = edges[i].v;
if(del[v]) continue;
solve(v);
} cnt++;
for(int i = head[s]; ~i; i = edges[i].nxt) {
int v = edges[i].v;
if(del[v]) continue;
tot = 0;
getproduct(v, s, a[v]);
int m = mul_mod(k, inv[a[s]]);
for(int i = 1; i <= tot; i++) {
if(path[i] == m) {
PII tmp = getpair(num[i], s);
if(!ans.first || tmp < ans) ans = tmp;
}
int m2 = mul_mod(k, mul_mod(inv[path[i]], inv[a[s]]));
if(has[m2] == cnt) {
PII tmp = getpair(num[i], id[m2]);
if(!ans.first || tmp < ans) ans = tmp;
}
}
for(int i = 1; i <= tot; i++) {
if(has[path[i]] != cnt || (has[path[i]] == cnt && id[path[i]] > num[i])) {
has[path[i]] = cnt;
id[path[i]] = num[i];
}
}
} del[s] = false;
} int main()
{
for(int i = 1; i < MOD; i++) inv[i] = inverse(i); while(scanf("%d%d", &n, &k) == 2) {
for(int i = 1; i <= n; i++) read(a[i]);
ecnt = 0;
memset(head, -1, sizeof(head));
for(int i = 1; i < n; i++) {
int u, v; read(u); read(v);
AddEdge(u, v);
AddEdge(v, u);
} ans = MP(0, 0);
memset(has, 0, sizeof(has));
cnt = 0;
solve(1);
if(!ans.first) puts("No solution");
else printf("%d %d\n", ans.first, ans.second);
} return 0;
}

HDU 4812 D Tree 树分治的更多相关文章

  1. HDU 4812 D Tree 树分治+逆元处理

    D Tree Problem Description   There is a skyscraping tree standing on the playground of Nanjing Unive ...

  2. hdu 4812 D Tree(树的点分治)

    D Tree Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others) Total ...

  3. HDU - 4812 D Tree 点分治

    http://acm.hdu.edu.cn/showproblem.php?pid=4812 题意:有一棵树,每个点有一个权值要求找最小的一对点,路径上的乘积mod1e6+3为k 题解:点分治,挨个把 ...

  4. HDU 4812 D Tree 树分区+逆+hash新位置

    意甲冠军: 特定n点树 K 以下n号码是正确的点 以下n-1行给出了树的侧. 问: 所以,如果有在正确的道路点图的路径 % mod  = K 如果输出路径的两端存在. 多条路径则输出字典序最小的一条. ...

  5. HDU 4812 D Tree

    HDU 4812 思路: 点分治 先预处理好1e6 + 3以内到逆元 然后用map 映射以分治点为起点的链的值a 成他的下标 u 然后暴力跑出以分治点儿子为起点的链的值b,然后在map里查找inv[b ...

  6. 【BZOJ-1468】Tree 树分治

    1468: Tree Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1025  Solved: 534[Submit][Status][Discuss] ...

  7. POJ 1741 Tree 树分治

    Tree     Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...

  8. POJ 1741.Tree 树分治 树形dp 树上点对

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 24258   Accepted: 8062 Description ...

  9. poj 1744 tree 树分治

    Tree Time Limit: 1000MS   Memory Limit: 30000K       Description Give a tree with n vertices,each ed ...

随机推荐

  1. Windows2

    windows如何打开dvd, iso镜像文件 .iso后缀的文件是一个压缩文件, 使用Winrar等压缩工具即可打开 windows7如何下载Visual Studio 2010(2010是流行的开 ...

  2. react中constructor和super()以及super(props)的区别。

    react中这两个API出镜率超级高,但是一直不太懂这到底是干嘛的,有什么用:今天整理一下,方便自己查看同时方便大家. 1.constructor( )-----super( )的基本含义 const ...

  3. 【转】Java Cipher类 DES算法(加密与解密)

    Java Cipher类 DES算法(加密与解密) 1.加密解密类 import java.security.*; import javax.crypto.*; import java.io.*; / ...

  4. Ecshop:ecshop nginx下实现url静态化

    1.在nginx/conf/tuwen.com.conf中添加: include ecshop.conf; 2.编辑nginx/ecshop.conf: location / { rewrite &q ...

  5. 将服务器上的文件通过HttpWebRequest下载到本地

    外网地址需要先映射. string path=""; path=@"http://222.92.71.116/P2Foundation/Images/logo.gif&q ...

  6. IIS 7.0的根文件(applicationHost.config)位置及说明

    位置 C:\Windows\System32\inetsrv\config\applicationHost.config 说明 https://www.microsoft.com/taiwan/tec ...

  7. UVA 1623 Enther the Dragon 神龙喝水 (贪心)

    贪心,每次遇到一个满水的湖要下暴雨的时候,就往前找之前最后一次满水之后的第一个没有下雨的且没有被用掉天day1. 因为如果不选这day1,那么之后的湖不一定能选上这一天.如果这一天后面还有没有下雨的天 ...

  8. 2018.3.12 Leecode习题 给定一个整数数列,找出其中和为特定值的那两个数。

    给定一个整数数列,找出其中和为特定值的那两个数. 你可以假设每个输入都只会有一种答案,同样的元素不能被重用. 示例: 给定 nums = [2, 7, 11, 15], target = 9; 因为 ...

  9. Vue -- 仿照商城分类竖向侧边栏点击居中效果(横向原理相同)

    github代码地址 效果图

  10. spring mvc + freemarker 整合

    <?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.s ...