hiho1192 Simple tree embedding (structural problem)

Source: Internet
Author: User
Tags printf first row time limit

Topic links

#1192: Simple tree embedding time limitation: 10000ms single point time limit: 1000ms memory limit: 256MB description

Given an n-point tree of no permission. Defines the distance between two points I, J (0≤i, j≤n-1) and D (I, j) as the length (number of sides) of the shortest path on the tree between two points.

We need to map these n points to the vector in V0 = (v0, 0, ..., V0, m-1), ..., vn-1 = (vn-1, 0, ..., vn-1, m-1). Define two vector VI, the L1 distance between VJ is


We hope that for all 0≤i, J≤n-1 has D (i, j) = D1 (vi, VJ). input

First row N. Below N-1 line, where I act two spaces separated by an integer XI, Yi, representing the Point Xi and Yi have an edge.

N≤100. 0≤xi, Yi≤n-1. Output

First line M. The following n lines, where I act m spaces separate integers vi-1, 0, ..., vi-1, m-1.

Requires m≤100 and-100≤vi, j≤100. Also, for all 0≤i, J≤n-1,d (i, j) = D1 (vi, VJ).

If there is more than one solution, output any set of solutions that meet the requirements. Sample input

3
0 1)
1 2
Sample output
2
0 0
1 0
1 1

Exercises

Assuming there is only one point, the vector that constructs the point is {0}. If the second point and this point have an edge, the vector of the point can be constructed as {0,1}, and the first point's vector ends with a 0-bit {0,0}. And so on If the point I and the J Point (I>j) is connected to an edge, that is, the vector of the first J Point is paid to the point I, the vector of point I is added one dimension is 1, the other point vector is also added one dimension to 0. Due to the addition of one dimension, this dimension has only the first I point of 1, so the effect of increasing this dimension on other points is the same, and the other points will not be affected by the addition of this dimension. One dimension is added to each point, so the vector is up to n dimensions. To the diagram of BFS, according to the above method of construction can be.

The code is as follows:

#include <iostream> #include <stdio.h> #include <algorithm> #include <math.h> #include < queue> #include <string> #include <string.h> #include <stack> #include <vector> #include <
set> #include <map> typedef long Long LL;
typedef unsigned long long LLU;
Double pi = ACOs (-1);
const int NN = 110000;
const int inf = 0X3FFFFFFF;
const int INFF = 0X3FFFFFFF;
Const LL mod = 1000000003;
using namespace Std;
int n; struct node {int en,next;}
E[210];
int p[110],num;
int ans[110][110];
int len[110];
    void Init () {memset (p,-1,sizeof (p));
num=0;
    } void Add (int st,int en) {e[num].en=en;
    E[NUM].NEXT=P[ST];

    p[st]=num++;
    e[num].en=st;
    E[num].next=p[en];
p[en]=num++;
} bool Use[nn];
queue<int>que;
    int main () {int i,j;
        while (scanf ("%d", &n)!=eof) {init ();
        int u,v;
            for (i=1;i<n;i++) {scanf ("%d%d", &u,&v);
        Add (U,V);
 }       memset (ans,0,sizeof (ans));
        memset (len,0,sizeof (len));
        memset (use,false,sizeof (use));
        Use[0]=true;
        Len[0]=1;
        Que.push (0);
        int cnt=1;
            while (Que.size ()) {int Sta=que.front ();
            Que.pop ();
                for (i=p[sta];i+1;i=e[i].next) {int w=e[i].en;
                    if (!use[w]) {use[w]=true;
                    for (j=0;j<len[sta];j++) {ans[w][j]=ans[sta][j];
                    } ans[w][cnt++]=1;
                    len[w]=cnt;
                Que.push (w);
        }}} printf ("%d\n", CNT); for (i=0;i<n;i++) {for (j=0;j<cnt;j++) {printf ("%d%c", ans[i][j],j==c Nt-1? '
            \ n ': ');
}}} return 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.