HDU 4776 Ants tire+优先队列

【题目大意】

有一棵树,树上一条简单路径(u,v)的价值定义为这条路径的所有边的异或和。求这些价值的第k大。

【思路】

如果我们统计根到节点u,路径的异或和为a[u],显然,路径(u,v)的价值为a[u]^a[v]。问题变为了从n个数中选两个不同的数,求异或,找第k大。因为k其实不是很大,我们可以考虑将1-max(k)的价值都找出来。用a[]建立tire树,首先枚举路径的左端点,通过tire找到一个右端点,使得其异或值最大。把这些数加入优先队列,显然,这些数中,最大的那个,就是第一大的价值。然后把这个价值删掉,通过原来的左端点,再找一个右端点,使其异或值变成次大,并把这个值加入优先队列。这个时候,队列中最大的元素是第二大的价值。这样一直找下去,就能把所有k都找到。

#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#include<cctype>
#include<string>
#include<algorithm>
#include<iostream>
#include<ctime>
#include<map>
#include<set>
using namespace std;
#define MP(x,y) make_pair((x),(y))
#define PB(x) push_back(x)
typedef __int64 LL;
//typedef unsigned __int64 ULL;
/* ****************** */
const LL INF = 1LL<<55;
const double INFF = 1e100;
const double eps = 1e-8;
const LL mod = 10000000007LL;
const int NN = 100010;
const int MM = 5000010;
/* ****************** */

const int limit = 59;

struct G
{
    int v, next;
    LL w;
}E[NN*2];
int p[NN], T;
struct node
{
    LL x, x_xor;
    int num;
    node(LL a = 0,LL b = 0,int c = 0):x(a),x_xor(b),num(c){}
    bool operator<(const node &tt)const
    {
        return x_xor < tt.x_xor;
    }
};
LL a[NN];
struct Tire
{
    int num;
    int ch[2];
    void init()
    {
        num = 0;
        memset(ch, -1, sizeof(ch));
    }
}tire[NN*60];
struct Q
{
    int k, id;
    bool operator<(const Q &tt)const
    {
        return k < tt.k;
    }
}q[NN];
LL anss[NN];

void add(int u,int v,LL w)
{
    E[T].v = v;
    E[T].w = w;
    E[T].next = p[u];
    p[u] = T++;
}

void dfs(int u,int fa)
{
    int i, v;
    for(i = p[u]; i + 1; i = E[i].next)
    {
        v = E[i].v;
        if(v == fa)continue;
        a[v] = a[u]^E[i].w;
        dfs(v, u);
    }
}

void tire_insert(LL val,int root,int &tire_cnt)
{
    int i, x, p = root;
    for(i = limit; i >=0; i --)
    {
        if(val&(1LL<<i))
            x = 1;
        else
            x = 0;
        if(tire[p].ch[x]==-1)
        {
            tire[p].ch[x] = ++tire_cnt;
            tire[tire_cnt].init();
        }
        p = tire[p].ch[x];
    }
    tire[p].num ++;
}
LL tire_find_max(LL val,int root,int &num)
{
    int i, x, p = root;
    LL ans = 0;
    for(i = limit; i >= 0; i --)
    {
        if(val&(1LL<<i))
            x = 0;
        else
            x = 1;
        if(tire[p].ch[x]!=-1)
        {
            ans |= (1LL<<i);
            p = tire[p].ch[x];
        }
        else
        {
            p = tire[p].ch[x^1];
        }
    }
    num = tire[p].num;
    return ans;
}
LL tire_find_lower(LL val,LL x_xor,int root,int &num)
{
    int i, x, xx, p = root;
    int last = -1;
    LL ans = 0;

    for(i = limit; i >= 0; i --)
    {
        if( (val^x_xor)&(1LL<<i) )
            x = 1;
        else
            x = 0;
        if( val&(1LL<<i))
            xx = 0;
        else
            xx = 1;
        if(tire[p].ch[x^1]!=-1 && x == xx)
            last = i;
        p = tire[p].ch[x];
    }
    if(last==-1)return -1;

    p = root;
    for(i = limit; i >= 0; i--)
    {
        if(i >= last)
        {
            if( (val^x_xor)&(1LL<<i) )
                x = 1;
            else
                x = 0;
            if(i == last)
                x = (x^1);
        }
        else
        {
            if(val&(1LL<<i))
                x = 0;
            else
                x = 1;
            if(tire[p].ch[x]==-1)
                x = (x^1);
        }
        if( ( val^((LL)x<<i) )&(1LL<<i) )
            ans |= (1LL<<i);
        p = tire[p].ch[x];
    }
    num = tire[p].num;
    return ans;
}

void solve(int n,int m)
{
    int i, j, tire_root, tire_cnt, num;
    LL t;
    priority_queue<node>my_q;
    tire_root = tire_cnt = 0;
    tire[tire_root].init();
    for(i = 1; i <= n; i ++)
    {
        tire_insert(a[i], tire_root, tire_cnt);
    }
    for(i = 1; i <= n; i ++)
    {
        t = tire_find_max(a[i], tire_root, num);
        my_q.push( node(a[i], t, num) );
    }

    j = 1;
    memset(anss, -1, sizeof(anss));
    for(i = 1; i <= (LL)n*(n-1) && j <= m ; i ++)
    {
        node ix = my_q.top();
        my_q.pop();

        while(j <= m && i==q[j].k)
        {
            anss[q[j].id] = ix.x_xor;
            j ++;
        }
        if(ix.num > 1)
        {
            my_q.push( node(ix.x, ix.x_xor, ix.num-1) );
        }
        else
        {
            t = tire_find_lower(ix.x, ix.x_xor, tire_root, num);
            if(t!=-1)
                my_q.push( node(ix.x, t, num) );
        }
    }

    for(i = 1; i <= m; i ++)
        printf("%I64d\n", anss[i]);
}

int main()
{
    int n, m, u, v, i;
    LL t;
    while(scanf("%d", &n) != EOF)
    {
        if(n==0)break;

        memset(p, -1, sizeof(p));
        T = 0;
        for(i = 1; i < n; i ++)
        {
            scanf("%d%d", &u, &v);
            scanf("%I64d", &t);
            add(u, v, t);
            add(v, u, t);
        }
        a[1] = 0;
        dfs(1,-1);
        scanf("%d", &m);
        for(i = 1; i <= m; i ++)
        {
            scanf("%d", &q[i].k);
            q[i].id = i;
        }
        sort(q+1,q+1+m);
        solve(n, m);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于HDU4546问题,还可以使用优先队列(Priority Queue)来解决。以下是使用优先队列的解法思路: 1. 首先,将数组a进行排序,以便后续处理。 2. 创建一个优先队列(最小堆),用于存储组合之和的候选值。 3. 初始化优先队列,将初始情况(即前0个数的组合之和)加入队列。 4. 开始从1到n遍历数组a的元素,对于每个元素a[i],将当前队列中的所有候选值取出,分别加上a[i],然后再将加和的结果作为新的候选值加入队列。 5. 重复步骤4直到遍历完所有元素。 6. 当队列的大小超过k时,将队列中的最小值弹出。 7. 最后,队列中的所有候选值之和即为前k小的组合之和。 以下是使用优先队列解决HDU4546问题的代码示例: ```cpp #include <iostream> #include <vector> #include <queue> #include <functional> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); // 对数组a进行排序 priority_queue<long long, vector<long long>, greater<long long>> pq; // 最小堆 pq.push(0); // 初始情况,前0个数的组合之和为0 for (int i = 0; i < n; i++) { long long num = pq.top(); // 取出当前队列中的最小值 pq.pop(); for (int j = i + 1; j <= n; j++) { pq.push(num + a[i]); // 将所有加和结果作为新的候选值加入队列 num += a[i]; } if (pq.size() > k) { pq.pop(); // 当队列大小超过k时,弹出最小值 } } long long sum = 0; while (!pq.empty()) { sum += pq.top(); // 求队列中所有候选值之和 pq.pop(); } cout << sum << endl; return 0; } ``` 使用优先队列的方法可以有效地找到前k小的组合之和,时间复杂度为O(nklog(k))。希望这个解法对你有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值