codeforces979C Kuro and Walking Route——深搜


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 1 to n, and n1 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 n

, x and y ( 1n3105,1x,yn, xy

) - the number of towns, index of the town Flowrisa and index of the town Beetopia, respectively.

n1

lines follow, each line contains two integers a and b ( 1a,bn, 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)
: his route would be 12
, (2,3): his route would be 23, (3,2): his route would be 32, (2,1): his route would be 21, (3,1): his route would be 321
  • .

Kuro can't choose pair (1,3)

since his walking route would be 123, in which Kuro visits town 1 (Flowrisa) and then visits town 3 (Beetopia), which is not allowed (note that pair (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)
: his route would be 12, (2,1): his route would be 21, (3,2): his route would be 312, (3,1): his route would be 31



#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>

using namespace std;

long long n,x,y;
vector<vector<int> > mp(300005, vector<int>(0,0));
int ans=0;
int vis[300005]={};
long long cntx=0,cnty=0;


int findy(int cur){///找到x到y的路径,并标记路径上的点(包括x,y)vis=1
    if(cur==y){
        vis[cur]=1;
        return 1;
    }
    int next;
    vis[cur]=-1;
    for(int i=0; i<mp[cur].size(); i++){
        next=mp[cur][i];
        if(vis[next]!=-1 && findy(next)==1){
            vis[cur]=1;
            return 1;
        }
    }

    return 0;
}

void calx(int cur){///找到挂靠在x上的点
    if(vis[cur]==2){
        return;

    }
    vis[cur]=2;
    cntx++;
    int next;
    for(int i=0;i<mp[cur].size();i++){
        next=mp[cur][i];
        if(vis[next]!=1){
            calx(next);
        }
    }
}

void caly(int cur){///找到挂靠在y上的点
    if(vis[cur]==2){
        return;

    }
    vis[cur]=2;
    cnty++;
    int next;
    for(int i=0;i<mp[cur].size();i++){
        next=mp[cur][i];
        if(vis[next]!=1){
            caly(next);
        }
    }
}

int main(){
    cin>>n>>x>>y;
    int a,b;

    for(int i=0;i<n-1;i++){
        cin>>a>>b;
        mp[a].push_back(b);
        mp[b].push_back(a);
    }

    findy(x);
    calx(x);
    caly(y);
    long long ans=n*(n-1)-cntx*cnty;
    cout<<ans<<endl;


    return 0;
}

要注意数据范围,用int溢出。使用longlong;

使用排除法,n个顶点任取两个做顶点对,有n*n-1个,减去不符合条件的即为答案。

把点分为三类:在x到y路径上的,挂在x上不与y通的,计数cntx个,挂在y上不与x通的,计数cnty个。

所有(第二类,第三类)的点对都是不符合条件的,所以ans=n*(n-1) - cntx*cnty;


*****学会了用vector实现动态二维数组:

初始化方式如下

vector<vector<int> > mp(300005, vector<int>(0,0));

--注意后面两个>>中间要加空格

向数组中加入元素方式如下

mp[a].push_back(b);
mp[b].push_back(a);

猜你喜欢

转载自blog.csdn.net/adobemomo/article/details/80373277