hup

Revision en1, by b3r2k9, 2026-01-09 10:39:49

include <bits/stdc++.h>

using namespace std;

const int MAXN = 2005; vector adj[MAXN]; bool visited[MAXN]; int v_count, e_count;

void dfs(int u) { visited[u] = true; v_count++; e_count += adj[u].size(); for (int v : adj[u]) { if (!visited[v]) { dfs(v); } } }

int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; for (int i = 1; i <= N; i++) { adj[i].clear(); visited[i] = false; }

for (int i = 1; i <= N - 1; i++) {
    int x, y;
    cin >> x >> y;
    adj[x].push_back(y);
    adj[y].push_back(x);
}
int comp_A_v = -1; 
for (int i = 1; i <= N; i++) {
    if (!visited[i]) {
        v_count = 0;
        e_count = 0;
        dfs(i);
        e_count /= 2; 

        if (e_count > v_count) {
            cout << 0 << '\n';
            return 0;
        }

        if (e_count == v_count - 1) {
            if (comp_A_v != -1) {
                cout << 0 << '\n';
                return 0;
            }
            comp_A_v = v_count;
        }
    }
}
if (comp_A_v == -1) {
    cout << 0 << '\n';
    return 0;
}
long long ways = 1LL * comp_A_v * (comp_A_v - 1) / 2 + 1LL * comp_A_v * (N - comp_A_v);
cout << ways << '\n';

return 0;

}

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English b3r2k9 2026-01-09 10:39:49 1488 Initial revision (published)