zoukankan      html  css  js  c++  java
  • codeforces gym 101611C 重链剖分构造

    给一棵树 要求在一个20*1e6的矩阵上放下这棵树,每个点的坐标都是整数且所有边都不相叉

    题解

    按照重链遍历,先给轻儿子坐标,然后沿着重儿子向下走即可

    #include <bits/stdc++.h>
    #define endl '
    '
    #define ll long long
    #define pii pair<int,int>
    #define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
    #define rep(ii,a,b) for(int ii=a;ii<=b;++ii)
    #define per(ii,a,b) for(int ii=b;ii>=a;--ii)
    #define forn(ii,x) for(int ii=head[x];ii;ii=e[ii].next)
    using namespace std;
    const int maxn=1e6+10,maxm=2e6+10;
    vector<int>g[maxn];
    int n,ansx[maxn],ansy[maxn],sz[maxn],son[maxn],num[maxn];
    void dfs1(int now,int pre){
        sz[now]=1;
        for(auto to:g[now]){
            if(to==pre) continue;
            dfs1(to,now);
            sz[now]+=sz[to];
            if(sz[son[now]]<sz[to])son[now]=to;
        }
    }
    void dfs2(int now,int pre,int y){
        ansy[now]=y;ansx[now]=++num[y];
        for(auto to:g[now]) if(to!=pre&&to!=son[now])dfs2(to,now,y+1);
        if(son[now]) dfs2(son[now],now,y);
    }
    int main() {
    	IO;
    	cin>>n;
    	rep(i,2,n) {int a,b;
            cin>>a>>b;
            g[a].emplace_back(b);g[b].emplace_back(a);
    	}
        dfs1(1,1);dfs2(1,1,1);
        rep(i,1,n) cout<<ansx[i]<<' '<<ansy[i]<<endl;
    }
    
  • 相关阅读:
    最小生成树(卡鲁斯卡尔)
    最小生成树(kruskal模版 Prim模板)
    hdu1247(Hat’s Words)
    hdu1671Phone List(字典树)
    hdu1305Immediate Decodability(字典树)
    hdu1251(统计难题)
    oj1500(Message Flood)字典树
    oj2892(字典树)
    数论公式
    2504(多项式求和)
  • 原文地址:https://www.cnblogs.com/nervendnig/p/10753544.html
Copyright © 2011-2022 走看看