Codeforces 519 E. A and B and Lecture Rooms】的更多相关文章

Description 询问一个树上与两点距离相等的点的个数. Sol 倍增求LCA. 一棵树上距离两点相等,要么就只有两点的中点,要么就是与中点相连的所有点. 有些结论很容易证明,如果距离是偶数,那么他们没有中点,树上不存在距离两点相等的点. 如果中点恰好是两点LCA,那么答案就是\(n-size_x-size_y\) ,\(size_x\) 和 \(size_y\) 表示LCA的子节点中子树包含 \(u,v\) 的子节点的\(size\) 如果不是LCA,那么答案就是 \(size_{LCA…
题目:http://codeforces.com/problemset/problem/519/E 题意:给你一个n个点的树,有m个询问(x,y),对于每个询问回答树上有多少个点和x,y点的距离相等 分析:对于x,y,容易知道距离相等的点是链x->y上的中点除去x.y所在子树的其他所有子树的和 于是分类: 链x->y的长度是奇数:则一定没有距离相等的点,输出0 链x->y的长度是偶数: 链上的中点Mid恰好是x,y的lca,则输出n-size[lca(x,y)的某个儿子(这个儿子是x的父…
转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud E. A and B and Lecture Rooms A and B are preparing themselves for programming contests. The University where A and B study is a set of rooms connected by corridors. Overall, the University ha…
A and B and Lecture Rooms time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output A and B are preparing themselves for programming contests. The University where A and B study is a set of rooms co…
题目链接:E. A and B and Lecture Rooms 题目大意 给定一颗节点数10^5的树,有10^5个询问,每次询问树上到xi, yi这两个点距离相等的点有多少个. 题目分析 若 x==y 直接返回 n. 先求出 x, y 两个点的中点. 先求出 LCA(x, y) = z,假设 Depth[x] >= Depth[y] ,若Depth[x] == Depth[y] ,那么 z 就是它们的中点. 答案就是,n - Size[fx] - Size[fy],fx 是从x向上跳,一直跳…
http://codeforces.com/contest/519/problem/E 题意: 给出一棵树和m次询问,每次询问给出两个点,求出到这两个点距离相等的点的个数. 思路: lca...然后直接判就好了,挂dp标签的人是什么心态.. #include<cstdio> #include<cmath> #include<algorithm> #include<cstring> #include<iostream> ],first[],next…
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 519E Description A and B are preparing themselves for programming contests. The University where A and B study is a set of rooms connected…
题意: 给你一棵有n个节点的树,给你m次询问,查询给两个点,问树上有多少个点到这两个点的距离是相等的.树上所有边的边权是1. 思路: 很容易想到通过记录dep和找到lca来找到两个点之间的距离,然后分情况讨论. 一开始困扰我的问题是如果lca不是正中间的点,如何在比较低的复杂度的层面上求解中点. 倍增法lca不光可以在logn的时间复杂度内查询某两个点的lca,还可以实现在logm的时间复杂度能查询某个节点的第m个父亲节点. 算法的核心是用二进制的运算来实现查询. #include<bits/s…
A and B are preparing themselves for programming contests. The University where A and B study is a set of rooms connected by corridors. Overall, the University has n rooms connected by n - 1corridors so that you can get from any room to any other one…
给出一棵树,有若干次询问,每次询问距两个点u, v距离相等的点的个数. 情况还挺多的,少侠不妨去看官方题解.^_^ #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> using namespace std; + ; int n, Q; vector<int> G[maxn]; int L[max…
传送门:>Here< 题意:询问给出一棵无根树上任意两点$a,b$,求关于所有点$i$,$dist(a,i) = dist(b,i)$的点的数量.要求每一次询问在$O(log n)$的时间复杂度内完成. 解题思路 由于在树上求距离,并且还要$O(log n)$,自然会联想到$LCA$.由于边权是$1$,那么点到根的距离就是该点的深度.这个深度可以在$dfs$预处理的过程中处理完成.那么两个点之间的距离就是两个点到根节点的距离减去两点的LCA到根节点距离的两倍.这个随便yy一下就好了. 得到$a…
最近很颓……难题想不动……水题写不对,NOIP怕是…
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 最近公共祖先. (树上倍增 一开始统计出每个子树的节点个数_size[i] 如果x和y相同. 那么直接输出n. 否则求出x和y的最近公共祖先.z (假定y的深度大于x [1]如果z等于x或y中的一个. 那么久就找到x..y的路径(长度设为L)中的中点u. 显然,u和它的其他len-1个子树上的任意一个节点都是可行的(除了那个包含y的子树 设_get(x,step)表示x节点往上走step步到达的节点 则输出_sum[中点]-_s…
题目描述 A and B are preparing themselves for programming contests. The University where A and B study is a set of rooms connected by corridors. Overall, the University has n rooms connected by n - 1 corridors so that you can get from any room to any oth…
http://codeforces.com/problemset/problem/499/B B. Lecture     You have a new professor of graph theory and he speaks very quickly. You come up with the following plan to keep up with his lecture and make notes. You know two languages, and the profess…
传送门:A and B and Lecture Rooms 题意:给定一棵树,每次询问到达点u,v距离相等的点有多少个. 分析:按情况考虑: 1.abs(deep[u]-deep[v])%2==1时,必定不存在到达u,v距离相等的点. 2.如果deep[u]==deep[v]时,ans=n-num[lca(u,v)u在的儿子树]-num[lca(u,v)v在的儿子树]. 3.如果deep[u]!=deep[v]时,在u到v的路径中找出到达u,v相等的节点x,则ans=num[x]-num[u在的…
1. CF 438D The Child and Sequence 大意: n元素序列, m个操作: 1,询问区间和. 2,区间对m取模. 3,单点修改 维护最大值, 取模时暴力对所有>m的数取模. 因为取模后至少减半, 复杂度$O(nlognlogC)$ 2. CF 431E Chemistry Experiment 大意: n个试管, 第$i$个试管有$a_i$单位水银, m个操作: 1, 修改$a_x$改为$v$. 2, 将$v$单位水倒入试管, 求一种方案使得有水的试管水银与水总量的最大…
录音文件 https://pan.baidu.com/s/1qYYZGWO The process of learning and exploring a subject can lead to a whole new way of looking at the world. The following is a summary of costs and special inclusive offers on holidays for the coming summer. A wave of b…
##Advice for Applying Machine Learning Applying machine learning in practice is not always straightforward. In this module, we share best practices for applying machine learning in practice, and discuss the best ways to evaluate performance of the le…
题目链接:http://codeforces.com/problemset/problem/499/B 题目意思:给出两种语言下 m 个单词表(word1, word2)的一一对应,以及 professor's lecture 的 n 个单词.问记下来的笔记是什么.对于professor's lecture 的某个单词,如果在单词表中找到,word1, word2 都有可能.如果 word1 的长度  <= word2 的长度,就输出word1,否则word2 考了map<string, st…
本文为博主原创文章,未经允许不得转载. 我在csdn也同步发布了此文,链接 https://blog.csdn.net/umbrellalalalala/article/details/79891969   题目来源 http://codeforces.com/problemset/problem/961/B [题目] Your friend Mishka and you attend a calculus lecture. Lecture lasts n minutes. Lecturer t…
Codeforces Round #519 by Botan Investments #include<bits/stdc++.h> #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> #include<queue> #include<vect…
开个新号打打codeforces(以前那号玩废了),结果就遇到了这么难一套.touristD题用了map,被卡掉了(其实是对cf的评测机过分自信),G题没过, 700多行代码,码力惊人.关键是这次tourist掉到第二了,掉了200多分,为神节哀. 做了4道,要不是第4题一直炸第5题也能做出来.本来想着上蓝名的.然后我第二题挂了,判断循环节写错了.绝望啊---- 比赛传送门:http://codeforces.com/contest/1043 A. Elections 这题很简单,就是说有n个人…
The Maths Lecture 题意:求存在后缀Si mod k =0,的n位数的数目.(n <=1000,k<=100); 用f[i][j]代表 长为i位,模k等于j的数的个数. 可以用 f[i+1][(t*10i+j)%k]=∑f[i][j]+(j==0),(t*10i+j)%k!=0;动态规划 这样可以求出所有f[n][i] i>0 的值. 最后用9*10^(n-1)-∑f[n][i] 就可以得到 答案 #include <bits/stdc++.h> using…
https://codeforces.com/contest/1043/problem/F 题意 给你n个数,求一个最小集合,这个集合里面数的最大公因数等于1 1<=n<=3e5 1<=a[i]<=3e5 思路 先考虑什么情况下满足集合中的最大公因数=1? 集合中的每个数没有共同的素因子,即所有素因子并没有包含于选出集合的所有数中,存在结论前7个素因子的乘积为510510,所以可以得出选出的集合大小最大为7 定义dp[i][j]为,集合大小为i,集合最大公因数=j的方案数 dp[i…
A. Elections 题意概述 给出 \(a_1, \ldots, a_n\),求最小的 \(k (k \ge \max a_i)\), 使得 \(\sum_{i=1}^n a_i < \sum_{i=1}^n (k-a_i)\) 原题链接 解题思路 数据范围小,直接枚举就好了 代码 #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); int n; cin &g…
传送门 D. The Maths Lecture time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Amr doesn't like Maths as he finds it really boring, so he usually sleeps in Maths lectures. But one day the teacher…
[题目链接]:http://codeforces.com/problemset/problem/507/D [题意] 让你找符合这样数字的数的个数: 1.有n个数码 2.某个后缀%k的值为0 3.大于0 [题解] 数位DP; 设f[i][j][0]和f[i][j][1]分别表示; (把最后的数字倒过来) 前i个数字组成的数%k的结果为j; 且前i-1个数字没有出现过%k结果为0; 前i-1个数字有出现过%k结果为0的方案数; 枚举新添加的一位; 看看新的取余值是啥; 然后想一想转移方程就好; d…
题目链接:传送门   A. Elections (思维+暴力) 思路: 从最小的k开始枚举就好了- -. #include <bits/stdc++.h> using namespace std; + ; int a[MAX_N]; int main() { int N; cin >> N; , sum = ; ; i <= N; i++) { scanf("%d", a+i); m = max(m, a[i]); sum += a[i]; } int a…
A:枚举答案即可.注意答案最大可达201,因为这个wa了一发瞬间爆炸. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; int read() { ,f=;char c=getchar(); ;c=getchar();} )+(x…