Codeforces 761E Dasha and Puzzle(构造)

题目链接 Dasha and Puzzle

对于无解的情况:若存在一个点入度大于4,那么直接判断无解。

从根结点出发(假设根结点的深度为0),

深度为0的节点到深度为1的节点的这些边长度为2^30,

深度为1的节点到深度为2的节点的这些边的长度为2^29,

………………………………………………………………

以此类推。

因为结点个数最多只有30个,所以长度分配足够。

#include <bits/stdc++.h>

using namespace std;

#define REP(i,n)                for(int i(0); i <  (n); ++i)
#define rep(i,a,b)              for(int i(a); i <= (b); ++i)
#define LL              long long
#define PB              push_back

const int Q     = 1000        +       10;
const int A    = 30          +       1;
const LL dx[]     = {0, 1, 0, -1};
const LL dy[]     = {1, 0, -1, 0};

struct node{ LL x, y;} c[A];
vector <int> v[A];
int f[A][A];
LL num[Q];
int n;
int x, y;
int inn[Q];
bool vis[Q];

void dfs(int x, int cnt){
    REP(i, (int)v[x].size()){
        int u = v[x][i];
        if (!vis[u]){
            vis[u] = true;
            REP(j, 4){
                if (!f[x][j] && !f[u][(j + 2) % 4]){
                    c[u].x = c[x].x + dx[j] * num[cnt];
                    c[u].y = c[x].y + dy[j] * num[cnt];
                    f[u][(j + 2) % 4] = 1;
                    f[x][j] = 1;
                    break;
                }
            }
            dfs(u, cnt - 1);
        }
    }
}    

int main(){

    num[0] = 1; rep(i, 1, 36) num[i] = num[i - 1] * 2;
    scanf("%d", &n);
    rep(i, 1, n - 1){
        scanf("%d%d", &x, &y);
        v[x].PB(y);
        v[y].PB(x);
        ++inn[x], ++inn[y];
    }

    rep(i, 1, n) if (inn[i] > 4){
        puts("NO");
        return 0;
    }

    memset(vis, false, sizeof vis); vis[1] = true;
    c[1].x = c[1].y = 0;
    dfs(1, 30);

    puts("YES");
    rep(i, 1, n){
        printf("%lld %lld\n", c[i].x, c[i].y);
    }

    return 0;

}

 

转载于:https://www.cnblogs.com/cxhscst2/p/6367933.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值