2019年ICPC南昌网络赛 J. Distance on the tree(树链剖分+树状数组)

题目链接:https://nanti.jisuanke.com/t/38229

DSM(Data Structure Master) once learned about tree when he was preparing for NOIP(National Olympiad in Informatics in Provinces) in Senior High School. So when in Data Structure Class in College, he is always absent-minded about what the teacher says.

The experienced and knowledgeable teacher had known about him even before the first class. However, she didn't wish an informatics genius would destroy himself with idleness. After she knew that he was so interested in ACM(ACM International Collegiate Programming Contest), she finally made a plan to teach him to work hard in class, for knowledge is infinite.

This day, the teacher teaches about trees." A tree with nn nodes, can be defined as a graph with only one connected component and no cycle. So it has exactly n-1n−1 edges..." DSM is nearly asleep until he is questioned by teacher. " I have known you are called Data Structure Master in Graph Theory, so here is a problem. "" A tree with nn nodes, which is numbered from 11to nn. Edge between each two adjacent vertexes uu and vv has a value w, you're asked to answer the number of edge whose value is no more than kk during the path between uu and vv."" If you can't solve the problem during the break, we will call you DaShaMao(Foolish Idiot) later on."

The problem seems quite easy for DSM. However, it can hardly be solved in a break. It's such a disgrace if DSM can't solve the problem. So during the break, he telephones you just for help. Can you save him for his dignity?

Input

In the first line there are two integers n,mn,m, represent the number of vertexes on the tree and queries(2 \le n \le 10^5,1 \le m \le 10^52≤n≤105,1≤m≤105)

The next n-1n−1 lines, each line contains three integers u,v,wu,v,w, indicates there is an undirected edge between nodes uu and vv with value ww. (1 \le u,v \le n,1 \le w \le 10^91≤u,v≤n,1≤w≤109)

The next mm lines, each line contains three integers u,v,ku,v,k , be consistent with the problem given by the teacher above. (1 \le u,v \le n,0 \le k \le 10^9)(1≤u,v≤n,0≤k≤109)

Output

For each query, just print a single line contains the number of edges which meet the condition.

样例输入1复制

3 3
1 3 2
2 3 7
1 3 0
1 2 4
1 2 7

样例输出1复制

0
1
2

样例输入2复制

5 2
1 2 1000000000
1 3 1000000000
2 4 1000000000
3 5 1000000000
2 3 1000000000
4 5 1000000000

样例输出2复制

2
4

题意:

给你一棵n(n<=1e5)个节点的带权(权值<=1e9)树,然后q(q<=2e5)次询问,每次询问两点(u,v)的路径中权值<=k的边的条数。

思路:

貌似这题有十分相似的原题。。。

如果是给一个数组,查询区间里[l,r]中a[i]<=k的数的个数,我们可以用主席树或者离线树状数组/线段树来做。

我是用的树状数组:把询问和边权一起从小到大排序,每次遇到询问就求query(r)-query(l-1),遇到边权就add(i,1)。

既然是在树上,那就再加个树剖就行了。把边权化为点权,只需要把这个点和它父亲的点相连的边的权值当做这个点的权值就可以了。根节点置为inf。

其实并不算难,也难怪过了辣么多人。。。

代码:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define lson(x) ((x<<1))
#define rson(x) ((x<<1)+1)
#define mp(x,y)  make_pair(x,y)
#define rep(i,a,b) for(register int i=(a);i<=(b);i++)
#define dep(i,a,b) for(register int i=(a);i>=(b);i--)
using namespace std;
const int N = 100005;
const int maxn = 100005;
int n,m;
int dep[N],siz[N],fa[N],id[N],son[N],val[N],top[N]; //top 近期的重链父节点
int num;
vector<pair<int,int> > v[N];
int a[N];
struct nod
{
    int id,v,fg;
    int x,y;
    bool operator<(nod aa)const
    {
        return v<aa.v||(v==aa.v&&fg<aa.fg);
    }
}q[maxn<<1];
int as[maxn];
struct tree
{
    int x,y,val;
    void read(){
        scanf("%d%d%d",&x,&y,&val);
    }
};
tree e[N];
void dfs1(int u, int f, int d) {
    dep[u] = d;
    siz[u] = 1;
    son[u] = 0;
    fa[u] = f;
    for (int i = 0; i < v[u].size(); i++) {
        int ff = v[u][i].first;
        if (ff == f) continue;
        q[ff].v=v[u][i].second;
        q[ff].id=ff;
        q[ff].x=ff;
        q[ff].y=u;
        dfs1(ff, u, d + 1);
        siz[u] += siz[ff];
        if (siz[son[u]] < siz[ff])
            son[u] = ff;
    }
}
void dfs2(int u, int tp) {
    top[u] = tp;
    id[u] = ++num;
    if (son[u]) dfs2(son[u], tp);
    for (int i = 0; i < v[u].size(); i++) {
        int ff = v[u][i].first;
        if (ff == fa[u] || ff == son[u]) continue;
        dfs2(ff, ff);
    }
}

int lb(int x){return x&(-x);}
void add(int x,int v)
{
    while(x<N)
    {
        a[x]+=v;
        x+=lb(x);
    }
}
int query(int x)
{
    int sum=0;
    while(x)
    {
        sum+=a[x];
        x-=lb(x);
    }
    return sum;
}


int Yougth(int u, int v) {
    //cout<<"OK";
    int tp1 = top[u], tp2 = top[v];
    int ans = 0;
    while (tp1 != tp2) {
        //printf("YES\n");
        if (dep[tp1] < dep[tp2]) {
            swap(tp1, tp2);
            swap(u, v);
        }
        ans+=query(id[u])-query(id[tp1]-1);
        u = fa[tp1];
        tp1 = top[u];
    }
    if (u == v) return ans;
    if (dep[u] > dep[v]) swap(u, v);
    ans += query(id[v])-query(id[son[u]]-1);
    //cout<<"OOOOK"<<endl;;
    return ans;
}
void Clear(int n)
{
    for(int i=0;i<=n;i++)
    {
        a[i]=0;
        v[i].clear();
    }
}
int main()
{
    int T,cas=1;
    scanf("%d%d",&n,&m);
    {
        for(int i=1;i<n;i++)
        {
            e[i].read();
            q[i].fg=0;
            v[e[i].x].push_back(mp(e[i].y,e[i].val));
            v[e[i].y].push_back(mp(e[i].x,e[i].val));
        }
        num = 0;
        q[1].v=inf;
        q[1].id=1;
        q[1].x=1;
        q[1].y=1;
        dfs1(1,0,1);
        //rep(i,1,n)
        //cout<<q[i].fg<<" "<<q[i].x<<endl;
        dfs2(1,1);
        for (int i = 1; i < n; i++) {
            if (dep[e[i].x] < dep[e[i].y]) swap(e[i].x, e[i].y);
            val[id[e[i].x]] = e[i].val;
        }
        rep(i,1,m)
        {
            scanf("%d%d%d",&q[i+n].x,&q[i+n].y,&q[i+n].v);
            q[i+n].id=i+n;
            q[i+n].fg=1;
        }
        //cout<<"*"<<endl;
        sort(q+1,q+n+m+1);
        rep(i,1,n+m)
        {
            //cout<<i<<"*"<<q[i].fg<<" "<<q[i].id<<" "<<q[i].x;
            if(q[i].fg)
            {
                as[q[i].id-n]=Yougth(q[i].x,q[i].y);
            }
            else
            {
                int u=q[i].x;int v=q[i].y;
                int tp1 = top[u], tp2 = top[v];
                if (dep[tp1] < dep[tp2]) {
                swap(tp1, tp2);
                swap(u, v);
                }
                //cout<<u<<" "<<id[u]<<endl;
                add(id[u],1);
            }
            //cout<<"*"<<endl;;
        }
        rep(i,1,m)
        printf("%d\n",as[i]);
        //Clear(n);
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值