【codeforces 761E】Dasha and Puzzle

【题目链接】:http://codeforces.com/contest/761/problem/E

【题意】

给你一棵树,让你在平面上选定n个坐标;
使得这棵树的连接关系以二维坐标的形式展现出来;

【题解】

dfs来搞;
显然如果某个点的度数大于4就无解。
初始坐标为(0,0)然后每一层的边的长度变为上一层长度的1/2
初始层的长度为2 30  
这样可以保证每层节点都不会和上一层的相交;
因为2 i >2 1 +2 2 +...+2 i1  
又因为最多只有30个节点,所以这么做肯定是可以的,且不会超过题目的限制10 18  
写dfs的时候,控制一下行走的方向就好;
不要走回头路。。
然后第一个点是4个方向都可以走的。不要写错了。

【Number Of WA

2

【完整代码】

#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 ps push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,0,-1,0,-1,-1,1,1};
const int dy[9] = {0,0,-1,0,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 30+5;

struct point{
    LL x,y;
};
int n,du[N];
vector <int> G[N];
point ans[N];

void dfs(int v,int fa,LL x0,LL y0,LL len,int fx)
{
    ans[v].x = x0,ans[v].y = y0;
    int l = G[v].size();
    int now = 0;
    rep1(i,0,l-1)
    {
        if (G[v][i]==fa) continue;
        now++;
        int tnow = now+2;
        if (tnow>4) tnow-=4;
        if (tnow==fx) now++;
        LL x1 = x0+1LL*dx[now]*len;LL y1 = y0+1LL*dy[now]*len;
        dfs(G[v][i],v,x1,y1,len>>1,now);
    }
}

int main()
{
    //freopen("D:\\rush.txt","r",stdin);
    rei(n);
    rep1(i,1,n-1)
    {
        int x,y;
        rei(x),rei(y);
        du[x]++,du[y]++;
        G[x].ps(y),G[y].ps(x);
    }
    rep1(i,1,n)
        if (du[i]>4) return puts("NO"),0;
    puts("YES");
    dfs(1,0,0,0,1<<30,0);
    rep1(i,1,n)
        cout << ans[i].x<<' '<<ans[i].y<<endl;
    //printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
    return 0;
}
posted @ 2017-10-04 18:44  AWCXV  阅读(134)  评论(0编辑  收藏  举报