codeforces 979C Kuro and Walking Route

题意;

给出一棵树,其中有两个点,x和y,限制走了x之后的路径上不能有y,问可以走的路径(u,v)有多少条,(u,v)和(v,u)考虑为两条不同的路径。

思路:

简单树形dp,dfs统计在x到y路径(不包括x和y)之外的所有点,在x这边的有a个,y这边的有b个,那么答案就是n*(n-1) - a * b。

代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4 #include <vector>
 5 using namespace std;
 6 const int N = 3e5 + 10;
 7 vector<int> g[N];
 8 int num[N];
 9 int par[N];
10 int n,x,y;
11 int dfs(int u,int fa)
12 {
13     par[u] = fa;
14     int cnt = 0;
15     for (int v:g[u])
16     {
17         if (v != fa)
18         {
19             cnt += dfs(v,u);
20         }
21     }
22     return num[u] = cnt + 1;
23 }
24 int main()
25 {
26     scanf("%d%d%d",&n,&x,&y);
27     for (int i = 1;i < n;i++)
28     {
29         int u,v;
30         scanf("%d%d",&u,&v);
31         g[u].push_back(v);
32         g[v].push_back(u);
33     }
34     dfs(x,-1);
35     long long ans = 0;
36     int m = y;
37     while (par[m] != x)
38     {
39         m = par[m];
40     }
41     //printf("%d*\n",m);
42     ans = (long long) n * (n - 1);
43     ans -= (long long)num[y] * (1LL * (n - num[m]));
44     printf("%lld\n",ans);
45     return 0;
46 }

原文地址:https://www.cnblogs.com/kickit/p/9038994.html

时间: 2024-10-09 17:38:19

codeforces 979C Kuro and Walking Route的相关文章

Codeforces 1296C - Yet Another Walking Robot

题目大意: 给定一个机器人的行走方式 你需要取走一段区间 但要保证取走这段区间后机器人最终到达的终点位置是不变的 问这段区间最短时是哪一段 解题思路: 易得,如果重复走到了某些已经走过的点,那么肯定就有一段区间可以被删除 但是行走次数最大有2e5,即用数组记录坐标状态的话起码要开4e5*4e5的空间,显然不可能 所以可以用map储存上一次走到某个坐标是第几步 那么每次只要判断当前的坐标是否已经被走过即可,走过的话就尝试更新答案 因为map中未调用过的int值为0 所以让原点的步数设置为1防止混淆

Codeforces Round #482 (Div. 2)

A. Pizza, Pizza, Pizza!!! 注意:long long B. Treasure Hunt 注意:第五个样例(aaaaa,n==1)的情况要先处理一下. 感受:还是读题的问题,and the ribbon abcdabc has the beauty of 2 because its subribbon abc appears twice.(误导信息一),题目没要求是最大的连续子串.还有就是只能修改一个位置,记得给的单词是segment(一段同颜色的),后来题面好像改了. #

code forces 979C

C. Kuro and Walking Route time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Kuro is living in a country called Uberland, consisting of nn towns, numbered from 11 to nn, and n?1n?1 bidirectio

2018HDU多校训练-3-Problem M. Walking Plan

链接:http://acm.hdu.edu.cn/showproblem.php?pid=6331 Walking Plan Problem Description There are n intersections in Bytetown, connected with m one way streets. Little Q likes sport walking very much, he plans to walk for q days. On the i -th day, Little

Google Maps API V3 之 路线服务

概述 您可以使用 DirectionsService 对象计算路线(使用各种交通方式).此对象与 Google Maps API 路线服务进行通信,该服务会接收路线请求并返回计算的结果.您可以自行处理这些路线结果,也可以使用 DirectionsRenderer 对象呈现这些结果. 您可以通过文本字符串(例如,“伊利诺斯州芝加哥市”或“澳大利亚新南威尔士州达尔文市”)或 LatLng 值的形式来指定路线的起点和终点.路线服务可以使用一系列路标返回多段路线.路线可以显示为一条在地图上绘制路线的折线

Codeforces 327E Axis Walking (状压dp lowbit优化)

E. Axis Walking time limit per test:3 seconds memory limit per test:512 megabytes Iahub wants to meet his girlfriend Iahubina. They both live in Ox axis (the horizontal axis). Iahub lives at point 0 and Iahubina at point d. Iahub has n positive integ

CodeForces D. Walking Between Houses

http://codeforces.com/contest/1015/problem/D There are nn houses in a row. They are numbered from 11 to nn in order from left to right. Initially you are in the house 11. You have to perform kk moves to other house. In one move you go from your curre

Diagonal Walking v.2 CodeForces - 1036B (思维,贪心)

Diagonal Walking v.2 CodeForces - 1036B Mikhail walks on a Cartesian plane. He starts at the point (0,0)(0,0), and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point (0,0)(0,0), he can go to any o

Codeforces Round #482 (Div. 2)D. Kuro and GCD and XOR and SUM+字典树

题目链接:D. Kuro and GCD and XOR and SUM 题意:两种操作:第一种给数组添加一个数,第二种输入x,k,s,要求从数组中找到一个数v,要求k能整除gcd(k,v);并且v<=s-x,然后异或v与k的异或值最大. 题解:对与k大于1的情况我们暴力枚举过去,k为1的特殊处理建一颗字典树,如果可以的满足条件的话,每次取值时往相反方向取. 1 #include<bits/stdc++.h> 2 #include <iostream> 3 #include