题意;

给出一棵树,其中有两个点,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. 《linux 文件目录》- touch/rm/mv/cat/head/tail/cp/mkdir/chmod/chown/find/locate/which/whereis

    一:基本 / 根目录下目录结构定义 /bin 常见的用户指令 ls/echo ...... 等 /boot 内核和启动文件 /dev 设备文件 /etc 系统和服务配置文件 /home 用户家目录 / ...

  2. Monkey测试执行_真机测试(2)

    提:按照前面的<Monkey环境搭建>先把环境搭建好. 此处为真机测试: 1.首先需要将手机连接到PC,测试连接是否正常,可在cmd里输入adb devices来进行验证: 2.输入adb ...

  3. java 集合(五)MapDemo

    package cn.sasa.demo3; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedH ...

  4. es修改指定的field(partial update)

    PUT /index/type/id 创建文档&替换文档,就是一样的语法一般对应到应用程序中,每次的执行流程基本是这样的:1.应用程序发起一个get请求,获取到document,展示到前台界面 ...

  5. SpringBoot-@value自定义参数

    自定义参数 配置文件值 name=itmayiedu.com 代码:       @Value("${name}")       private String name; @Res ...

  6. 【托业】【新托业TOEIC新题型真题】学习笔记2-题库一-->P5-6

    P5-6 --------------------------------------单词-------------------------------------- transfrom 转化 jus ...

  7. LeetCode-714.Best Time to Buy and Sell Stock with Transaction Fee

    Your are given an array of integers prices, for which the i-th element is the price of a given stock ...

  8. 微信公众号ID也可以修改了!

    差不多一年前,微信团队宣布个人类帐号一个自然年内可主动修改两次名称,那一天大家奔走相告纷纷修改成自己早就心仪的名字,有人猛然发现公众号名字改了,可ID还是xiaopipi(小屁屁)这可如何是好,洗不去 ...

  9. 各版本系统安装tesseract-ocr

    Mac版本 1.tesseract-ocr安装  brew install tesseract-ocr 注意:如果未安装brew命令,可以输入命令: brew官网:http://brew.sh /us ...

  10. push到Git时常见的失败

           之前学用git的时候,不想记命令,总是gui和bash交互的用,但是发现总出现push失败的问题,用gui来fetch的时候,显示下拉成功,但事实上并没有,这时候得在bash上用命令来下 ...