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时,此路不通。路径(u,v)和(v,u)是不同的。
思路:一开始大神是给每个点都用BFS找出能到的点的路径,同时记录搜索的状态,但出了点小问题,估计修正了也回超时,现在给出大神思路,因为不能自己到自己所以点对数肯定是n*(n-1)这是全部的可能,那我们只要找出不可能的情况相减,那就是答案的对不对?这是肯定的,现在我们来考虑怎么找出不成立的情况,我们首先可以对x到y用dfs找到经过的点,在对x用dfs找到x可以去到那些点(这同时也是那些点可以到x。。这个结论很重要)这些点不包括x与y还有x到经历的点,共sumx,同理找到sumy,答案为n*(n-1)-sumx*sumy;为什么呢?画个图呗,首先假设有一条路径x到y是不符合的那么假设A点可以到x,那A-x-y这条路是不满足条件的,同理假设B点可以到y,那X-y-B也 不,满足。
Ac代码:
#include<bits/stdc++.h>
using namespace std;
int n,x,y;
#define ll long long
const int maxn=+;
vector<int>e[maxn];
bool xoy[maxn],book[maxn];
int per[maxn];
void init( )
{
memset(xoy,false,sizeof(xoy));
memset(book,false,sizeof(book));
memset(per,-,sizeof(per));
}
void perdfs(int u,int fa)
{
per[u]=fa;
if(u==y)
return ;
for(int i= ; i<e[u].size() ; i++)
{
int v=e[u][i];
if(v!=fa)
{
perdfs(v,u);
}
}
}
int dfs(int u)
{
book[u]=true;
int sum=;
for(int i= ; i<e[u].size() ; i++)
{
int v=e[u][i];
if(!book[v]&&!xoy[v]&&v!=x&&v!=y)
sum+=dfs(v);
}
return sum;
}
int main( )
{
init();
scanf("%d%d%d",&n,&x,&y);
for(int i= ; i<=n- ; i++)
{
int u,v;
scanf("%d%d",&u,&v);
e[u].push_back(v);
e[v].push_back(u);
}
perdfs(x,-);
for(int i=per[y];i!=x;i=per[i])
xoy[i]=true;
ll sumx=dfs(x);
ll sumy=dfs(y);
ll ans=(ll)n*(n-)-(ll)(sumx*sumy);
cout<<ans<<endl;
return ; }
参考出处
https://blog.csdn.net/amovement/article/details/80323045
Codeforces Round #482 (Div. 2) C 、 Kuro and Walking Route(dfs)979C的更多相关文章
- Codeforces Round #482 (Div. 2) :C - Kuro and Walking Route
题目连接:http://codeforces.com/contest/979/problem/C 解题心得: 题意就是给你n个点,在点集中间有n-1条边(无重边),在行走的时候不能从x点走到y点,问你 ...
- 【Codeforces Round #482 (Div. 2) C】Kuro and Walking Route
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 把x..y这条路径上的点标记一下. 然后从x开始dfs,要求不能走到那些标记过的点上.记录节点个数为cnt1(包括x) 然后从y开始 ...
- Codeforces Round #539 (Div. 2) - D. Sasha and One More Name(思维)
Problem Codeforces Round #539 (Div. 2) - D. Sasha and One More Name Time Limit: 1000 mSec Problem ...
- Codeforces Round #482 (Div. 2) B、Treasure Hunt(模拟+贪心)979B
题目 大致题意 n表示要进行n次操作,接着给出三个字符串,表示三个人初始拥有的串.每次操作要替换字符串中的字母,询问最后在游戏中曾出现过的相同的子串谁最多. 思路 (1) 讨论最多的子串,肯定是全部 ...
- Codeforces Round #647 (Div. 2) C. Johnny and Another Rating Drop(数学)
题目链接:https://codeforces.com/contest/1362/problem/C 题意 计算从 $0$ 到 $n$ 相邻的数二进制下共有多少位不同,考虑二进制下的前导 $0$ .( ...
- Codeforces Round #319 (Div. 2) C Vasya and Petya's Game (数论)
因为所有整数都能被唯一分解,p1^a1*p2^a2*...*pi^ai,而一次询问的数可以分解为p1^a1k*p2^a2k*...*pi^aik,这次询问会把所有a1>=a1k &&am ...
- Codeforces Round #581 (Div. 2) B. Mislove Has Lost an Array (贪心)
B. Mislove Has Lost an Array time limit per test1 second memory limit per test256 megabytes inputsta ...
- Codeforces Round #561 (Div. 2) E. The LCMs Must be Large(数学)
传送门 题意: 有 n 个商店,第 i 个商店出售正整数 ai: Dora 买了 m 天的东西,第 i 天去了 si 个不同的个商店购买了 si 个数: Dora 的对手 Swiper 在第 i 天去 ...
- 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 ...
随机推荐
- spring-初始化bean
@EnableScheduling@Servicepublic class BaseTask implements InitializingBean, ApplicationContextAware{ ...
- android官方手册学习笔记
数据存储 在提交sharedpreference 修改的时候,用apply代替commit 避免UI线程阻塞 设备兼容 系统会自动根据当前屏幕的大小等,在相应的文件夹里去找资源,如large等等 ...
- actionbar中添加searchview并监听期伸缩/打开的方法
首先在xml中设置actionviewclass <item android:id="@+id/m1" android:title="setting" a ...
- 使用批处理替换windows系统中的hosts文件
chcp 936 >nul@echo offmode con lines=30 cols=60%1 mshta vbscript:CreateObject("Shell.Applica ...
- CF438D The Child and Sequence
外国人的数据结构题真耿直 唯一有难度的操作就是区间取模,然而这个东西可以暴力弄一下,因为一个数$x$被取模不会超过$logn$次. 证明如下(假设$x Mod y$): 如果$y \leq \fr ...
- WordCount编码和测试
WordCount编码和测试 项目地址:https://github.com/handsomesnail/WordCount PSP表格 PSP2.1 PSP阶段 预估耗时(分钟) 实际耗时(分钟) ...
- Android应用启动优化:一种DelayLoad的实现和原理
http://www.androidperformance.com/2015/11/18/Android-app-lunch-optimize-delay-load.html
- ROS nodelet 理解记录
发现网上许多的例子都是基于官网的例子,还需要做进一步的说明. 1. NODELET_DEBUG 是无法打印的信息的,需要使用NODELET_INFO NODELET_DEBUG("Addin ...
- ASP.NET网页动态添加数据行
一看到这标题<ASP.NET网页动态添加数据行>,想起来似乎有点难实现.因为网页的周期性原因,往往在PostBack之后,状态难于有所保留.但Insus.NET又想实现这样的效果,用户点击 ...
- Android - AndroidStudio 的熟悉
开发环境 * JDK * SDK * AndroidStudio * Genimotioin HelloWorld [ 第一个Android项目建立 ] * 创建项目 [ 项目相关目录 ] Hell ...