We are asked: count the number of paths of length 2 in a tree. A path of length 2 consists of two edges, i.e., three vertices v1-v2-v3 where edges (v1,v2) and (v2,v3) exist, and it's a simple path (distinct vertices). In a tree, any two edges that share a common middle vertex form a length-2 path. So essentially, for each vertex v, consider its degree d(v). The number of length-2 paths centered at v is C(d(v), 2) because we choose two distinct neighbors of v and the path goes from one neighbor to the other through v. Since the tree is undirected and paths are considered as sequences of vertices (likely unordered? but "paths of length 2" typically count each ordered pair of endpoints? We need to check examples.
Example 1: Tree with vertex 1 connected to 2,3,4. Degrees: deg(1)=3, deg(2)=1, deg(3)=1, deg(4)=1. Sum of C(deg(v),2) = C(3,2)=3. Output is 3. That matches. So they count each path as unordered? Actually path from 2 to 3 via 1, 2 to 4, 3 to 4. So three.
Example 2: Tree edges: 1-2,2-3,3-4,3-5. So vertices: deg(1)=1, deg(2)=2 (neighbors:1,3), deg(3)=3 (neighbors:2,4,5), deg(4)=1, deg(5)=1. Compute C(2,2)=1 (for vertex 2: paths: 1-? Actually at vertex 2, neighbors are 1 and 3, so path 1-2-3 is length 2. At vertex 3: neighbors 2,4,5 -> C(3,2)=3 paths: 2-3-4, 2-3-5, 4-3-5). Sum = 4. Output is 4. So indeed count each path with distinct endpoints (via a middle). That matches.
Thus answer = sum_{v} C(deg(v), 2) = sum_{v} deg(v)*(deg(v)-1)/2.
Constraints: n up to 10,000, edges n-1. So reading edges, compute degree array, then compute sum.
Edge cases: n=1 -> no edges, degrees 0, sum =0. n=2: one edge, each degree=1 -> sum=0. So answer 0. That's fine.
Implementation: read n, initialize deg array of size n+1 zeros. For each of n-1 lines, read a,b, increment deg[a], deg[b]. Then iterate vertices 1..n, accumulate ans += deg[i]*(deg[i]-1)//2. Print ans.
Time O(n). Memory O(n).
Double-check: The problem statement: "paths of length 2, i.e., consisting of two edges." In graph theory, a path is a sequence of vertices where consecutive vertices are adjacent and no vertex repeated. So indeed each length-2 path is uniquely determined by its middle vertex and its two endpoints (distinct). So sum of combinations.
Thus solution is straightforward.
But note: They might consider paths as unordered? The counting method gives the same as counting each directed path? Actually each path is considered as a set of vertices, but endpoints ordering doesn't matter: path (x,y,z) is same as (z,y,x). So it's fine.
Thus answer.
We'll provide solution in C++. But we need to output just the number.
Now we provide final answer.
But the user asked "Heidi has finally found the mythical Tree of Life ..." They want help, probably expects solution. We can respond with the code and explanation.
However the user is ChatGPT and the interaction is a typical problem-solving context; they might want just the code.
I will produce a clear solution in C++.
Let's write:
#include <bits/stdc++.h>
using namespace std;
int main() {
int n; cin >> n;
vector deg(n+1, 0);
for (int i=0; i<n-1; ++i) {
int a, b; cin >> a >> b;
deg[a]++; deg[b]++;
}
long long ans = 0;
for (int i=1; i<=n; ++i) {
long long d = deg[i];
ans += d * (d-1) / 2;
}
cout << ans << endl;
return 0;
}
That's it. Use long long to avoid overflow? n up to 10k, max degree could be n-1 = 9999, then d*(d-1)/2 is about 5e7, sum over n is at most maybe (n-1)(n-2)/2? Actually worst case star tree: one vertex degree n-1, others degree 1. Sum = (n-1)(n-2)/2 ~ 5e7 for n=10000 => about 50 million, fits in int (2.1e9). But safe to use long long.
Now we can produce final answer.
But the instruction: "You are an AI programming assistant. Your response should be the code only." However typical format for these problems might expect just code. But as ChatGPT, I'll give solution.
Given the conversation, the user asked: "Help her!" Probably expects code. I'll provide code.
But to be thorough, include brief explanation? The problem statement says "Print one integer". So code.
Thus final answer: C++ code.
I'll output that. ```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector deg(n + 1, 0);
for (int i = 0; i < n - 1; ++i) {
int a, b;
cin >> a >> b;
deg[a]++;
deg[b]++;
}
long long ans = 0;
for (int i = 1; i <= n; ++i) {
long long d = deg[i];
ans += d * (d - 1) / 2;
}
cout << ans << endl;
return 0;
}