Codeforces Round #482 (Div. 2) :C - Kuro and Walking Route
题目连接: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的更多相关文章
- 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
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 把x..y这条路径上的点标记一下. 然后从x开始dfs,要求不能走到那些标记过的点上.记录节点个数为cnt1(包括x) 然后从y开始 ...
- 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 ...
- Codeforces Round #482 (Div. 2) :B - Treasure Hunt
题目链接:http://codeforces.com/contest/979/problem/B 解题心得: 这个题题意就是三个人玩游戏,每个人都有一个相同长度的字符串,一共有n轮游戏,每一轮三个人必 ...
- 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 ...
- 【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,并 ...
- Codeforces Round #482 (Div. 2) B题
题目链接:http://codeforces.com/contest/979/problem/B B. Treasure Hunt time limit per test1 second memory ...
- 【枚举】【贪心】Codeforces Round #482 (Div. 2) B. Treasure Hunt
题意:给你3个字符串,3个人各对自己的字符串执行n轮操作,每一次选择一个字符变为任意一个和原来不同的字符.最后问你谁能使自己的串中的任意重复子串出现的次数最大化. 显然只需关注字符而非子串. 枚举每个 ...
- Codeforces Round #532 (Div. 2):F. Ivan and Burgers(贪心+异或基)
F. Ivan and Burgers 题目链接:https://codeforces.com/contest/1100/problem/F 题意: 给出n个数,然后有多个询问,每次回答询问所给出的区 ...
随机推荐
- 《Cron表达式详解》
Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式: Seconds Minutes Hours DayofMonth Month ...
- android libs库中的armeabi-v7a,armeabi和x86
以下内容转载于:http://blog.csdn.net/liumou111/article/details/52949156 1.区别: 这三者都表示的是CPU类型,早期的Android系统几乎只支 ...
- Illegal access:this web application instance has been stopped already
七月 23, 2014 2:34:35 下午 org.apache.catalina.loader.WebappClassLoader loadClass信息: Illegal access: thi ...
- win8下使用IIS服务器及自定义服务器端包含模块(SSI)步骤
配置完过段时间就容易忘记,特此记录. 1.开启IIS服务器. 默认没有安装,需要先安装. 打开控制面板--> 打开“程序和功能”--> 左侧选择“启用或关闭windows功能”--> ...
- url获取MVC域,action,controller的方法
域:filterContext.RequestContext.RouteData.DataTokens["area"] 控制器:filterContext.RequestConte ...
- SQL语句关于时间的查询小心得,希望大家给点意见
完全使用时间函数去搞定查询日期,之前写的可能有些问题,现在删了修正一下 本月记录: SELECT * FROM 表 WHERE datediff(month,[dateadd],getdate())= ...
- UESTC 757 棋盘
虽然是水题,但是还是很interesting的.(大概就是我最晚出这个题了... 博弈感觉就是靠yy能力啊.这题是对称性. 最后的必败态是白色格子对称的,一旦对称形成,对手怎么选,跟随就好,对手无法摆 ...
- 单调队列 poj2823,fzu1894
题目链接:http://poj.org/problem?id=2823 用RMQ超时了,我想应该是不会的,看discuss说,之前RMQ过了. 维护两个单调队列. 单调递减的队列,每插入一个时: 超过 ...
- 二叉树遍历,先序序列+中序序列=后序序列,Poj(2255)
这里我参考了JHF大神的写法啦,直接把输出写在了建树的过程中了. 思路: 先根据先序序列找到根节点,在找该节点在中序序列中的位置,这样,左右子树有分开了.这里的细节值得注意一下,不然很容易建树出错.( ...
- python 3+djanjo 2.0.7简单学习(一)
1.安装django pip install django 我这里已经安装过了 整个目录结构如下: votes : migrations : __init__.py : admin.py : apps ...