• DFS——CodeForces740DAlyona and a tree


    一、题目回顾

    题目链接:Alyona and a tree

    Examples
    Input
    5
    2 5 1 4 6
    1 7
    1 1
    3 5
    3 6
    Output
    1 0 1 0 0
     
    Input
    5
    9 7 8 6 5
    1 1
    2 1
    3 1
    4 1
    Output
    4 3 2 1 0
     
    Note

    In the example test case the vertex 1 controls the vertex 3, the vertex 3 controls the vertex 5 (note that is doesn't mean the vertex 1 controls the vertex 5).

    题意:对于两个点,如果dis(u,v)<=val(v)且v是u的子孙,则称u掌控v,现在求每个结点掌控点的个数。

    二、解题思路

    •  dfs+二分

    dis[x]表示根节点到x节点上的边权和,则x这个节点是被u节点控制的,当且仅当 dis[x]-dis[u]<=a[x],即dis[x]-a[x] <= dis[u]; 而dis这个数组如果在dfs序上,肯定是单调递增的。所以维护一下dfs序、维护一下dis数组。 在处理x的出度的时候找出dis数组中第一个满足dis[x]-a[x]<=dis[u] 的u节点, 则u--->x的路径上的所有点都能够控制x节点,而u节点以上的节点都不能控制x节点。设u节点的dfs序的上一个节点为y,则让ans[y]-,然后在dfs的时候累加答案即可。设当前节点为x,出度节点为y,则ans[x]+=ans[y],当遇到那些ans被减过的节点(即执行过ans[x]–的节点x), 则在算的时候就会把那个不属于它的节点给扣掉。而以上的节点相应的也会受“ans[x]–”的也不会算那些不属于它们的节点了。一开始让ans[x]都等于1,最后再减去1即可。

    三、代码

    #include <bits/stdc++.h>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    #define rei(x) scanf("%d",&x)
    #define rel(x) scanf("%I64d",&x)
    #define all(x) x.begin(),x.end()
    
    typedef pair<int,int> pii;
    typedef pair<LL,LL> pll;
    
    const int MAXN = 2e5+10;
    const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
    const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
    const double pi = acos(-1.0);
    
    int n;
    LL a[MAXN];
    LL ans[MAXN],dis[MAXN];
    vector <LL> w[MAXN];
    vector <int> G[MAXN];
    vector < pair<LL,int> > temp;
    
    void dfs(int x,int fa)
    {
        ans[x] = 1;
        LL t = dis[x]-a[x];
        int pos = lower_bound(all(temp),mp(t,0))-temp.begin();
        pos--;
        if (pos >= 0)
            ans[temp[pos].se]--;
        temp.pb(mp(dis[x],x));
        int len = G[x].size();
        rep1(i,0,len-1)
        {
            int y = G[x][i];
            if (y==fa) continue;
            dis[y] = dis[x] + w[x][i];
            dfs(y,x);
            ans[x] += ans[y];
        }
        temp.pop_back();
    }
    
    int main()
    {
        //freopen("F:\rush.txt","r",stdin);
        rei(n);
        rep1(i,1,n)
            rel(a[i]);
        rep1(i,2,n)
        {
            int fa;LL cost;
            rei(fa);rel(cost);
            G[fa].pb(i);
            w[fa].pb(cost);
            G[i].pb(fa);
            w[i].pb(cost);
        }
        dfs(1,-1);
        rep1(i,1,n)
            {
                printf("%I64d",ans[i]-1);
                if (i==n)
                    puts("");
                else
                    putchar(' ');
            }
        return 0;
    }
    
  • 相关阅读:
    poj 1684 Lazy Math Instructor(字符串)
    STL内存配置器
    迭代器(iterators)
    类型萃取(type traits)
    hdu 2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活(多重背包+dp)
    hdoj 1114 Piggy-Bank(完全背包+dp)
    hdoj 2546 饭卡(0-1背包)
    hdoj 2620 Bone Collector(0-1背包)
    U3d开发个人总结
    Android软键盘的用法总结
  • 原文地址:https://www.cnblogs.com/xzxl/p/7310285.html
Copyright © 2020-2023  润新知