func_code_string: #include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e5 + 7;   int vis1[MAXN], vis2[MAXN], dis[MAXN];   int st, ed, n, ans;   queue<int> q;   vector<int> G[MAXN];   void Bfs1() {     for (; !q.empty();) q.pop();     q.push(1);     vis1[1] = 1;     for (; !q.empty();) {       int u = q.front();       q.pop();       for (auto v : G[u]) {         if (vis1[v]) continue;         q.push(v);         vis1[v] = u;       }       st = u;     }   }   void Bfs2() {     for (; !q.empty();) q.pop();     q.push(st);     vis2[st] = st;     for (; !q.empty();) {       int u = q.front();       q.pop();       for (auto v : G[u]) {         if (vis2[v]) continue;         q.push(v);         vis2[v] = u;       }       ed = u;     }   }   int degree(int x) { return (int)G[x].size(); }   bool check(int root) {     int Tim = 0;     for (int i = 1; i <= n; i++) vis1[i] = dis[i] = 0;     for (; !q.empty();) q.pop();     q.push(root);     vis1[root] = 1;     for (; !q.empty();) {       int u = q.front();       q.pop();       for (auto v : G[u]) {         if (vis1[v]) continue;         if (++Tim == n) {         }         q.push(v);         vis1[v] = vis1[u] + 1;         if (!dis[vis1[v]]) dis[vis1[v]] = v;         if (degree(v) != degree(dis[vis1[v]])) return false;       }     }     ans = root;     return true;   }   bool checkCenter() {     int Tim = 0;     int D = 0;     for (int u = ed; u != st; u = vis2[u]) D++;     if (D & 1) return false;     int center = ed;     for (int i = 0; i != D / 2; i++, center = vis2[center])       ;     if (check(center)) return true;     for (int i = 1; i <= n; i++) vis1[i] = dis[i] = 0;     for (; !q.empty();) q.pop();     q.push(center);     vis1[center] = 1;     for (; !q.empty();) {       int u = q.front();       q.pop();       for (auto v : G[u]) {         if (vis1[v]) continue;         if (++Tim == n) {         }         q.push(v);         vis1[v] = vis1[u] + 1;         if (!dis[vis1[v]]) dis[vis1[v]] = v;         if (degree(v) != degree(dis[vis1[v]])) {           for (int x = v, tim = 0;;) {             if (++tim == n) break;             if (G[x].size() == 1) {               if (check(x))                 return true;               else                 break;             } else               x = (vis1[G[x][0]] == 0) ? G[x][0] : G[x][1];           }           for (int x = dis[vis1[v]], tim = 0;;) {             if (++tim == n) break;             if (G[x].size() == 1) {               if (check(x))                 return true;               else                 break;             } else               x = (vis1[G[x][0]] == 0) ? G[x][0] : G[x][1];           }           return false;         }       }     }     ans = center;     return true;   }   int main() {     cin >> n;     for (int i = 1; i < n; i++) {       int u, v;       cin >> u >> v;       G[u].push_back(v);       G[v].push_back(u);     }     Bfs1();     Bfs2();     if (check(st) || check(ed))       cout << ans;     else if (checkCenter())       cout << ans;     else       cout << -1;   }   