malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int maxn = 500 + 5;   char mp[maxn][maxn];   int main() {     int n, m;     cin >> n >> m;     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) scanf(  %c , &mp[i][j]);     int x1, y1, x2, y2;     for (int i = 0; i < n; i++) {       bool bb = false;       for (int j = 0; j < m; j++)         if (mp[i][j] ==  X ) {           x1 = i;           y1 = j;           bb = true;           break;         }       if (bb) break;     }     for (int i = n - 1; i >= 0; i--) {       bool bb = false;       for (int j = m - 1; j >= 0; j--) {         if (mp[i][j] ==  X ) {           x2 = i;           y2 = j;           bb = true;           break;         }       }       if (bb) break;     }     if (x1 > x2 || y1 > y2) {       puts( NO );       return 0;     }     for (int i = x1; i <= x2; i++)       for (int j = y1; j <= y2; j++) {         if (mp[i][j] ==  . ) {           puts( NO );           return 0;         }         mp[i][j] =  . ;       }     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++)         if (mp[i][j] ==  X ) {           puts( NO );           return 0;         }     puts( YES );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, d = 0, e = 0;     cin >> n;     int a[n];     for (int i = 0; i < n; i++) {       cin >> a[i];       if (i > 0) {         e += a[i - 1] - a[i];         if (e < 0) {           d -= e;           e = 0;         }       } else {         d += a[0];       }     }     cout << d;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int test_time, x = 0;     cin >> test_time;     for (int i = 0; i < test_time; ++i) {       string temp;       cin >> temp;       for (int j = 0; j < temp.length(); ++j) {         if (temp[j] ==  - ) {           x--;           break;         } else if (temp[j] ==  + ) {           x++;           break;         }       }     }     cout << x;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long int dp[101000];   int main(int argc, char const *argv[]) {     int t, a, b, k;     scanf( %d%d , &t, &k);     dp[0] = 1;     for (int i = 1; i <= 100000; i++) {       dp[i] = (dp[i] + dp[i - 1]) % 1000000007;       if (i >= k) dp[i] = (dp[i] + dp[i - k]) % 1000000007;     }     dp[0] = 0;     for (int i = 2; i <= 100000; i++) {       dp[i] = (dp[i] + dp[i - 1]) % 1000000007;     }     while (t--) {       scanf( %d%d , &a, &b);       long long int ans = (dp[b] - dp[a - 1] + 1000000007) % 1000000007;       cout << ans <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   struct SegTree {     int L, R, P, DELTA;     struct node {       int d;       bool zero;     } v[212345 * 4];     inline void set_zero(int x) {       v[x].zero = 1;       v[x].d = 0;     }     inline void fix(int x, int d) {       v[x].zero = 0;       v[x].d += d;     }     inline void rep(int x) {       if (v[x].zero) {         set_zero(x * 2);         set_zero(x * 2 + 1);         v[x].zero = 0;       } else {         fix(x * 2, v[x].d);         fix(x * 2 + 1, v[x].d);         v[x].d = 0;       }     }     void update(int x, int l, int r) {       if (l > R || r < L) return;       if (L <= l && r <= R) {         if (l < r)           rep(x);         else           v[x].zero = 0;         v[x].d += DELTA;         return;       }       int m = (l + r) / 2;       rep(x);       update(x * 2, l, m);       update(x * 2 + 1, m + 1, r);     }     void gao_zero(int x, int l, int r) {       if (l > R || r < L) return;       if (L <= l && r <= R) {         set_zero(x);         return;       }       int m = (l + r) / 2;       rep(x);       gao_zero(x * 2, l, m);       gao_zero(x * 2 + 1, m + 1, r);     }     int add(int x, int y) {       if (~y) return x + y;       return -1;     }     int query(int x, int l, int r) {       if (v[x].zero) return -1;       if (l == r) return v[x].d;       int m = (l + r) / 2;       if (P <= m)         return add(v[x].d, query(x * 2, l, m));       else         return add(v[x].d, query(x * 2 + 1, m + 1, r));     }     void up(int l, int r, int d) {       L = l, R = r, DELTA = d;       update(1, 1, n);     }     void zero(int l, int r) {       L = l, R = r;       gao_zero(1, 1, n);     }     int qr(int p) {       P = p;       return query(1, 1, n);     }   } tree;   int x[212345], len[212345];   vector<pair<int, int> > qu[212345];   int ans[212345], nxt[212345];   int L[212345], D[212345], id[212345], top = 0;   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) scanf( %d%d , x + i, len + i);     int q;     scanf( %d , &q);     for (int i = 0; i < int(q); i++) {       int u, v;       scanf( %d%d , &u, &v);       qu[u].push_back(make_pair(v, i));     }     nxt[n] = -1;     for (int i = n - 1; i >= 1; --i) {       int j = i + 1;       while (~j && x[j] + len[j] <= x[i] + len[i]) j = nxt[j];       nxt[i] = j;     }     for (int i = n; i >= 1; --i) {       if (nxt[i] == -1) {       } else {         while (top > 0 && id[top] < nxt[i]) {           tree.up(L[top], n, -D[top]);           --top;         }         if (x[i] + len[i] >= x[nxt[i]]) {         } else {           tree.up(nxt[i], n, x[nxt[i]] - x[i] - len[i]);           ++top;           L[top] = nxt[i];           D[top] = x[nxt[i]] - x[i] - len[i];           id[top] = i;         }       }       for (auto x : qu[i]) ans[x.second] = tree.qr(x.first);     }     for (int i = 0; i < int(q); i++) printf( %d n , ~ans[i] ? ans[i] : 0);   }
#include <bits/stdc++.h>   using namespace std;   struct car {     int c, v;   };   bool operator<(car a, car b) { return a.v < b.v; }   int main() {     ios::sync_with_stdio(false);     int n, k, s, t;     cin >> n >> k >> s >> t;     vector<car> arr(n);     for (int i = 0; i < n; i++) cin >> arr[i].c >> arr[i].v;     vector<int> g(k + 2);     g[0] = 0;     for (int i(1); i <= k; i++) cin >> g[i];     k += 2;     g[k - 1] = s;     sort(arr.begin(), arr.end());     sort(g.begin(), g.end());     int l = -1, r = n;     int cnt = 0;     while (r - l > 1) {       cnt++;       int m = (r + l) / 2;       int time = 0;       for (int i(1); i < k; i++) {         int d = g[i] - g[i - 1];         int a = min(arr[m].v - d, d);         int norm = d - a;         if (arr[m].v < d) {           time = 2000000007;           break;         }         time += a + 2 * norm;       }       if (time > t)         l = m;       else         r = m;     }     if (r == n)       cout << -1 << endl;     else {       int ans = 1000000007;       for (int i = r; i < n; ++i) ans = min(ans, arr[i].c);       cout << ans << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   char t[200003], p[200003];   int a, pos[200003], nt, np;   bool check(int mid) {     int it = 0;     for (int ip = 0; ip < np; ip++) {       while (it < nt && (t[it] != p[ip] || pos[it + 1] <= mid)) it++;       if (it == nt) return false;       it++;     }     return true;   }   int main() {     scanf( %s , t);     scanf( %s , p);     nt = strlen(t);     np = strlen(p);     for (int i = 1; i <= nt; i++) {       scanf( %d , &a);       pos[a] = i;     }     int lo = 1, mid, hi = nt;     while (hi >= lo) {       mid = (lo + hi) / 2;       if (check(mid))         lo = mid + 1;       else         hi = mid - 1;     }     printf( %d n , hi);   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1000 + 10;   char ch[N];   int a[N][N];   int h[N], l[N];   int n, m;   int main() {     cin >> n >> m;     int tot = 0;     for (int i = 1; i <= n; i++) {       scanf( %s , ch + 1);       for (int j = 1; j <= m; j++) {         a[i][j] = ch[j] ==  *  ? 1 : 0;         h[i] += a[i][j];         l[j] += a[i][j];         tot += a[i][j];       }     }     int x = -1, y = -1;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         if (h[i] + l[j] - a[i][j] == tot) {           x = i;           y = j;           break;         }     if (x == -1) {       puts( NO );     } else {       puts( YES );       cout << x <<     << y << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <typename A, typename B>   string to_string(pair<A, B> p);   template <typename A, typename B, typename C>   string to_string(tuple<A, B, C> p);   string to_string(const string& s) { return     + s +    ; }   string to_string(const char* s) { return to_string((string)s); }   string to_string(bool b) { return (b ?  true  :  false ); }   string to_string(vector<bool> v) {     bool f = true;     string res =  { ;     for (long long i = (0); i <= (static_cast<long long>(v.size()) - 1); i++) {       if (!f) res +=  ,  ;       res += to_string(v[i]);       f = false;     }     res +=  } ;     return res;   }   template <size_t N>   string to_string(bitset<N> v) {     string res =   ;     for (auto& a : v) {       res += (char)( 0  + a);     }     return res;   }   template <typename A>   string to_string(A v) {     bool f = true;     string res =  { ;     for (auto& x : v) {       if (!f) res +=  ,  ;       res += to_string(x);       f = false;     }     res +=  } ;     return res;   }   template <typename A, typename B>   string to_string(pair<A, B> p) {     return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ;   }   template <typename A, typename B, typename C>   string to_string(tuple<A, B, C> t) {     return  (  + to_string(get<0>(t)) +  ,   + to_string(get<1>(t)) +  ,   +            to_string(get<2>(t)) +  ) ;   }   void DBG() { cerr << endl; }   template <typename H, typename... T>   void DBG(H h, T... t) {     cerr <<     << to_string(h);     DBG(t...);   }   template <typename T>   void re(T& x) {     cin >> x;   };   template <typename H, typename... T>   void re(H& h, T&... t) {     re(h);     re(t...);   }   template <typename T>   void pr(T x) {     cout << x;   };   template <typename H, typename... T>   void pr(H h, T... t) {     pr(h);     pr(t...);   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     long long tt;     re(tt);     while (tt--) {       string s;       re(s);       long long n = (long long)s.size();       long long a, b, c;       a = b = c = -1;       long long inf = (long long)9e18;       long long ans = inf;       for (long long i = (0); i <= (n - 1); i++) {         if (s[i] ==  1 ) {           a = i;           if (b == -1 || c == -1) continue;           ans = min(ans, i - min(b, c) + 1);         }         if (s[i] ==  2 ) {           b = i;           if (a == -1 || c == -1) continue;           ans = min(ans, i - min(a, c) + 1);         }         if (s[i] ==  3 ) {           c = i;           if (b == -1 || a == -1) continue;           ans = min(ans, i - min(b, a) + 1);         }       }       pr((ans == inf ? 0 : ans),   n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[200015], n, k, b[100015];   int cmp(int x, int y) { return x < y; }   int main() {     while (scanf( %d%d , &n, &k) != EOF) {       memset(a, 0, sizeof(a));       int ans = 0, flag = 1, t = 0;       int m = k;       for (int i = 0; i < n; i++) {         scanf( %d , &a[i]);         if (a[i] < 0) t++;         if (flag == 1 && a[i] < 0) {           flag = 0;           ans++;         } else if (flag == 0 && a[i] >= 0) {           flag = 1;           ans++;         }       }       if (t == 0) {         printf( 0 n );       } else if (t > k) {         printf( -1 n );       } else if (t == k) {         printf( %d n , ans);       } else {         k = k - t;         int sum = 0, c = -1;         for (int i = n - 1; i >= 0; i--) {           if (a[i] < 0) {             if (c == -1) {               t = sum;               c++;             } else if (sum) {               b[c] = sum;               c++;             }             sum = 0;           } else             sum++;         }         sort(b, b + c, cmp);         int ans1 = ans, ans2 = ans, kk = k;         if (kk > 0) {           for (int i = 0; i < c; i++) {             if (kk - b[i] >= 0) {               kk -= b[i];               ans1 -= 2;             } else               break;           }         }         kk = k - t;         if (kk >= 0 && t) {           ans2 = ans - 1;           for (int i = 0; i < c; i++) {             if (kk - b[i] >= 0) {               kk -= b[i];               ans2 -= 2;             } else               break;           }         }         printf( %d n , min(ans1, ans2));       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     if (n % 2 == 1) {       cout << (n - 1) / 2 << endl;     } else {       long long x = 1;       while (2 * x <= n) {         x *= 2;       }       cout << (n - x) / 2 << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int mark[(10000000 >> 5) + 1];   void sieve() {     register int i, j, k;     (mark[1 >> 5] |= 1 << (1 & 31));     int n = 10000000;     for (i = 2; i <= n; i++) {       if (!(mark[i >> 5] >> (i & 31) & 1)) {         for (k = n / i, j = i * k; k >= i; k--, j -= i)           (mark[j >> 5] |= 1 << (j & 31));       }     }   }   long long C(long long n, long long k) {     if (k == 0) return 1;     return (n * C(n - 1, k - 1)) / k;   }   long long modular_pow(long long budgetase, long long exponent, int modulus) {     long long result = 1;     while (exponent > 0) {       if (exponent % 2 == 1) result = (result * budgetase) % modulus;       exponent = exponent >> 1;       budgetase = (budgetase * budgetase) % modulus;     }     return result;   }   long long binaryToDec(string number) {     long long result = 0, pow = 1;     for (int i = number.length() - 1; i >= 0; --i, pow <<= 1)       result = (result + (number[i] -  0 ) * pow) % 1000003;     return result;   }   long long GCD(long long a, long long b) { return b == 0 ? a : GCD(b, a % b); }   int main() {     double d, h, v, e;     cin >> d >> h >> v >> e;     double r = d / 2;     double area = 3.1415926535897932384626433832795 * r * r;     if (e * area > v || fabs(e * area - v) < 0.0000000001) {       cout <<  NO  << endl;       return 0;     }     double vol = 3.1415926535897932384626433832795 * r * r * h;     double p = e * 3.1415926535897932384626433832795 * r * r;     double dif = fabs(p - v);     cout <<  YES  << endl;     cout << setprecision(12) << fixed << (double)vol / dif << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<int> a(n, 0);     vector<int> p(n, 0);     for (int i = 0; i < n; i++) {       cin >> a[i];       a[i]--;     }     int x;     for (int i = 0; i < n; i++) {       cin >> x;       p[x - 1] = i;     }     int ii = 1;     while (ii < n && p[a[ii - 1]] < p[a[ii]]) {       ii++;     }     cout << n - ii << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int s, t, n;   string str;   bool flag = true, flag1 = true;   int main() {     scanf( %d , &n);     cin >> str;     for (int i = 0; i < n; i++) {       if (str[i] ==  R  && flag) {         s = i;         flag = false;       }       if (str[i] ==  L  && flag1) {         t = i;         flag1 = false;       }     }     if (flag1) {       for (int i = 0; i < n; i++)         if (str[i] ==  R ) t = i;       printf( %d %d , s + 1, t + 2);       return 0;     }     if (flag) {       for (int i = 0; i < n; i++)         if (str[i] ==  L ) s = i;       printf( %d %d , s + 1, t);       return 0;     }     printf( %d %d , s + 1, t);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long INF = -10000000;   int main() {     long long da, db;     cin >> da >> db;     if (da == db) {       cout << da * 10 <<     << db * 10 + 1;     } else if (da == 9 && db == 1) {       cout << da * 10 + 9 <<     << db * 100;     } else if (da == 0 && db == 1) {       cout << da <<     << db;     } else if (da + 1 == db) {       cout << da * 10 + 9 <<     << db * 10;     } else {       cout << -1;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int c1, c2, c3, c4;     cin >> c1 >> c2 >> c3 >> c4;     int n, m;     cin >> n >> m;     int a[n], b[m];     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < m; i++) cin >> b[i];     int sb = 0, st = 0;     for (int i = 0; i < n; i++) sb += min(a[i] * c1, c2);     sb = min(sb, c3);     for (int i = 0; i < m; i++) st += min(b[i] * c1, c2);     st = min(st, c3);     cout << min(st + sb, c4);   }
#include <bits/stdc++.h>   using namespace std;   char ans[1000000];   int main() {     int n, c, k = 1, u = 0, w = 1, h = 0;     cin >> n;     int a[n];     for (int i = 0; i < n; i++) cin >> a[i];     int m = 0;     while (w) {       u = 0;       for (int i = 0; i < n; i++) {         if (a[i] == 0) u++;       }       if (u == n) break;       c = 0;       for (int i = h; i < n; i++) {         if (a[i] != 0) {           ans[m] =  P ;           a[i]--;           m++;           u = 0;           for (int i = 0; i < n; i++) {             if (a[i] == 0) u++;           }           if (u == n) {             break;             w = 0;           }           if ((i + 1) != n) {             ans[m] =  R ;             m++;           }           u = 0;           for (int i = 0; i < n; i++) {             if (a[i] == 0) u++;           }           if (u == n) {             break;             w = 0;           }         } else if (a[i] == 0) {           u = 0;           for (int i = 0; i < n; i++) {             if (a[i] == 0) u++;           }           if (u == n) {             break;             w = 0;           }           if ((i + 1) != n) {             ans[m] =  R ;             m++;           }         }       }       u = 0;       for (int i = 0; i < n; i++) {         if (a[i] == 0) u++;       }       if (u == n) break;       ans[m] =  L ;       m++;       for (int i = (n - 2); i >= 0; i--) {         if (a[i] != 0) {           ans[m] =  P ;           a[i]--;           m++;           u = 0;           for (int i = 0; i < n; i++) {             if (a[i] == 0) u++;           }           if (u == n) {             break;             w = 0;           }           if ((i - 1) != -1) {             ans[m] =  L ;             m++;           }           u = 0;           for (int i = 0; i < n; i++) {             if (a[i] == 0) u++;           }           if (u == n) {             break;             w = 0;           }         } else if (a[i] == 0) {           u = 0;           for (int i = 0; i < n; i++) {             if (a[i] == 0) u++;           }           if (u == n) {             break;             w = 0;           }           if ((i - 1) != -1) {             ans[m] =  L ;             m++;           }         }         if (i == 0) {           ans[m] =  R ;           m++;         }       }       u = 0;       for (int i = 0; i < n; i++) {         if (a[i] == 0) u++;       }       if (u == n) break;       h = 1;     }     for (int i = 0; i < m; i++) cout << ans[i];     cout <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX = 105;   vector<int> adj[MAX];   string s[MAX];   int mark[MAX];   bool cycle;   string ans =   ;   void dfs(int v) {     mark[v] = 1;     for (int i = 0; i < adj[v].size(); i++) {       int u = adj[v][i];       if (mark[u] == 1) cycle = true;       if (!mark[u]) dfs(u);     }     mark[v] = 2;   }   void print(int v) {     mark[v] = 1;     for (int i = 0; i < adj[v].size(); i++) {       int u = adj[v][i];       if (!mark[u]) print(u);     }     ans +=  a  + v;   }   int main() {     ios::sync_with_stdio(false);     int n;     cin >> n;     for (int i = 0; i < n; i++) cin >> s[i];     for (int i = 0; i < n - 1; i++) {       int j = 0;       while (j < s[i].size() && j < s[i + 1].size() && s[i][j] == s[i + 1][j])         j++;       if (j == s[i + 1].size() && s[i].size() > s[i + 1].size()) {         cout <<  Impossible  << endl;         return 0;       }       if (j < s[i].size() && j < s[i + 1].size())         adj[s[i][j] -  a ].push_back(s[i + 1][j] -  a );     }     for (int i = 0; i < 26; i++)       if (!mark[i]) dfs(i);     if (cycle) {       cout <<  Impossible  << endl;       return 0;     }     memset(mark, 0, sizeof(mark));     for (int i = 0; i < 26; i++)       if (!mark[i]) print(i);     reverse(ans.begin(), ans.end());     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios ::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     if (fopen( main.in ,  r ))       freopen( main.in ,  r , stdin);     else if (fopen( something                      .inp ,                     r ))       freopen(            something             .inp ,            r , stdin),           freopen(                something                 .out ,                w , stdout);     int n, m;     cin >> n >> m;     int cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0;     for (int i = 1; i <= n; i++) {       int a;       cin >> a;       cnt1 += (a % 2 == 1);       cnt2 += (a % 2 == 0);     }     for (int i = 1; i <= m; i++) {       int a;       cin >> a;       cnt3 += (a % 2 == 1);       cnt4 += (a % 2 == 0);     }     cout << min(cnt1, cnt4) + min(cnt2, cnt3) <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int d[1234678], a[1234678], ans;   int main() {     int n, cnt = 0;     char c;     scanf( %d n , &n);     for (int i = 1; i <= n; i++) {       scanf( %c , &c);       d[i] = (c ==  R );     }     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     ans = 1047483647;     for (int i = 2; i <= n; i++)       if (d[i - 1] == 1 && d[i] == 0) ans = min(ans, (a[i] - a[i - 1]) / 2);     if (ans == 1047483647)       puts( -1 );     else       printf( %d n , ans);   }
#include <bits/stdc++.h>   using namespace std;   int main() {     char a;     int b;     scanf( %c%d , &a, &b);     if ((a ==  a  || a ==  h ) && (b == 1 || b == 8))       printf( 3 n );     else if (a ==  a  || a ==  h  || b == 1 || b == 8)       printf( 5 n );     else       printf( 8 n );   }
#include <bits/stdc++.h>   using namespace std;   const long long NMAX = 1000001;   vector<int> v[NMAX];   int tobehappy[NMAX];   void DFS(int node) {     if (v[node].size() == 0) tobehappy[node] = 1;     for (auto x : v[node]) {       DFS(x);       tobehappy[node] += tobehappy[x];     }   }   int main() {     int n, i;     cin >> n;     for (i = 2; i <= n; i++) {       int x, y;       cin >> y;       v[y].push_back(i);     }     DFS(1);     sort(tobehappy + 1, tobehappy + n + 1);     for (i = 1; i <= n; i++) cout << tobehappy[i] <<    ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e6 + 7;   int n, u, v, cnt, head[maxn], to[maxn << 1], nex[maxn << 1], deep[maxn],       len[maxn], son[maxn], *f[maxn], tmp[maxn], *id = tmp, ans[maxn];   inline int read() {     int x = 0, f = 1;     char ch = getchar();     while (!isdigit(ch)) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (isdigit(ch)) {       x = x * 10 + ch - 48;       ch = getchar();     }     return x * f;   }   inline void add(int u, int v) {     to[++cnt] = v;     nex[cnt] = head[u];     head[u] = cnt;   }   void dfs1(int x, int pre, int deep) {     int maxson = 0;     ::deep[x] = deep;     for (int i = head[x]; i; i = nex[i]) {       if (to[i] == pre) continue;       dfs1(to[i], x, deep + 1);       if (len[to[i]] > maxson) son[x] = to[i], maxson = len[to[i]];     }     len[x] = maxson + 1;   }   void dfs2(int x, int pre) {     f[x][0] = 1;     if (son[x]) {       f[son[x]] = f[x] + 1;       dfs2(son[x], x);       ans[x] = ans[son[x]] + 1;     }     for (int i = head[x]; i; i = nex[i]) {       int v = to[i];       if (v == pre || v == son[x]) continue;       f[v] = id, id += len[v], dfs2(v, x);       for (int j = 1; j <= len[v]; ++j) {         f[x][j] += f[v][j - 1];         if (f[x][j] > f[x][ans[x]] || (f[x][j] == f[x][ans[x]] && j < ans[x]))           ans[x] = j;       }     }     if (f[x][ans[x]] == 1) ans[x] = 0;   }   int main() {     n = read();     for (int i = 1; i < n; ++i) {       u = read(), v = read();       add(u, v);       add(v, u);     }     dfs1(1, 0, 1);     f[1] = id, id += len[1], dfs2(1, 0);     for (int i = 1; i <= n; ++i) printf( %d n , ans[i]);   }