题目连接:http://codeforces.com/contest/979/problem/C

解题心得:

  • 题意就是给你n个点,在点集中间有n-1条边(无重边),在行走的时候不能从x点走到y点,问你任意两点一共有多少种走法,u到v和v到u为不同的走法。
  • 首先要明白的是n个点n-1条边就是一棵树。而在树中任意两点有且仅有一条路径。这样就简单了,先从x点dfs到y点,将唯一的一条路径标记上就将树x点和y点形成的子树分开了。然后不走标记的点就可以再次dfs的得到x点子树上的点数sumx,以及y点子树上的点数sumy。答案就是n*(n-1)-sumx*sumy
 #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 3e5+; vector <int> ve[maxn];
int n,x,y;
int vis[maxn];
void init(){
memset(vis,-, sizeof(vis));
scanf("%d%d%d",&n,&x,&y);
for(int i=;i<n;i++){
int a,b;
scanf("%d%d",&a,&b);
ve[a].push_back(b);
ve[b].push_back(a);
}
} int find(int pos){
vis[pos] = -;
if(pos == y) {
vis[y] = ;
return ;
}
for(int i=;i<ve[pos].size();i++) {
int v = ve[pos][i];
if(vis[v] == -) {
if(find(v) == ) {
vis[pos] = ;
return ;
}
}
}
return vis[pos];
} ll cnt_chx;
void find_chx(int pos){
vis[pos] = ;
cnt_chx++;
for(int i=;i<ve[pos].size();i++) {
int v = ve[pos][i];
if(vis[v] < )
find_chx(v);
}
} ll cnt_chy;
void find_chy(int pos) {
vis[pos] = ;
cnt_chy++;
for(int i=;i<ve[pos].size();i++) {
int v = ve[pos][i];
if(vis[v] < )
find_chy(v);
}
} int main(){
init();
ll ans = (ll)n*((ll)n-(ll));
find(x);
find_chx(x);
find_chy(y);
ans -= cnt_chx*cnt_chy;
printf("%lld\n",ans);
return ;
}

Codeforces Round #482 (Div. 2) :C - Kuro and Walking Route的更多相关文章

  1. 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时,此路不通.路 ...

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

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

  3. Codeforces Round #482 (Div. 2) : Kuro and GCD and XOR and SUM (寻找最大异或值)

    题目链接:http://codeforces.com/contest/979/problem/D 参考大神博客:https://www.cnblogs.com/kickit/p/9046953.htm ...

  4. Codeforces Round #482 (Div. 2) :B - Treasure Hunt

    题目链接:http://codeforces.com/contest/979/problem/B 解题心得: 这个题题意就是三个人玩游戏,每个人都有一个相同长度的字符串,一共有n轮游戏,每一轮三个人必 ...

  5. 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 ...

  6. 【Trie】【枚举约数】Codeforces Round #482 (Div. 2) D. Kuro and GCD and XOR and SUM

    题意: 给你一个空的可重集,支持以下操作: 向其中塞进一个数x(不超过100000), 询问(x,K,s):如果K不能整除x,直接输出-1.否则,问你可重集中所有是K的倍数的数之中,小于等于s-x,并 ...

  7. Codeforces Round #482 (Div. 2) B题

    题目链接:http://codeforces.com/contest/979/problem/B B. Treasure Hunt time limit per test1 second memory ...

  8. 【枚举】【贪心】Codeforces Round #482 (Div. 2) B. Treasure Hunt

    题意:给你3个字符串,3个人各对自己的字符串执行n轮操作,每一次选择一个字符变为任意一个和原来不同的字符.最后问你谁能使自己的串中的任意重复子串出现的次数最大化. 显然只需关注字符而非子串. 枚举每个 ...

  9. Codeforces Round #532 (Div. 2):F. Ivan and Burgers(贪心+异或基)

    F. Ivan and Burgers 题目链接:https://codeforces.com/contest/1100/problem/F 题意: 给出n个数,然后有多个询问,每次回答询问所给出的区 ...

随机推荐

  1. __getattr__,__getattribute__????

    class Foo(object): def __getattr__(sel,item): print('y') def __getattribute(self,item): print('x') o ...

  2. $.ajax显示进度条

  3. 轻松完成excel读写操作- 基于POI的框架BingExcel的使用(1)

    Bingexcel User Guide 使用maven进行项目开发目前项目的maven仓库是在github上,浏览地址为 https://github.com/bingyulei007/mvn-re ...

  4. Android内存监测工具使用

    用 Heap监测应用进程使用内存情况的步骤如下:1. 启动eclipse后,切换到DDMS透视图,并确认Devices视图.Heap视图都是打开的:2. 将手机通过USB链接至电脑,链接时需要确认手机 ...

  5. UIWindow及程序启动的过程

    1.   UIWindow才有自发显示的功能, 一个程序之所以能显示东西,是因为有window !//  [self.window makeKeyAndVisible]; 2.   任何view的显示 ...

  6. Cookie和Session 简单介绍

    cookie :     1.cookie是存在客户端(浏览器)的进程内存中和客户端所在的机器硬盘上     2.cookie只能能够存储少量文本,大概4K大小     3.cookie是不能在不同浏 ...

  7. 第四章 T-SQL编程

    1.前言->此T-SQL编程是基于sql server开发环境->关键字:T-SQL编程:游标:视图和索引 2.T-SQL编程基础->标识符:常规标识符必须以汉字.字母.下划线_.@ ...

  8. 实现vmare虚拟机系统随主机开机自动启动

    服务器主机上的虚拟机每次开机要手动启动是很麻烦的事,so,在网上找到一方法让虚拟机随主机开机自动运行:挺方便的,记录下来: 1.操作环境 主机:windows 2003 虚拟机:centos6 2.下 ...

  9. jQuery-prepend、append、before、after的区别

    举例说明,原始html代码如下: <ol> <li>List item 1</li> <li>List item 2</li> <li ...

  10. xampp中mysql重置root密码

    1.  停止mysql:用图形化工具或者在cmd命令下输入net stop mysql,在c盘根目录下输入 2.  打开cmd,切换目录到 /xampp/mysql/bin, 运行    mysqld ...