Algorithm


C. Kuro and Walking Route
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Kuro is living in a country called Uberland, consisting of n towns, numbered from 11 to n, and n1�−1 bidirectional roads connecting these towns. It is possible to reach each town from any other. Each road connects two towns a and b. Kuro loves walking and he is planning to take a walking marathon, in which he will choose a pair of towns (u,v)(�,�) (uv�≠�) and walk from u using the shortest path to v (note that (u,v)(�,�) is considered to be different from (v,u)(�,�)).

Oddly, there are 2 special towns in Uberland named Flowrisa (denoted with the index x) and Beetopia (denoted with the index y). Flowrisa is a town where there are many strong-scent flowers, and Beetopia is another town where many bees live. In particular, Kuro will avoid any pair of towns (u,v)(�,�) if on the path from u to v, he reaches Beetopia after he reached Flowrisa, since the bees will be attracted with the flower smell on Kuro’s body and sting him.

Kuro wants to know how many pair of city (u,v)(�,�) he can take as his route. Since he’s not really bright, he asked you to help him with this problem.

Input

The first line contains three integers nx and y (1n31051≤�≤3⋅1051x,yn1≤�,�≤�xy�≠�) - the number of towns, index of the town Flowrisa and index of the town Beetopia, respectively.

n1�−1 lines follow, each line contains two integers a and b (1a,bn1≤�,�≤�ab�≠�), describes a road connecting two towns a and b.

It is guaranteed that from each town, we can reach every other town in the city using the given roads. That is, the given map of towns and roads is a tree.

Output

A single integer resembles the number of pair of towns (u,v)(�,�) that Kuro can use as his walking route.

Examples
input
Copy
3 1 3
1 2
2 3
output
Copy
5
input
Copy
3 1 3
1 2
1 3
output
Copy
4
Note

On the first example, Kuro can choose these pairs:

  • (1,2)(1,2): his route would be 121→2,
  • (2,3)(2,3): his route would be 232→3,
  • (3,2)(3,2): his route would be 323→2,
  • (2,1)(2,1): his route would be 212→1,
  • (3,1)(3,1): his route would be 3213→2→1.

Kuro can't choose pair (1,3)(1,3) since his walking route would be 1231→2→3, in which Kuro visits town 11 (Flowrisa) and then visits town 33 (Beetopia), which is not allowed (note that pair (3,1)(3,1) is still allowed because although Kuro visited Flowrisa and Beetopia, he did not visit them in that order).

On the second example, Kuro can choose the following pairs:

  • (1,2)(1,2): his route would be 121→2,
  • (2,1)(2,1): his route would be 212→1,
  • (3,2)(3,2): his route would be 3123→1→2,
  • (3,1)(3,1): his route would be 313→1.

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <bits/stdc++.h>

using namespace std;

int const N = 3e5 + 1;
int n, x, y, p[N], cnt;
bool vis[N];
vector<int> path;
vector<vector<int> > g;
queue<int> q;

int BFS() {
  q.push(x);
  p[x] = x;
  vis[x] = true;

  while(!q.empty()) {
    int fr = q.front();
    q.pop();

    for(int i = 0; i < g[fr].size(); ++i) {
      if(!vis[g[fr][i]]) {
        vis[g[fr][i]] = true;
        p[g[fr][i]] = fr;
        q.push(g[fr][i]);
      }
    }
  }

  int v = y;
  path.push_back(v);
  while(v != p[v]) {
    v = p[v];
    path.push_back(v);
  }

  path.pop_back();
  return path.back();
}

void DFS(int u) {
  vis[u] = true;
  ++cnt;

  for(int i = 0; i < g[u].size(); ++i)
    if(!vis[g[u][i]])
      DFS(g[u][i]);
}

int main() {
  cin >> n >> x >> y;
  --x, --y;
  g.resize(n);
  for(int i = 0, a, b; i < n - 1; ++i) {
    cin >> a >> b;
    --a, --b;
    g[a].push_back(b);
    swap(a, b);
    g[a].push_back(b);
  }

  int rem = BFS();

  memset(vis, false, sizeof vis);
  vis[rem] = true;
  DFS(x);

  int tmp = cnt;
  cnt = 0;

  memset(vis, false, sizeof vis);
  vis[path[1]] = true;
  DFS(y);

  cout << (1ll * n * (n - 1)) - (1ll * tmp * cnt) << endl;

  return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
3 1 3
1 2
2 3

Output

x
+
cmd
5
Advertisements

Demonstration


Codeforces Solution-C. Kuro and Walking Route-Solution in C, C++, Java, Python

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+