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;
    }
    
  • 相关阅读:
    py程序----两个判断回文的程序
    Python特性
    python-基本数据类型
    shell编程第一天
    iptables防火墙
    纤维参数测量
    线性代数及其应用(最小二乘、PCA、SVD)
    水流方向检测
    微信跳一跳-MATLAB实现
    相机标定opencv实现
  • 原文地址:https://www.cnblogs.com/nervendnig/p/10753544.html
Copyright © 2011-2022 走看看