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;
    }
    
  • 相关阅读:
    Python下用Tkinter进行GUI编程
    6月3日——回首一个半月
    Consistent Hashing算法
    学生终究要面对社会
    MySQL的锁(1)
    Memcached笔记之分布式算法(idv2.com)
    4月21日总结
    2012.3.29小结
    C#调用c++创建的dll
    以post方式发送文档到端口
  • 原文地址:https://www.cnblogs.com/nervendnig/p/10753544.html
Copyright © 2011-2022 走看看