Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define LOCAL
- #define MULTITEST
- #define int long long
- #ifdef LOCAL
- #define debug(x) cerr << #x << " = " << x << endl;
- #define ddebug(x, y) cerr << #x << " = " << x << " , " << #y << " = " << y << endl;
- #define arraydebug(a) for (auto e : a) cerr << e << ' '; cerr << endl;
- #define pairdebug(p) cerr << #p << " = " << p.first << ',' << p.second << endl;
- #else
- #define debug(x);
- #define ddebug(x, y);
- #define arraydebug(a);
- #define pairdebug(p);
- #endif
- #define ll long long
- #define ld long double
- #define ull unsigned ll
- #define pii pair<int, int>
- #define fi first
- #define se second
- #define vi vector<int>
- #define vvi vector<vi>
- #define pb push_back
- #define vpii vector<pii>
- #define ALL(x) x.begin(), x.end()
- const int INF32 = 2e9 + 5;
- const ll INF64 = 4e18 + 5;
- const ld eps = 1e-10;
- const int mod = 1e9 + 7;
- const int SZ = 1 << 10;
- vi e[SZ];
- int h[SZ];
- set<pii> s[SZ];
- int dp[SZ];
- bool cmp(int a, int b){
- return h[a] < h[b];
- }
- int dh(int x){
- return (x * x % mod * x + x * x + x + 17) % mod;
- }
- void dfs1(int cur, int par = 0){
- for (int next : e[cur]){
- if (next != par){
- dfs1(next, cur);
- }
- }
- sort(e[cur].begin(), e[cur].end(), cmp);
- h[cur] = 0;
- for (int next : e[cur]){
- if (next == par){
- continue;
- }
- h[cur] += dh(h[next]);
- s[cur].insert({h[next], next});
- }
- h[cur] %= mod;
- }
- pii pos[SZ];
- int cnt_pos = 0;
- void draw_pos(int cur, int y){
- pos[cur] = {++cnt_pos, y};
- for (pii elem : s[cur]){
- draw_pos(elem.se, y + 1);
- }
- }
- int cnt_neg = 0;
- void draw_neg(int cur, int y){
- pos[cur] = {--cnt_neg, y};
- for (pii elem : s[cur]){
- draw_neg(elem.se, y + 1);
- }
- }
- bool f(int cur, int par = 0, int y = 0){
- if (s[cur].size() == 0){
- pos[cur] = {0, y};
- return true;
- }
- pii last = {-1, -1};
- int z = 0;
- vpii a;
- for (pii elem : s[cur]){
- if (last.fi == -1){
- last = elem;
- continue;
- }
- if (last.fi == elem.fi){
- a.pb({last.se, elem.se});
- last = {-1, -1};
- continue;
- }
- if (!z){
- z = last.se;
- }else{
- z = -1;
- break;
- }
- last = elem;
- }
- if (last.fi != -1){
- if (!z){
- z = last.se;
- }else{
- z = -1;
- }
- }
- if (z == -1){
- return false;
- }
- bool ans = z == 0;
- if (z){
- ans = f(z, cur, y + 1);
- }
- pos[cur] = {0, y};
- if (ans){
- for (pii elem : a){
- draw_pos(elem.fi, y + 1);
- draw_neg(elem.se, y + 1);
- }
- }
- return ans;
- }
- int n;
- bool dfs2(int cur, int par = 0){
- if (f(cur)){
- return true;
- }
- for (int next : e[cur]){
- if (next != par){
- h[cur] = (h[cur] - dh(h[next]) + mod) % mod;
- s[cur].erase({h[next], next});
- if (dp[next] * 2 == n && h[cur] == h[next]){
- draw_pos(cur, 0);
- draw_neg(next, 0);
- return true;
- }
- h[next] = (h[next] + dh(h[cur])) % mod;
- s[next].insert({h[cur], cur});
- if (dfs2(next, cur)){
- return true;
- }
- h[next] = (h[next] - dh(h[cur]) + mod) % mod;
- s[next].erase({h[cur], cur});
- h[cur] = (h[cur] + dh(h[next])) % mod;
- s[cur].insert({h[next], next});
- }
- }
- return false;
- }
- int dfs0(int cur = 1, int par = 0){
- dp[cur] = 1;
- for (int next : e[cur]){
- if (next == par){
- continue;
- }
- dp[cur] += dfs0(next, cur);
- }
- return dp[cur];
- }
- void solve(){
- cnt_pos = 0;
- cnt_neg = 0;
- for (int i = 0; i < SZ; i++){
- h[i] = INF32;
- e[i].clear();
- s[i].clear();
- }
- cin >> n;
- for (int i = 1; i < n; i++){
- int u, v;
- cin >> u >> v;
- e[u].pb(v);
- e[v].pb(u);
- }
- dfs0();
- dfs1(1);
- if (!dfs2(1)){
- cout << "NO";
- return;
- }
- cout << "YES\n";
- for (int i = 1; i <= n; i++){
- cout << pos[i].fi << ' ' << pos[i].se << '\n';
- }
- cout << "1 0 0";
- }
- signed main(){
- srand(time(NULL));
- cout << setprecision(10);
- cout << fixed;
- #ifdef LOCAL
- freopen("input.txt", "r", stdin);
- // freopen("input.txt", "w", stdout);
- #else
- ios_base::sync_with_stdio(NULL);
- cin.tie(NULL);
- cout.tie(NULL);
- #endif
- int t = 1;
- #ifdef MULTITEST
- cin >> t;
- #endif
- while (t--){
- solve();
- cout << '\n';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement