malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   long long a[70];   long long b[70];   long long ans[4909];   int main() {     long long x, y, l, r, u = 1, v = 1, w = 0;     cin >> x >> y >> l >> r;     a[0] = 1;     b[0] = 1;     for (int i = 1; i <= 65; i++) {       if (a[u - 1] <= 1000000000000000010 / x) {         a[u] = a[u - 1] * x;         u++;       }     }     for (int i = 1; i <= 65; i++) {       if (b[v - 1] <= 1000000000000000010 / y) {         b[v] = b[v - 1] * y;         v++;       }     }     for (int i = 0; i < u; i++) {       for (int j = 0; j < v; j++) {         if (1ll * a[i] + 1ll * b[j] >= l && 1ll * a[i] + 1ll * b[j] <= r) {           ans[w++] = 1ll * a[i] + 1ll * b[j];         }       }     }     ans[w++] = l - 1;     ans[w++] = r + 1;     sort(ans, ans + w);     long long fans = 0;     for (int i = 1; i < w; i++) {       if (ans[i] - ans[i - 1] - 1 > fans) {         fans = ans[i] - ans[i - 1] - 1;       }     }     cout << fans;   }
#include <bits/stdc++.h>   using namespace std;   struct p {     int s, n;   };   int comp(struct p a, struct p b) { return (a.s <= b.s); }   int main() {     vector<long long> a, sb, s, r;     long long i, j, k, n, ss, sss, rrr;     cin >> n;     s.resize(n + 2, 0);     r.resize(n + 2, 0);     sb.push_back(0);     for (i = 1; i <= n; i++) {       cin >> k;       a.push_back(k);       sb.push_back(sb.back() + k);     }     ss = sb.back();     if (ss % 3 != 0 || n <= 2) {       cout <<  0  << endl;       return 0;     }     sss = 0;     rrr = 0;     for (i = 0; i < n; i++) {       if (sss == ss / 3 && i != 0) s[i]++;       if (rrr == ss / 3 && i != 0) r[i]++;       sss += a[i];       rrr += a[n - i - 1];       s[i + 1] = s[i];       r[i + 1] = r[i];     }     long long c = s[0], var = 0;     for (i = 1; i < n; i++) {       if (s[i] != c) {         c = s[i];         var += r[n - i - 1];       }     }     cout << var << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 2000005;   int n, m;   int in[MAX_N];   int inv[MAX_N];   int out[MAX_N], temp[MAX_N];   int pos2bckt[MAX_N];   int bckt[MAX_N], relabel[MAX_N];   int bpos[MAX_N];   void build() {     for (int i = 0; i < n; i++) out[i] = i;     sort(out, out + n, [](int a, int bckt) { return in[a] < in[bckt]; });     for (int i = 0, c = 0; i < n; i++) {       bckt[i] = c;       if (i + 1 == n || in[out[i]] != in[out[i + 1]]) c++;     }     for (int h = 1; h < n; h <<= 1) {       for (int i = 0; i < n; i++) pos2bckt[out[i]] = bckt[i];       for (int i = n - 1; i >= 0; i--) bpos[bckt[i]] = i;       for (int i = 0; i < n; i++)         if (out[i] >= n - h) temp[bpos[bckt[i]]++] = out[i];       for (int i = 0; i < n; i++)         if (out[i] >= h) temp[bpos[pos2bckt[out[i] - h]]++] = out[i] - h;       for (int i = 0, c = 0; i < n; i++) {         relabel[i] = c;         if (i + 1 == n) break;         if (bckt[i] != bckt[i + 1])           c++;         else if (temp[i + 1] >= n - h || temp[i] >= n - h)           c++;         else if (pos2bckt[temp[i + 1] + h] != pos2bckt[temp[i] + h])           c++;       }       memcpy(out, temp, sizeof(temp[0]) * n);       memcpy(bckt, relabel, sizeof(relabel[0]) * n);     }   }   char str[MAX_N];   int cnt[MAX_N], mmn[MAX_N], rmmn[MAX_N];   int main() {     scanf( %s , str);     while (str[n]) n++;     for (int i = 0; i < n; i++) in[i] = str[i] ==  ) ;     for (int i = 0; i < n; i++) in[i + n] = in[i];     int step = 0, mn = 0;     for (int i = 0; i < n; i++) {       if (in[i] == 1)         step--;       else         step++;       cnt[i + 1] = step;       if (mn > step) mn = step;       mmn[i + 1] = mn;     }     int p = step;     for (int i = n - 1; i >= 0; i--) {       if (in[i] == 1)         step++;       else         step--;       if (p > step) p = step;       rmmn[i] = p;     }     n *= 2;     build();     n /= 2;     int c[2] = {         0,     };     for (int i = 0; i < n; i++) c[in[i]]++;     if (c[0] >= c[1]) {       for (int i = 0; i < 2 * n; i++) {         if (out[i] < n && rmmn[out[i]] - cnt[out[i]] >= 0 &&             cnt[n] - cnt[out[i]] + mmn[out[i]] >= 0) {           for (int j = 0; j < n; j++) str[j] = in[out[i] + j] == 0 ?  (  :  ) ;           for (int j = n, k = 0; k < c[0] - c[1]; j++, k++) str[j] =  ) ;           break;         }       }     } else {       for (int i = 0; i < 2 * n; i++) {         if (out[i] < n && rmmn[out[i]] - cnt[out[i]] >= c[0] - c[1] &&             cnt[n] - cnt[out[i]] + mmn[out[i]] >= c[0] - c[1]) {           for (int j = 0; j < c[1] - c[0]; j++) str[j] =  ( ;           for (int j = c[1] - c[0], k = 0; k < n; j++, k++)             str[j] = in[out[i] + k] == 0 ?  (  :  ) ;           break;         }       }     }     puts(str);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long int powr(long long a, long long b) {     if (b == 0) {       return 1;     }     if (b % 2 == 0) {       return powr(((a % 1000000007) * (a % 1000000007)) % 1000000007, b / 2);     } else {       if (b == 1) {         return a;       } else {         return (a * powr(((a % 1000000007) * (a % 1000000007) % 1000000007),                          (b - 1) / 2)) %                1000000007;       }     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long s, b;     cin >> s >> b;     long long a[s];     long long u[b];     for (long long i = 0; i < s; i++) {       cin >> a[i];     }     vector<pair<long long, long long> > p;     long long c, d;     for (long long i = 0; i < b; i++) {       cin >> c >> d;       p.push_back({c, d});       u[i] = c;     }     sort(p.begin(), p.end());     sort(u, u + b);     long long y = 0;     long long sum[b];     for (long long i = 0; i < b; i++) {       y += p[i].second;       sum[i] = y;     }     for (long long i = 0; i < s; i++) {       pair<long long, long long> cur = {a[i], INT_MAX};       auto idx = lower_bound(p.begin(), p.end(), cur);       if (idx == p.begin()) {         cout << 0 <<    ;         continue;       }       idx--;       long long j = idx - p.begin();       cout << sum[j] <<    ;     }     cerr <<  Time :   << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC          <<  ms n ;     return 0;   }
#include <bits/stdc++.h>   using llong = long long;   using namespace std;   set<llong> factors;   void f(llong num) {     factors.insert(1);     llong to = sqrt(num);     for (auto i = 2; i <= to; ++i) {       if (num % i == 0) {         factors.insert(i);         factors.insert(num / i);       }     }     factors.insert(num);   }   int main() {     ios_base::sync_with_stdio(false);     cout.tie(0);     cin.tie(0);     llong n;     cin >> n;     f(n);     set<llong> res;     for (auto e : factors) {       res.insert((n + 1 + 1) * (n / e + 1) / 2 - (n + 1));     }     for (auto i : res) cout << i <<    ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   typedef struct {     int u, v, cu, cv;   } Edge;   const int N = 500010, M = 1000010;   pair<int, int> stk[M];   int top;   Edge e[N];   int cnt;   int fa[M];   int co[N];   int sz[M];   int st[N];   int n, m, k;   int find(int x) {     if (x == fa[x])       return x;     else       return find(fa[x]);   }   void merge(int u, int v) {     int fu = find(u), fv = find(v);     if (sz[fu] > sz[fv]) swap(fu, fv);     stk[++top] = {fu, fv};     fa[fu] = fv;     sz[fv] += sz[fu];   }   int cmp(Edge a, Edge b) {     if (a.cu == b.cu)       return a.cv < b.cv;     else       return a.cu < b.cu;   }   void undo() {     pair<int, int> t = stk[top--];     fa[t.first] = t.first;     sz[t.second] -= sz[t.first];   }   int main() {     cin >> n >> m >> k;     for (int i = 1; i <= n + n; i++) sz[i] = 1, fa[i] = i;     for (int i = 1; i <= n; i++) cin >> co[i];     for (int i = 1; i <= m; i++) {       int u, v;       cin >> u >> v;       if (co[u] == co[v]) {         int fu = find(u), fv = find(v);         if (fu == fv) {           st[co[u]] = 1;         } else {           int fuu = find(u + n), fvv = find(v + n);           merge(fu, fvv), merge(fv, fuu);         }       } else {         e[++cnt] = {u, v, min(co[u], co[v]), max(co[v], co[u])};       }     }     long long aa = 0;     for (int i = 1; i <= k; i++)       if (!st[i]) aa++;     long long ans = aa * (aa - 1) / 2;     sort(e + 1, e + 1 + cnt, cmp);     int idx = top;     int flag = 0;     for (int i = 1; i <= cnt; i++) {       if (e[i].cv != e[i - 1].cv || e[i].cu != e[i - 1].cu) {         flag = 0;         while (idx != top) undo();       }       if (st[e[i].cv] || st[e[i].cu]) continue;       if (flag) continue;       int fu = find(e[i].u), fv = find(e[i].v);       if (fu == fv) {         ans--;         flag = 1;         continue;       }       int fuu = find(e[i].u + n), fvv = find(e[i].v + n);       merge(fu, fvv), merge(fv, fuu);     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int mod = 1000000007;   int dv[1000005];   int cnt[1000006];   inline void add(int &r, int a) {     r += a;     if (r >= mod) r -= mod;   }   int mPow(int a, int x) {     int ret = 1;     while (x > 0) {       if (x & 1) ret = 1LL * ret * a % mod;       a = 1LL * a * a % mod;       x >>= 1;     }     return ret;   }   int fact[200006], rfact[200006], C[200006];   int _C(int n, int k) {     if (k > n || k < 0) return 0;     return 1LL * fact[n] * rfact[k] % mod * rfact[n - k] % mod;   }   int dp[1000006], n, k, q;   int rem[55] = {0}, base;   int p[1000006];   int ans = 0, old[1000006];   inline void go3(const vector<pair<int, int>> &v, int pos, int num) {     if (pos == v.size()) {       ++cnt[num];       add(ans, mod - 1LL * p[num] * (old[num] - C[cnt[num]] + mod) % mod);       old[num] = C[cnt[num]];       return;     }     vector<int> tmp(v[pos].second + 1);     for (int i = 0; i <= v[pos].second; ++i) {       tmp[i] = num;       num *= v[pos].first;     }     rem[pos] = 0;     for (int i = v[pos].second; i >= 0; --i) {       go3(v, pos + 1, tmp[i]);       ++rem[pos];     }   }   void go1(const vector<pair<int, int>> &v, int pos, int num) {     if (pos == v.size()) {       if (base != num) {         p[base] -= p[num];       }       return;     }     for (int i = 0; i <= v[pos].second; ++i) {       go1(v, pos + 1, num);       num *= v[pos].first;     }   }   int main() {     scanf( %d%d%d , &n, &k, &q);     fact[0] = rfact[0] = 1;     for (int i = 1; i <= n + q; ++i) {       fact[i] = 1LL * fact[i - 1] * i % mod;       rfact[i] = mPow(fact[i], mod - 2);     }     for (int i = 0; i <= n + q; ++i) {       C[i] = _C(i, k);     }     for (int i = 2; i <= 1000000; ++i) {       if (dv[i] == 0) {         for (int j = i; j <= 1000000; j += i) {           if (dv[j] == 0) dv[j] = i;         }       }     }     for (int i = 1; i <= 1000000; ++i) {       p[i] = i;       int aa = i;       vector<pair<int, int>> v;       while (aa > 1) {         int d = dv[aa], cnt = 0;         while (aa % d == 0) {           aa /= d;           ++cnt;         }         v.emplace_back(make_pair(d, cnt));       }       base = i;       go1(v, 0, 1);     }     for (int i = 0; i < n + q; ++i) {       int aa;       scanf( %d , &aa);       vector<pair<int, int>> v;       while (aa > 1) {         int d = dv[aa], cnt = 0;         while (aa % d == 0) {           aa /= d;           ++cnt;         }         v.emplace_back(make_pair(d, cnt));       }       go3(v, 0, 1);       if (i >= n) {         printf( %d n , ans);       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3fffffff;   const long long _INF = 0x7ffffffffffffffll;   const double EPS = 1e-9;   const double PI = acos(-1.0);   template <class T, class INT>   inline T pow(T x, INT y) {     T res(1);     for (T tt(x); y; y /= 2) {       if (y & 1) res *= tt;       tt *= tt;     }     return res;   }   template <class T, class INT>   inline T pow(T x, INT y, T p) {     T res(1);     for (T tt(x); y; y /= 2) {       if (y & 1) res = res * tt % p;       tt = tt * tt % p;     }     return res;   }   int toInt(string s) {     int x = 0;     istringstream sin(s);     sin >> x;     return x;   }   template <class T>   inline string toString(T x) {     ostringstream sout;     sout << x;     return sout.str();   }   template <class INT>   inline INT gcd(INT x, INT y) {     return y ? gcd(y, x % y) : x;   }   template <class INT>   inline int calc_bits(INT x) {     return x ? (x & 1) + calc_bits(x >> 1) : 0;   }   const int MAXN = 1000 + 10;   int n, m, cnt, r[MAXN], c[MAXN], u[MAXN], d[MAXN];   string a[MAXN];   double ans;   inline long long calc(int x, int n) {     return (n - x) * (n - x - 1) / 2 + (x + 1) * x / 2;   }   int main(int argc, char* argv[]) {     cin >> n >> m;     for (int i = 0; i < (n); ++i) cin >> a[i];     cnt = n * m;     memset(r, 255, sizeof(r)), memset(c, 255, sizeof(c));     for (int i = 0; i < (n); ++i)       for (int j = 0; j < (m); ++j)         if (a[i][j] ==  X ) r[i] = j, c[j] = i, --cnt;     for (int i = 0; i < (n); ++i) {       u[i] = r[i];       d[i] = m - r[i] - 1;       if (r[i] != -1) {         if (i && r[i - 1] != -1 && r[i - 1] < r[i]) u[i] += u[i - 1];         if (i && r[i - 1] != -1 && r[i - 1] > r[i]) d[i] += d[i - 1];       }     }     for (int i = (n - 1); i > (-1); --i)       if (r[i] != -1) {         if (i < n - 1 && r[i + 1] != -1 && r[i + 1] < r[i]) u[i] += u[i + 1];         if (i < n - 1 && r[i + 1] != -1 && r[i + 1] > r[i]) d[i] += d[i + 1];       }     for (int i = 0; i < (m); ++i) ans += n * n * calc(i, m);     for (int i = 0; i < (n); ++i)       for (int j = 0; j < (n); ++j) {         ans += (r[i] == -1 ? m : m - 1) * (r[j] == -1 ? m : m - 1) * abs(i - j);         if (i == j && r[i] != -1)           ans += 2 * (u[i] * (m - r[i] - 1) + d[i] * r[i]);         if (r[i] != -1) ans -= calc(r[i], m);         if (r[j] != -1) ans -= calc(r[j], m);         if (r[i] != -1 && r[j] != -1) ans += abs(r[i] - r[j]);       }     for (int i = 0; i < (m); ++i) {       u[i] = c[i];       d[i] = n - c[i] - 1;       if (c[i] != -1) {         if (i && c[i - 1] != -1 && c[i - 1] < c[i]) u[i] += u[i - 1];         if (i && c[i - 1] != -1 && c[i - 1] > c[i]) d[i] += d[i - 1];       }     }     for (int i = (m - 1); i > (-1); --i)       if (c[i] != -1) {         if (i < m - 1 && c[i + 1] != -1 && c[i + 1] < c[i]) u[i] += u[i + 1];         if (i < m - 1 && c[i + 1] != -1 && c[i + 1] > c[i]) d[i] += d[i + 1];       }     for (int i = 0; i < (m); ++i)       if (c[i] != -1) ans += 2 * (u[i] * (n - c[i] - 1) + d[i] * c[i]);     cout << setprecision(8) << fixed << ans / cnt / cnt << endl;   }
#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC target( avx,avx2,fma )   using namespace std;   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long lcm(long long a, long long b) { return a * b / (gcd(a, b)); }   void solve() {     long long n, ans = 0;     string s;     cin >> n >> s;     for (long long i = 0; i < n - 2; i++) {       if (s[i] ==  x  && s[i + 1] ==  x  && s[i + 2] ==  x ) ans++;     }     cout << ans << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long n, e = 0, aw = 0, k = 0;     cin >> n;     long a[n];     for (int i = 0; i < n; i++) {       cin >> a[i];       k = max(a[i], k);       e += a[i];     }     aw = k * n - e;     while (aw <= e) {       k++;       aw = k * n - e;     }     cout << k;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long ans, t, s, a, i, b, k;   int main() {     cin >> k >> a >> b;     s = (0 - a) / k + 1;     while (a <= 0) {       a = a + s * k;       b = s * k + b;     }     ans = b / k - (a - 1) / k;     cout << ans;   }
#include <bits/stdc++.h>   int main() {     int N;     std::cin >> N;     std::set<std::pair<int, int>> doubled;     std::set<std::pair<int, int>> pending;     int to_double = 0;     int l_doubled = 0;     int64_t total_damage = 0;     int t;     int d;     for (int i = 0; i < N; i++) {       std::cin >> t;       std::cin >> d;       total_damage += d;       to_double += t * (d > 0 ? 1 : -1);       if (d > 0) {         pending.insert({d, t});       } else {         auto search = doubled.find({-d, t});         if (search != doubled.end()) {           doubled.erase(search);           l_doubled -= t;           total_damage += d;         } else {           pending.erase(pending.find({-d, t}));         }       }       if (to_double > doubled.size()) {         if (pending.size() > 0) {           auto it_pending = --pending.end();           std::pair<int, int> max = *it_pending;           doubled.insert(max);           pending.erase(it_pending);           l_doubled += max.second;           total_damage += max.first;         }       } else if (to_double < doubled.size()) {         if (doubled.size() > 0) {           auto it_doubled = doubled.begin();           std::pair<int, int> min = *it_doubled;           doubled.erase(it_doubled);           pending.insert(min);           l_doubled -= min.second;           total_damage -= min.first;         }       } else {         if (to_double > 0 && pending.size() > 0) {           auto it_doubled = doubled.begin();           auto it_pending = --pending.end();           std::pair<int, int> max = *it_pending;           std::pair<int, int> min = *it_doubled;           if (max > min) {             doubled.erase(it_doubled);             pending.erase(it_pending);             doubled.insert(max);             pending.insert(min);             l_doubled += max.second - min.second;             total_damage += max.first - min.first;           }         }       }       if (doubled.size() == to_double && l_doubled == to_double &&           to_double > 0) {         auto it_doubled = doubled.begin();         std::pair<int, int> min = *it_doubled;         doubled.erase(it_doubled);         total_damage -= min.first;         l_doubled--;         if (pending.size() > 0) {           auto it_pending = --pending.end();           std::pair<int, int> max = *it_pending;           pending.erase(it_pending);           doubled.insert(max);           total_damage += max.first;         }         pending.insert(min);       }       std::cout << total_damage << std::endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxN = 1000000;   long long a1, a2, t0, t1, t2, p1, p2, num, f, T, Tim, tot1, tot2, l, r, ans,       Num;   bool fl;   void check1(long long my) {     num = my / p1;     if (num * a1 >= f) {       if (t1 * f <= T) fl = 1;       return;     }     for (long long i = num; i >= max(0LL, num - maxN); --i) {       long long left_money = my - i * p1, left_time = T - i * tot1,                 left_data = f - i * a1;       if (left_time < 0) return;       if (left_data <= 0) {         fl = 1;         return;       }       if (t2 >= t0) {         Tim = left_data * t0;         if (Tim <= left_time) fl = 1;         return;       }       Num = left_time % tot2 ? left_time / tot2 + 1 : left_time / tot2;       Num = min(num, left_money / p2);       if (Num * a2 >= left_data)         Tim = left_data * t2;       else         Tim = Num * tot2 + (left_data - Num * a2) * t0;       if (Tim <= left_time) {         fl = 1;         return;       }       --Num;       if (Num * a2 >= left_data)         Tim = left_data * t2;       else         Tim = Num * tot2 + (left_data - Num * a2) * t0;       if (Tim <= left_time) {         fl = 1;         return;       }     }   }   void check2(long long my) {     num = my / p2;     if (num * a2 >= f) {       if (t2 * f <= T) fl = 1;       return;     }     for (long long i = num; i >= max(0LL, num - maxN); --i) {       long long left_money = my - i * p2, left_time = T - i * tot2,                 left_data = f - i * a2;       if (left_time < 0) return;       if (left_data <= 0) {         fl = 1;         return;       }       if (t1 >= t0) {         Tim = left_data * t0;         if (Tim <= left_time) fl = 1;         return;       }       Num = left_time % tot1 ? left_time / tot1 + 1 : left_time / tot1;       Num = min(Num, left_money / p1);       if (Num * a1 >= left_data)         Tim = left_data * t1;       else         Tim = Num * tot1 + (left_data - Num * a1) * t0;       if (Tim <= left_time) {         fl = 1;         return;       }       --Num;       if (Num * a1 >= left_data)         Tim = left_data * t1;       else         Tim = Num * tot1 + (left_data - Num * a1) * t0;       if (Tim <= left_time) {         fl = 1;         return;       }     }   }   bool check(long long my) {     fl = 0;     check1(my);     if (fl) return 1;     check2(my);     if (fl) return 1;     return 0;   }   int main() {     scanf( %lld%lld%lld , &f, &T, &t0);     scanf( %lld%lld%lld , &a1, &t1, &p1);     tot1 = a1 * t1;     scanf( %lld%lld%lld , &a2, &t2, &p2);     tot2 = a2 * t2;     if (t0 * f <= T) {       puts( 0 );       return 0;     }     num = T % tot1 ? T / tot1 + 1 : T / tot1;     r = num * p1;     num = T % tot2 ? T / tot2 + 1 : T / tot2;     r = max(r, num * p2);     l = 1;     r *= 2;     ans = -1;     while (l <= r) {       long long mid = (l + r) >> 1;       if (check(mid))         ans = mid, r = mid - 1;       else         l = mid + 1;     }     printf( %lld n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<int> e[200010];   vector<int> a;   int n;   int siz[200010];   int id[200010];   int d[200010];   int c[200010];   int f[200010];   int dfn;   void dfs(int x, int fa) {     id[x] = ++dfn;     siz[x]++;     for (int i = 0; i < e[x].size(); i++) {       if (e[x][i] == fa) continue;       dfs(e[x][i], x);       siz[x] += siz[e[x][i]];     }   }   void dfs(int x, int fa, int *y) {     int temp = (n - siz[x]);     bool flag = true;     for (int i = 0; i < e[x].size(); i++) {       if (e[x][i] == fa) continue;       dfs(e[x][i], x, y);       if (temp != siz[e[x][i]]) {         flag = false;       }     }     if (flag && siz[x] > 1) {       *y = x;     }   }   bool judge(int x, int fa, int dep) {     if (c[x] != -1 && f[x] != fa && c[x] != fa) return false;     f[x] = -1;     for (int i = 0; i < e[x].size(); i++) {       int nt = e[x][i];       if (nt == fa) continue;       int son_siz = siz[nt];       if (id[x] > id[nt]) son_siz = n - siz[x];       if (d[dep] == -1) {         d[dep] = son_siz;       }       if (d[dep] != son_siz) {         c[x] = nt;         return false;       }       f[x] = nt;     }     for (int i = 0; i < e[x].size(); i++) {       int nt = e[x][i];       if (nt == fa) continue;       if (!judge(nt, x, dep + 1)) {         c[x] = nt;         return false;       }       f[x] = nt;     }     f[x] = -1;     return true;   }   inline bool scan_d(int &num) {     char in;     bool IsN = false;     in = getchar();     if (in == EOF) return false;     while (in !=  -  && (in <  0  || in >  9 )) in = getchar();     if (in ==  - ) {       IsN = true;       num = 0;     } else       num = in -  0 ;     while (in = getchar(), in >=  0  && in <=  9 ) {       num *= 10, num += in -  0 ;     }     if (IsN) num = -num;     return true;   }   int main() {     int x = -1, u, v, k = -1;     scanf( %d , &n);     for (int i = 1; i < n; i++) {       scan_d(u);       scan_d(v);       if (k == -1) k = u;       e[u].push_back(v);       e[v].push_back(u);     }     dfs(1, 1);     dfs(1, 1, &x);     for (int i = 1; i <= n; i++) {       if (e[i].size() <= 1) {         a.push_back(i);       }     }     if (x != -1) {       a.push_back(x);     }     memset(d, -1, sizeof(d));     memset(c, -1, sizeof(c));     memset(f, -1, sizeof(f));     for (int i = 0; i < a.size(); i++) {       if (judge(a[i], a[i], 1)) {         printf( %d n , a[i]);         return 0;       }       for (int i = 1;; i++) {         if (d[i] == -1) break;         d[i] = -1;       }     }     printf( -1 n );     return 0;   }