Advertisement
kolychestiy

Untitled

Sep 1st, 2022
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.93 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define LOCAL
  6. #define MULTITEST
  7.  
  8. #define int long long
  9.  
  10. #ifdef LOCAL
  11.     #define debug(x) cerr << #x << " = " << x << endl;
  12.     #define ddebug(x, y) cerr << #x << " = " << x << " , " << #y << " = " << y << endl;
  13.     #define arraydebug(a) for (auto e : a) cerr << e << ' '; cerr << endl;
  14.     #define pairdebug(p) cerr << #p << " = " << p.first << ',' << p.second << endl;
  15. #else
  16.     #define debug(x);
  17.     #define ddebug(x, y);
  18.     #define arraydebug(a);
  19.     #define pairdebug(p);
  20. #endif
  21.  
  22. #define ll long long
  23. #define ld long double
  24. #define ull unsigned ll
  25.  
  26. #define pii pair<int, int>
  27. #define fi first
  28. #define se second
  29.  
  30. #define vi vector<int>
  31. #define vvi vector<vi>
  32. #define pb push_back
  33. #define vpii vector<pii>
  34.  
  35. #define ALL(x) x.begin(), x.end()
  36.  
  37. const int INF32 = 2e9 + 5;
  38. const ll INF64 = 4e18 + 5;
  39.  
  40. const ld eps = 1e-10;
  41.  
  42. const int mod = 1e9 + 7;
  43. const int SZ = 1 << 10;
  44.  
  45. vi e[SZ];
  46.  
  47. int h[SZ];
  48. set<pii> s[SZ];
  49.  
  50. int dp[SZ];
  51.  
  52. bool cmp(int a, int b){
  53.     return h[a] < h[b];
  54. }
  55.  
  56. int dh(int x){
  57.     return (x * x % mod * x + x * x + x + 17) % mod;
  58. }
  59.  
  60. void dfs1(int cur, int par = 0){
  61.     for (int next : e[cur]){
  62.         if (next != par){
  63.             dfs1(next, cur);
  64.         }
  65.     }
  66.  
  67.     sort(e[cur].begin(), e[cur].end(), cmp);
  68.  
  69.     h[cur] = 0;
  70.    
  71.     for (int next : e[cur]){
  72.         if (next == par){
  73.             continue;
  74.         }
  75.  
  76.         h[cur] += dh(h[next]);
  77.         s[cur].insert({h[next], next});
  78.     }
  79.  
  80.     h[cur] %= mod;
  81. }
  82.  
  83.  
  84. pii pos[SZ];
  85.  
  86. int cnt_pos = 0;
  87. void draw_pos(int cur, int y){
  88.     pos[cur] = {++cnt_pos, y};
  89.  
  90.     for (pii elem : s[cur]){
  91.         draw_pos(elem.se, y + 1);
  92.     }
  93. }
  94.  
  95. int cnt_neg = 0;
  96. void draw_neg(int cur, int y){
  97.     pos[cur] = {--cnt_neg, y};
  98.  
  99.     for (pii elem : s[cur]){
  100.         draw_neg(elem.se, y + 1);
  101.     }
  102. }
  103.  
  104. bool f(int cur, int par = 0, int y = 0){
  105.     if (s[cur].size() == 0){
  106.         pos[cur] = {0, y};
  107.         return true;
  108.     }
  109.  
  110.     pii last = {-1, -1};
  111.     int z = 0;
  112.     vpii a;
  113.     for (pii elem : s[cur]){
  114.  
  115.         if (last.fi == -1){
  116.             last = elem;
  117.             continue;
  118.         }
  119.  
  120.         if (last.fi == elem.fi){
  121.             a.pb({last.se, elem.se});
  122.             last = {-1, -1};
  123.             continue;
  124.         }
  125.  
  126.         if (!z){
  127.             z = last.se;
  128.         }else{
  129.             z = -1;
  130.             break;
  131.         }
  132.  
  133.         last = elem;
  134.  
  135.     }
  136.  
  137.  
  138.     if (last.fi != -1){
  139.         if (!z){
  140.             z = last.se;
  141.         }else{
  142.             z = -1;
  143.         }
  144.     }
  145.  
  146.     if (z == -1){
  147.         return false;
  148.     }
  149.  
  150.     bool ans = z == 0;
  151.     if (z){
  152.         ans = f(z, cur, y + 1);
  153.     }
  154.  
  155.     pos[cur] = {0, y};
  156.  
  157.     if (ans){
  158.         for (pii elem : a){
  159.             draw_pos(elem.fi, y + 1);
  160.             draw_neg(elem.se, y + 1);
  161.         }
  162.     }
  163.     return ans;
  164.  
  165. }
  166.  
  167. int n;
  168.  
  169. bool dfs2(int cur, int par = 0){
  170.     if (f(cur)){
  171.         return true;
  172.     }
  173.  
  174.     for (int next : e[cur]){
  175.         if (next != par){
  176.             h[cur] = (h[cur] - dh(h[next]) + mod) % mod;
  177.             s[cur].erase({h[next], next});
  178.  
  179.             if (dp[next] * 2 == n && h[cur] == h[next]){
  180.                 draw_pos(cur, 0);
  181.                 draw_neg(next, 0);
  182.                 return true;
  183.             }
  184.  
  185.             h[next] = (h[next] + dh(h[cur])) % mod;
  186.             s[next].insert({h[cur], cur});
  187.  
  188.             if (dfs2(next, cur)){
  189.                 return true;
  190.             }
  191.  
  192.             h[next] = (h[next] - dh(h[cur]) + mod) % mod;
  193.             s[next].erase({h[cur], cur});
  194.             h[cur] = (h[cur] + dh(h[next])) % mod;
  195.             s[cur].insert({h[next], next});
  196.         }
  197.     }
  198.  
  199.     return false;
  200. }
  201.  
  202. int dfs0(int cur = 1, int par = 0){
  203.     dp[cur] = 1;
  204.     for (int next : e[cur]){
  205.         if (next == par){
  206.             continue;
  207.         }
  208.  
  209.         dp[cur] += dfs0(next, cur);
  210.     }
  211.     return dp[cur];
  212. }
  213.  
  214. void solve(){
  215.  
  216.     cnt_pos = 0;
  217.     cnt_neg = 0;
  218.     for (int i = 0; i < SZ; i++){
  219.         h[i] = INF32;
  220.         e[i].clear();
  221.         s[i].clear();
  222.     }
  223.  
  224.     cin >> n;
  225.  
  226.     for (int i = 1; i < n; i++){
  227.         int u, v;
  228.         cin >> u >> v;
  229.         e[u].pb(v);
  230.         e[v].pb(u);
  231.     }
  232.  
  233.     dfs0();
  234.  
  235.     dfs1(1);
  236.  
  237.     if (!dfs2(1)){
  238.         cout << "NO";
  239.         return;
  240.     }
  241.  
  242.     cout << "YES\n";
  243.     for (int i = 1; i <= n; i++){
  244.         cout << pos[i].fi << ' ' << pos[i].se << '\n';
  245.     }
  246.     cout << "1 0 0";
  247.  
  248. }
  249.  
  250. signed main(){
  251.  
  252.    srand(time(NULL));
  253.     cout << setprecision(10);
  254.     cout << fixed;
  255.  
  256.     #ifdef LOCAL
  257.         freopen("input.txt", "r", stdin);
  258.         // freopen("input.txt", "w", stdout);
  259.     #else
  260.         ios_base::sync_with_stdio(NULL);
  261.         cin.tie(NULL);
  262.         cout.tie(NULL);
  263.     #endif
  264.  
  265.     int t = 1;
  266.     #ifdef MULTITEST
  267.         cin >> t;
  268.     #endif
  269.    
  270.     while (t--){
  271.         solve();
  272.         cout << '\n';
  273.     }
  274.  
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement