codeforces 979C Kuro and Walking Route
题意;
给出一棵树,其中有两个点,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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 【codeforces-482div2-C】Kuro and Walking Route(DFS)
题目链接:http://codeforces.com/contest/979/problem/C Kuro is living in a country called Uberland, consis ...
- 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时,此路不通.路 ...
- Codeforces Round #482 (Div. 2) :C - Kuro and Walking Route
题目连接:http://codeforces.com/contest/979/problem/C 解题心得: 题意就是给你n个点,在点集中间有n-1条边(无重边),在行走的时候不能从x点走到y点,问你 ...
- 【Codeforces Round #482 (Div. 2) C】Kuro and Walking Route
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 把x..y这条路径上的点标记一下. 然后从x开始dfs,要求不能走到那些标记过的点上.记录节点个数为cnt1(包括x) 然后从y开始 ...
- CF979C Kuro and Walking Route(简单的dfs/树形dp)
题意:给出一个$n$个点,$n-1$条边的无向连通图,给出两个点$x,y$,经过$x$后的路径上就不能经过$y$,问可以走的路径$(u,v)$有多少条,($(u,v)$和$(v,u)$考虑为两条不同的 ...
- Codeforces 979E Kuro and Topological Parity - 动态规划 - 组合数学
题目传送门 传送点 题目大意 给定$n$个标号依次为$1, 2, \cdots, n$的点,其中一些点被染成一些颜色,剩下的点没有染色.你需要添加一些有向边并将剩下的点染色,满足有向边从编号小的一端指 ...
随机推荐
- XSL常用用法语句
1.xsl的开始语句 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Tra ...
- 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 ...
- 六、SpringBoot与数据访问
六.SpringBoot与数据访问 1.JDBC spring: datasource: username: root password: 123456 url: jdbc:mysql://192.1 ...
- private static final Logger logger= LoggerFactory.getLogger(WhMainBusi.class);
LoggerFactory.getLogger(WhMainBusi.class):指定类初始化日志对象,在日志输出的时候,将会打印日志信息所在的类.如: logger.info("日志信息 ...
- 前端 HTML标签属性
HTML标签可以设置属性,如下: <div id="i1">这是一个div标签</div> <p class='p1 p2 p3'>这是一个段落 ...
- 如何把Composer镜像迁移到Laravel China 维护的镜像?
今天在更新Laravel-admin:1.6.0提示没有对应的包,后面才发现需要使用官方或者 Laravel-China 的 composer 镜像,phpcomposer 镜像已经停止维护了.怎么从 ...
- Socket接口
Socket接口 Socket: Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口.在设计模式中,Socket其实就是一个门面模式,它把复杂的TCP/IP协议族隐藏在Sock ...
- javascript篇-typeof,instanceof,constructor,toString判断数据类型的用法和区别
javascript基本数据类型有:string,number,Boolean,undefined,null 引用类型(复杂类型):object, ES6中新增了一种数据类型:Symbol 以上数据类 ...
- 云计算概述及Centos7下安装kvm虚拟机
云计算(cloud computing)是基于互联网的相关服务的增加.使用和交付模式,通常涉及通过互联网来提供动态易扩展且经常是虚拟化的资源 云计算到底是什么? 按定义:云计算指的是一种使用模式,是基 ...
- xtrabackup全库还原+binlog日志还原
1.场景 mysql数据库误删某个库.误删表或者误删除数据 如下模拟图:备份策略定为每天凌晨进行全库备份,在B时间点进行了误操作以后,有两种恢复场景,一种是恢复到B时间点误操作前,一种是恢复到C时间点 ...