2019南昌邀請賽網絡賽J題

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 11 to 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-1條邊的樹,求每次詢問x到y的路徑上小於k的邊的數量。

做法:樹鏈剖分,離線保存詢問,按照詢問的k由小到大排序,維護每條小於k的邊的數量。

#include <bits/stdc++.h>
using namespace std;
const int N=300000+200;
typedef long long ll;
int n,m,ans;
#define mod 1000000007
const int MAXN=100010;
struct Edge
{
    int to,nexts;
}edge[MAXN*2];
int head[MAXN],tot;
int top[MAXN];
int fa[MAXN];
int deep[MAXN];
int num[MAXN];
int p[MAXN];
int fp[MAXN];
int son[MAXN];
int pos;
void init()
{
    tot=0;
    memset(head,-1,sizeof head);
    pos=0;
    memset(son,-1,sizeof son);
}
void addedge(int u,int v)
{
    edge[tot].to=v;
    edge[tot].nexts=head[u];
    head[u]=tot++;
}
void dfs1(int u,int pre,int d)
{
    deep[u]=d;
    fa[u]=pre;
    num[u]=1;
    for(int i=head[u];i!=-1;i=edge[i].nexts)
    {
        int v=edge[i].to;
        if(v!=pre)
        {
            dfs1(v,u,d+1);
            num[u]+=num[v];
            if(son[u]==-1||num[v]>num[son[u]])
                son[u]=v;
        }
    }
}
void dfs2(int u,int sp)
{
    top[u]=sp;
    p[u]=pos++;
    fp[p[u]]=u;
    if(son[u]==-1) return ;
    dfs2(son[u],sp);
    for(int i=head[u];i!=-1;i=edge[i].nexts)
    {
        int v=edge[i].to;
        if(v!=son[u]&&v!=fa[u])
            dfs2(v,v);
    }
}
struct node
{
    int l,r;
    int va;
}tree[MAXN*3];
void pushup(int rt)
{
    tree[rt].va=tree[rt<<1].va+tree[rt<<1|1].va;
}
void build(int l,int r,int rt)
{
    tree[rt].l=l;
    tree[rt].r=r;
    tree[rt].va=0;
    if(l==r) return ;
    int mid=(l+r)>>1;
    build(l,mid,rt<<1);
    build(mid+1,r,rt<<1|1);
}
void update(int rt,int p,int v)
{
    if(tree[rt].l==tree[rt].r)
    {
        tree[rt].va+=v;
        return;
    }
    int mid=(tree[rt].l+tree[rt].r)>>1;
    if(mid>=p)
    {
        update(rt<<1,p,v);
    }
    else update(rt<<1|1,p,v);
    pushup(rt);
}
int query(int rt,int L,int R)
{
    if(tree[rt].l>=L&&tree[rt].r<=R)
    {
        return tree[rt].va;
    }
    int mid=(tree[rt].l+tree[rt].r)>>1;
    int ans=0;
    if(mid>=L) ans+=query(rt<<1,L,R);
    if(mid<R) ans+=query(rt<<1|1,L,R);
    return ans;
}
int queryV(int u,int v)
{
    int f1=top[u],f2=top[v];
    int tmp=0;
    while(f1!=f2)
    {
        if(deep[f1]<deep[f2])
        {
            swap(f1,f2);
            swap(u,v);
        }
        tmp=tmp+query(1,p[f1],p[u]);
        u=fa[f1];f1=top[u];
    }
    if(u==v) return tmp;
    if(deep[u]>deep[v]) swap(u,v);
    tmp+=query(1,p[son[u]],p[v]);//邊權的話這裏是p[son[u]],點權的話是p[u]
    return tmp;
}
struct node1
{
    int u,v;
    ll w;
}e[MAXN];
struct QUE
{
    int u,v,w,ans;
    int id;
}que[MAXN];
bool cmp(node1 x,node1 y)
{
    return x.w<y.w;
}
bool cmp1(QUE x,QUE y)
{
    return x.id<y.id;
}
bool cmp2(QUE x,QUE y)
{
    return x.w<y.w;
}
int main()
{
    init();
    int n,m;
    scanf("%d %d",&n,&m);
    for(int i=1;i<n;i++)
    {
        scanf("%d %d %lld",&e[i].u,&e[i].v,&e[i].w);
        addedge(e[i].u,e[i].v);
        addedge(e[i].v,e[i].u);
    }
    dfs1(1,0,0);
    dfs2(1,1);
    build(0,pos-1,1);
    for(int i=1;i<=m;i++)
    {
        scanf("%d %d %d",&que[i].u,&que[i].v,&que[i].w);
        que[i].id=i;
    }
    sort(e+1,e+n,cmp);
    sort(que+1,que+m+1,cmp2);
    int cnte=1;
    for(int i=1;i<=m;i++)
    {
        if(i==1||que[i].w!=que[i-1].w)
        {
            for(; cnte<n; cnte++)
            {
                if(e[cnte].w<=que[i].w)
                {
                    if(deep[e[cnte].u]>deep[e[cnte].v])//這個if不能少呀
                        swap(e[cnte].u,e[cnte].v);
                    update(1,p[e[cnte].v],1);
                }
                else break;
            }
        }
        que[i].ans=queryV(que[i].u,que[i].v);
    }
    sort(que+1,que+m+1,cmp1);
    for(int i=1;i<=m;i++)
    {
        printf("%d\n",que[i].ans);
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章