HDU 4812 D Tree 点分治

10 篇文章 0 订阅
2 篇文章 0 订阅

见鬼了
如果把第59~60行与上面的循环合并会WA。。。
怎么想都没有问题
求教

由于树链分为经过根与在子树中2种情况。
而在子树中的情况递归转化为根的情况。
而根的情况就简单了,扫一遍子树,处理出所有可能走出的乘积,然后哈希判定即可。开map好像超时了。。应该是我写的比较渣。。

#include <cstdio>
#include <cstring>
#include <algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
#define FOR(i,j,k) for(i=j;i<=k;++i)
#define ms(i) memset(i,0,sizeof i)
const int N = 100005, M = N * 2, inf = 2147483647, MOD = 1000003;
using namespace std;
int a[N], inv[MOD], k;
pair<int, int> ans;
int vis[M], p[M], v[M], h[N], w[M], node, rt, cnt;
int dep[N], g[N], sz[N];
int hts[MOD], hval[MOD], ts = 0;
int dkey[N], dval[N], dtop;
void map_insert(int key, int val) {
    if (hts[key] == ts) hval[key] = min(hval[key], val);
    else hts[key] = ts, hval[key] = val;
}
void add(int a, int b) {
    p[++cnt] = h[a]; v[cnt] = b; h[a] = cnt;
}
void root(int x, int fa) {
    sz[x] = 1; g[x] = 0;
    for (int i = h[x]; i; i = p[i])
        if (v[i] != fa && !vis[i]) {
            root(v[i], x);
            sz[x] += sz[v[i]];
            g[x] = max(g[x], sz[v[i]]);
        }
    g[x] = max(g[x], node - sz[x]);
    if (g[x] < g[rt]) rt = x;
}
int get_root(int x, int fa, int sz) {
    rt = 0; node = sz; g[0] = inf;
    root(x, fa); return rt;
}
void dfs_seq(int x, int fa, int dep) {
    dkey[++dtop] = dep; dval[dtop] = x;
    for (int i = h[x]; i; i = p[i])
        if (!vis[i] && v[i] != fa)
            dfs_seq(v[i], x, 1ll * dep * a[v[i]] % MOD);
}
void update_ans(int a, int b) {
    if (a > b) swap(a, b);
    ans = min(ans, make_pair(a, b));
}
void work(int x) {
    int i, j; ++ts;
    for (i = h[x]; i; i = p[i]) if (!vis[i]) {
        dtop = 0; dfs_seq(v[i], x, a[v[i]]);
        FOR(j,1,dtop) {
            int p = 1ll * dkey[j] * a[x] % MOD, q = 1ll * k * inv[p] % MOD;
            if (p == k) update_ans(x, dval[j]);
            if (hts[q] == ts) update_ans(hval[q], dval[j]);
        }
        FOR(j,1,dtop) map_insert(dkey[j], dval[j]);
    }
    for (i = h[x]; i; i = p[i]) if (!vis[i]) {
        vis[i] = vis[i ^ 1] = 1;
        work(get_root(v[i], x, sz[v[i]]));
    }
}
int main() {
    int i, n, x, y;
    inv[1] = 1;
    for (i = 2; i < MOD; ++i) inv[i] = 1ll * (MOD - MOD / i) * inv[MOD % i] % MOD;
    while (scanf("%d%d", &n, &k) == 2) {
        ms(vis); ms(h); cnt = 1; ans = make_pair(inf, inf);
        FOR(i,1,n) scanf("%d", a + i);
        FOR(i,2,n) scanf("%d%d", &x, &y), add(x, y), add(y, x);
        work(get_root(1, 0, n));
        if (ans == make_pair(inf, inf)) puts("No solution");
        else printf("%d %d\n", ans.first, ans.second);
    }
    return 0;
}

D Tree

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 3285 Accepted Submission(s): 591

Problem Description

There is a skyscraping tree standing on the playground of Nanjing University of Science and Technology. On each branch of the tree is an integer (The tree can be treated as a connected graph with N vertices, while each branch can be treated as a vertex). Today the students under the tree are considering a problem: Can we find such a chain on the tree so that the multiplication of all integers on the chain (mod 106 + 3) equals to K?
Can you help them in solving this problem?

Input

There are several test cases, please process till EOF.
Each test case starts with a line containing two integers N(1 <= N <= 105) and K(0 <=K < 106 + 3). The following line contains n numbers vi(1 <= vi < 106 + 3), where vi indicates the integer on vertex i. Then follows N - 1 lines. Each line contains two integers x and y, representing an undirected edge between vertex x and vertex y.

Output

For each test case, print a single line containing two integers a and b (where a < b), representing the two endpoints of the chain. If multiply solutions exist, please print the lexicographically smallest one. In case no solution exists, print “No solution”(without quotes) instead.
For more information, please refer to the Sample Output below.

Sample Input

5 60
2 5 2 3 3
1 2
1 3
2 4
2 5
5 2
2 5 2 3 3
1 2
1 3
2 4
2 5

Sample Output

3 4
No solution

Hint

  1. “please print the lexicographically smallest one.”是指: 先按照第一个数字的大小进行比较,若第一个数字大小相同,则按照第二个数字大小进行比较,依次类推。

  2. 若出现栈溢出,推荐使用C++语言提交,并通过以下方式扩栈:
    #pragma comment(linker,"/STACK:102400000,102400000")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值