题意;

给出一棵树,其中有两个点,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. 继承数组的slice方法

    <script> var arr=[1,2,3,4,5]; console.log(Array.prototype.slice.call(arr,1)); </script> ...

  2. Dubbo服务化框架使用整理

    一.垂直应用架构拆分 在应用架构的演进过程中,垂直应用架构因为开发便捷,学习成本低,对于实现业务功能的增删改查提供了高效的开发支持,有利于前期业务高速发展的的快速实现.但是随着系统业务功能的不断扩展和 ...

  3. finecms同时调用子栏目和子栏目的文章怎么操作

    之前ytkah写过dedecms如何调用当前栏目的子栏目及子栏目文章,那如果是finecms如何同时调用子栏目和子栏目的文章呢? {list action=category pid=0 id=31} ...

  4. 经典的js返回(退个页面)

    <a href="javascript:history.back()">返回</a> <a href="javascript:window. ...

  5. 64位的pyd报"ImportError: No module named"错误

    今天仿照32位的ModuleA.cp35-win32.pyd,制作了64位的pyd.很随意地命名为ModuleA.cp35-win64.pyd,然后在64环境里编译.发现一直报"Import ...

  6. 手机端 https://doc.vux.li/zh-CN/components/badge.html

    https://doc.vux.li/zh-CN/components/badge.html 手机端前端框架

  7. [django]session设置与获取原理

    admin登录 情况1: 登录后会产生一个sessionid 情况2: 自定义设置了key后,会多一个sessionid, 登录后会替换为登录后的sessionid的key值 if username ...

  8. 如何创建线程第一种继承Thread类

    步骤 1:定义一个类 继承Thread类.2:重写Thread类的run方法.3:直接创建Thread的子类对象创建线程.4:调用start方法开启线程并调用线程的任务run方法执行.-------- ...

  9. Python 数据结构 树

    什么是树 数是一种抽象的数据类型(ADT)或是作这种抽象数据类型的数据结构,用来模拟具有树状结构性质的数据集合,它是由n(n>1)的有限个节点和节点之间的边组成的一个有层次关系的集合. 树的组成 ...

  10. Python复习基础篇

    4.4 使用列表的一部分   Python切片(切片就是取值的呗) print(players[0:3])  中括号,冒号隔开,顾头不顾尾. print([:4])    从开始取还是会顾尾的 pri ...