题目连接: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. (生产)vue-router:路由

    参考:https://router.vuejs.org/zh-cn/ 安装 直接下载 / CDN https://unpkg.com/vue-router/dist/vue-router.js 使用: ...

  2. C++ Knowledge series 4

    Programming language evolves always along with Compiler's evolvement The Semantics of Function C++ s ...

  3. typedef struct 与 struct

    学c++之前最好先学c.特别要说的是,一些虽然冠名为c++的项目的文件中却大部分都是c的代码. 比如我们这个例子: 在c语言中,定义一个结构体和其实适合c++中有区别的.比如我们有如下的代码: str ...

  4. Struts2 displaytag 导出文件为空

    以一个user的查询为例,在struts.xml配置里有这个action的配置: 然后可以在UserAction.java里找到list方法这个就是无非new 一个List<User> u ...

  5. Azure 媒体服务换新锁,更安全更方便,新钥匙请收好!

    不知道有多少人已经把家里的门锁换成了数字化的指纹锁?沿用了几百上千年的传统门锁,在技术的帮助下无疑变得更方便,不用带钥匙,还能远程控制和操作,最重要的是,终于不用担心「衣果(luǒ)着」出门扔垃圾,风 ...

  6. http相关文章目录

    四种常见的 POST 提交数据方式  https://imququ.com/post/four-ways-to-post-data-in-http.html

  7. Suggestion: use tools:overrideLibrary="android.support.v17.leanback" to force usage

    Android Studio下修改方法: 在manifest中添加<uses-sdk tools:overrideLibrary="android.support.v17.leanba ...

  8. 1.jdk的安装

    1.下载jdk放到某(E)盘底下的(java)某文件夹里 2.配置环境变量 (1)配置JAVA_HOME,CLASSPATH,PATH三个变量 其中JAVA_HOME必须的 JAVA_HOME=E:\ ...

  9. Sublime Text 3预览Markdown

    Sublime Text 3预览Markdown 安装控制器 按下按下ctrl+`出现小控制台 输入import urllib.request,os; pf = 'Package Control.su ...

  10. IOS GCD (事例下载图片)

    @interface HMViewController () @property (weak, nonatomic) IBOutlet UIImageView *imageView; @end @im ...