题目连接: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. 心得整理之一--RDLC多数据源多表

    我将项目中的一部分提炼出来,写了这个Demo. 先说一下需求, 从 API接口, 获取数据源, 调用RDLC 生成PDF文件. (后面还有涉及到使用福昕PDf阅读器进行设置文件自定义内容,以供外部程序 ...

  2. 学习笔记_TCP编程,服务端

    public void startTcpService(String ip) { try { InetAddress ipAddress = InetAddress.getByName(ip); tc ...

  3. redis笔记(三)

     redis配置文件 配置文件对单位大小写不敏感 tcp-backlog  511   高并发环境连接数 tcp-keepalive   单位为秒 0 表示不会进行keepalive检测,,,建议设置 ...

  4. easyUI 节点树选择

    定义: <input id="treeFFatherId" name="treeFFatherId" value="" style=& ...

  5. JavaScript 获取数组的最大值和最小值

    js获取数组最大值和最小值 使用apply方法: var a = [1,2,3,5]; console.log(Math.max.apply(null, a));//最大值 console.log(M ...

  6. 【Tim Sweeney】Why C++ for Unreal 4?

    The first three generations of the Unreal Engine included a sandboxed scripting language, UnrealScri ...

  7. Fiddler-3 Fiddler抓包-手机端配置

    电脑端可以通过Fiddler监听手机端的http请求.需要两个步骤:首先配置Fiddler,再配置手机端. 1 配置 Fiddler 允许远程设备连接: 菜单Tools - Telerik Fiddl ...

  8. DedeCms中出现Safe Alert: Request Error step 1/2 的解决方法

    dedecms安全警告:Safe Alert: Request Error step 2!不知道大家有没有发现这个现象.只从Dedecms官方公布了之前的版本有严重的漏洞以来,现在在仿站的时候都是采用 ...

  9. Uva 11384 正整数序列

    题目链接:https://vjudge.net/problem/UVA-11384 题意:给定正整数 n,用最少的操作把序列 1,2,,,n 全部变成 0: 操作是:每次可以从序列中选择一个或者多个, ...

  10. Linux 初学者:移动文件

    你学习了有关目录和访问目录的权限是如何工作的.你在这些文章中学习的大多数内容都可应用于文件 -- Paul Brown 在之前的该系列的部分中, 你学习了有关目录 和 访问目录 的权限 是如何工作的. ...