func_code_string: #include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 777;   vector<int> G[maxn];   map<int, int> savesiz[maxn];   int siz[maxn];   vector<int> ans;   int n;   void dfs(int s, int fa) {     siz[s] = 1;     int cnt = 0;     for (int d : G[s]) {       if (d == fa) continue;       dfs(d, s);       savesiz[s][siz[d]]++;       cnt++;       siz[s] += siz[d];     }     if (n - siz[s] > 0) {       savesiz[s][n - siz[s]]++;       cnt++;     }     if (savesiz[s].size() == 1 && cnt >= 2) ans.push_back(s);   }   vector<int> aans;   map<int, int> have[maxn];   void func(int s) {     int nowsiz = 1;     int nowid = G[s][0];     int fa = s;     while (G[nowid].size() == 2) {       nowsiz++;       if (G[nowid][0] == fa) {         fa = nowid;         nowid = G[nowid][1];       } else {         fa = nowid;         nowid = G[nowid][0];       }     }     if (have[nowid].count(nowsiz)) return;     if (have[nowid].size() >= 2) return;     have[nowid][nowsiz] = 1;     if ((savesiz[nowid][nowsiz] == 1 && savesiz[nowid].size() == 2) ||         savesiz[nowid].size() == 1) {       aans.push_back(s);     }   }   set<int> se[maxn];   int vis[maxn];   bool ck(int s) {     for (int i = 0; i <= n; i++) {       se[i].clear();       vis[i] = 0;     }     queue<pair<int, int> > q;     vis[s] = 1;     q.push(make_pair(s, 0));     while (!q.empty()) {       int nowid = q.front().first;       int nowdis = q.front().second;       q.pop();       se[nowdis].insert(G[nowid].size());       if (se[nowdis].size() >= 2) return false;       for (int d : G[nowid]) {         if (vis[d]) continue;         vis[d] = 1;         q.push(make_pair(d, nowdis + 1));       }     }     return true;   }   void solve() {     scanf( %d , &n);     if (n == 1) {       cout << 1 << endl;       return;     }     for (int i = 1; i < n; i++) {       int a, b;       scanf( %d %d , &a, &b);       G[a].push_back(b);       G[b].push_back(a);     }     dfs(1, 1);     for (int iid : ans) {       if (ck(iid)) {         cout << iid << endl;         return;       }     }     for (int i = 1; i <= n; i++) {       if (G[i].size() == 1) {         func(i);       }     }     for (int iid : aans) {       if (ck(iid)) {         cout << iid << endl;         return;       }     }     cout << -1 << endl;   }   signed main() { solve(); }   