codeforces 979C Kuro and Walking Route

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了codeforces 979C Kuro and Walking Route相关的知识,希望对你有一定的参考价值。

???????????????dp   scan   str   back   amp   ??????   turn   walk   ??????   

?????????

???????????????????????????????????????x???y???????????????x???????????????????????????y????????????????????????(u,v)???????????????(u,v)???(v,u)?????????????????????????????????

?????????

????????????dp???dfs?????????x???y??????????????????x???y???????????????????????????x????????????a??????y????????????b????????????????????????n*(n-1) - a * b???

?????????

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4 #include <vector>
 5 using namespace std;
 6 const int N = 3e5 + 10;
 7 vector<int> g[N];
 8 int num[N];
 9 int par[N];
10 int n,x,y;
11 int dfs(int u,int fa)
12 {
13     par[u] = fa;
14     int cnt = 0;
15     for (int v:g[u])
16     {
17         if (v != fa)
18         {
19             cnt += dfs(v,u);
20         }
21     }
22     return num[u] = cnt + 1;
23 }
24 int main()
25 {
26     scanf("%d%d%d",&n,&x,&y);
27     for (int i = 1;i < n;i++)
28     {
29         int u,v;
30         scanf("%d%d",&u,&v);
31         g[u].push_back(v);
32         g[v].push_back(u);
33     }
34     dfs(x,-1);
35     long long ans = 0;
36     int m = y;
37     while (par[m] != x)
38     {
39         m = par[m];
40     }
41     //printf("%d*\n",m);
42     ans = (long long) n * (n - 1);
43     ans -= (long long)num[y] * (1LL * (n - num[m]));
44     printf("%lld\n",ans);
45     return 0;
46 }

 

以上是关于codeforces 979C Kuro and Walking Route的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #482 (Div. 2) C Kuro and Walking Route

[Codeforces 979D] Kuro and GCD and XOR and SUM

CodeForces 979 D Kuro and GCD and XOR and SUM

Codeforces 979 D. Kuro and GCD and XOR and SUM(异或和,01字典树)

Codeforces Round #482 (Div. 2)D. Kuro and GCD and XOR and SUM+字典树

D. Kuro and GCD and XOR and SUM(Trie &筛)