题意;

给出一棵树,其中有两个点,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。

代码:

 #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 3e5 + ;
vector<int> g[N];
int num[N];
int par[N];
int n,x,y;
int dfs(int u,int fa)
{
par[u] = fa;
int cnt = ;
for (int v:g[u])
{
if (v != fa)
{
cnt += dfs(v,u);
}
}
return num[u] = cnt + ;
}
int main()
{
scanf("%d%d%d",&n,&x,&y);
for (int i = ;i < n;i++)
{
int u,v;
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
dfs(x,-);
long long ans = ;
int m = y;
while (par[m] != x)
{
m = par[m];
}
//printf("%d*\n",m);
ans = (long long) n * (n - );
ans -= (long long)num[y] * (1LL * (n - num[m]));
printf("%lld\n",ans);
return ;
}

codeforces 979C Kuro and Walking Route的更多相关文章

  1. codeforces 979 C. Kuro and Walking Route

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

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

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

  3. Kuro and Walking Route CodeForces - 979C (树上DFS)

    Kuro is living in a country called Uberland, consisting of nn towns, numbered from 11to nn, and n−1n ...

  4. 【codeforces-482div2-C】Kuro and Walking Route(DFS)

    题目链接:http://codeforces.com/contest/979/problem/C Kuro is living in a country called Uberland, consis ...

  5. Codeforces Round #482 (Div. 2) C 、 Kuro and Walking Route(dfs)979C

    题目链接:http://codeforces.com/contest/979/problem/C 大致题意 给出n个点,有n-1个边将他们链接.给出x,y,当某一路径中出现x....y时,此路不通.路 ...

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

    题目连接:http://codeforces.com/contest/979/problem/C 解题心得: 题意就是给你n个点,在点集中间有n-1条边(无重边),在行走的时候不能从x点走到y点,问你 ...

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

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 把x..y这条路径上的点标记一下. 然后从x开始dfs,要求不能走到那些标记过的点上.记录节点个数为cnt1(包括x) 然后从y开始 ...

  8. CF979C Kuro and Walking Route(简单的dfs/树形dp)

    题意:给出一个$n$个点,$n-1$条边的无向连通图,给出两个点$x,y$,经过$x$后的路径上就不能经过$y$,问可以走的路径$(u,v)$有多少条,($(u,v)$和$(v,u)$考虑为两条不同的 ...

  9. Codeforces 979E Kuro and Topological Parity - 动态规划 - 组合数学

    题目传送门 传送点 题目大意 给定$n$个标号依次为$1, 2, \cdots, n$的点,其中一些点被染成一些颜色,剩下的点没有染色.你需要添加一些有向边并将剩下的点染色,满足有向边从编号小的一端指 ...

随机推荐

  1. java 集合(一)ArrayList的继承树

    这是ArrayList的继承树,它继承了AbstractCollection抽象类,AbstractCollection类实现了Collection接口,Collection接口继承Iterable接 ...

  2. P1896 [SCOI2005]互不侵犯 状压dp

    正解:状压dp 解题报告: 看到是四川省选的时候我心里慌得一批TT然后看到难度之后放下心来觉得大概没有那么难 事实证明我还是too young too simple了QAQ难到爆炸TT我本来还想刚一道 ...

  3. python pip install 报错TypeError: unsupported operand type(s) for -=: 'Retry' and 'int' Command "python setup.py egg_info" failed with error code 1 in

    pip install http://download.pytorch.org/whl/cu80/torch-0.2.0.post3-cp27-cp27mu-manylinux1_x86_64.whl ...

  4. java实现消息队列的两种方式

    https://blog.csdn.net/fenfenguai/article/details/79257928

  5. 自然语言处理nlp工具

    1.结巴 适合语言:python 应用场景:中文分词较好 不适用于命名实体识别.信息抽取 2.nltk 适合语言:python 应用场景:不适用于中文分词,效果较差 提供了一些用于方便的方法

  6. 原生js获取子元素、给元素增加div

    //鼠标移入移出动画 解决页面闪屏问题 window.onload = function () { var el = document.createElement('div'); el.classNa ...

  7. 显示隐藏火车头快捷键Ctrl+t

    今天ytkah在使用火车头的时候也使用了Photoshop,用了组合键Ctrl+t来进行调整图层的大小,可能多按了几次的缘故吧,触发了火车头的隐藏老板键,找了半天也找到,因为当时编辑的任务还没保存,不 ...

  8. AdvStringGrid常用操作

    AdvStringGrid1.Options:=[goEditing]; then CanEdit := True;end; //设置单元格对齐方式 procedure TForm1.AdvStrin ...

  9. 数据库 - Navicat与pymysql模块

    一.Nabicat 在生产环境中操作MySQL数据库还是推荐使用命令行工具mysql,但在我们自己开发测试时, 可以使用可视化工具Navicat,以图形界面的形式操作MySQL数据库 官网下载:htt ...

  10. edraw的符号制作

    1.选中要制作的符号,在"符号"->"保存符号",这将保存为两个文件,1个是ede,另一个是png文件. 2. 3.名称,提示,随意写,只要你明白什么意思 ...