Codeforces 690 C3. Brain Network (hard) LCA】的更多相关文章

C3. Brain Network (hard)   Breaking news from zombie neurology! It turns out that – contrary to previous beliefs – every zombie is born with a single brain, and only later it evolves into a complicated brain structure. In fact, whenever a zombie cons…
题目链接: C3. Brain Network (hard) time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Breaking news from zombie neurology! It turns out that – contrary to previous beliefs – every zombie is born …
题目链接: C2. Brain Network (medium) time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Further research on zombie thought processes yielded interesting results. As we know from the previous prob…
题目链接: C1. Brain Network (easy) time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output One particularly well-known fact about zombies is that they move and think terribly slowly. While we still do…
题目描述 Brain Network (hard) 这个问题就是给出一个不断加边的树,保证每一次加边之后都只有一个连通块(每一次连的点都是之前出现过的),问每一次加边之后树的直径. 算法 每一次增加一条边之后,树的直径长度要么不变,要么会增加1,并且如果树的直径长度增加1了,新的直径的端点其中一个必然是新增的点,而另一个是原来直径的某个端点.关于为什么可以这样做,在Quora上有个回答解释地不错,可以参考. 实现 所以这个问题其实就是要计算树上任意两个点的距离,LCA可以很轻松地处理. 可以一次…
H - Brain Network (medium) Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u CodeForces 690C2 Description Further research on zombie thought processes yielded interesting results. As we know from the previous problem, the…
G - Brain Network (easy) Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u CodeForces 690C1 Description One particularly well-known fact about zombies is that they move and think terribly slowly. While we still don't know…
Brain Network (medium) Further research on zombie thought processes yielded interesting results. As we know from the previous problem, the nervous system of a zombie consists of n brains and m brain connectors joining some pairs of brains together. I…
Brain Network (easy) One particularly well-known fact about zombies is that they move and think terribly slowly. While we still don't know why their movements are so sluggish, the problem of laggy thinking has been recently resolved. It turns out tha…
poj 3417 Network(tarjan lca) 先给出一棵无根树,然后下面再给出m条边,把这m条边连上,然后每次你能毁掉两条边,规定一条是树边,一条是新边,问有多少种方案能使树断裂. 我们设添加了一条新边后,树形成了一个环,表示为x->y->lca(x,y),我们将其中的边都覆盖一次.添加了多条新边后,可知树上有些边是会被多次覆盖的,画图很容易发现,但一个树边被覆盖了2次或以上,它就是一条牢固的边,就是说毁掉它再毁掉任何一条新边都好,树都不会断裂.所以我们只要统计被覆盖过零次或一次的…
simple:并查集一下 #include <vector> #include <iostream> #include <queue> #include <cmath> #include <map> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; typedef long long LL; ; ; in…
题意: 就是有几个点,你掌控了几条路,你的商业对手也掌控了几条路,然后你想让游客都把你的所有路都走完,那么你就有钱了,但你又想挣的钱最多,真是的过分..哈哈 游客肯定要对比一下你的对手的路 看看那个便宜 就走哪个,(你的路的价钱和对手相等时 优先走你的): 思路想到了 但写不出来...真的有点巧妙了 用并查集来记录环路  如果两个点不能加入到并查集,那么肯定是加入这两个点后就构成了一个环路  记录下构成环路的u和v两点 其它点加入并查集 并加入到邻接表 dfs走一遍 记录下每个点的父结点 和 每…
树的直径新求法 讲解题目 今天考了一道题目,下面的思路二是我在考场上原创,好像没人想到这种做法,最原始的题目,考场上的题目是这样的: 你现在有1 个节点,他的标号为1,每次加入一个节点,第i 次加入的节点标号为i+1,且每次加入的节点父亲为已经存在的节点.你需要在每次加入节点后输出当前树的直径. 大意:给你$n$个点,最开始时只有一个点,一号节点,然后每一次把第$i$号节点和编号比他小的节点相连,连接一个点后,求树的直径. 样例输入 第一行一个整数n.接下来n-1 行,第i+1 行一个数表示标号…
Code: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 200000 + 4; const int logn = 19; int F[30][maxn], dep[maxn]; inline int lca(int a,int b) { if(dep[a] > dep[b]) swap(a,b); if(dep[a] !=…
题意:给定一棵树中,让你计算它的直径,也就是两点间的最大距离. 析:就是一个树上DP,用两次BFS或都一次DFS就可以搞定.但两次的时间是一样的. 代码如下: #include<bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; vector<int> G[maxn]; int f[maxn], g[maxn], l[maxn]; int dfs(int root, int fa){ if(f[root] !=…
题意:给定 n 条边,判断是不是树. 析:水题,判断是不是树,首先是有没有环,这个可以用并查集来判断,然后就是边数等于顶点数减1. 代码如下: #include <bits/stdc++.h> using namespace std; const int maxn =1000 + 5; int p[maxn]; int Find(int x){ return x == p[x] ? x : p[x] = Find(p[x]); } int main(){ int n, m, x, y; cin…
Organization, development and function of complex brain networks The Brain as a Complex System: Using Network Science as a Tool for Understanding the Brain Rubinov M, Sporns O. Complex network measures of brain connectivity: uses and interpretations.…
3732: Network 题目:传送门 题解: 第一眼就看到最大边最小,直接一波最小生成树. 一开始还担心会错,问了一波肉大佬,任意两点在最小生成树上的路径最大边一定是最小的. 那么事情就变得简单起来了嘿嘿嘿,建棵树,直接在线LCA啊,用一个mx[i][j]记录i往上2^j这段区间的最大值. 代码: #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include&l…
题目链接: http://codeforces.com/gym/101161/attachments 题意: 给出节点数为$n$的树 有$q$次询问,输出$a$节点到$b$节点路程中,经过的边的中位数 数据范围: $1\leq n \leq 100000$ $1\leq q \leq 100000$ 分析: 建一颗主席树,不同的是,节点的根继承的是父节点的根 再用$lca$求出公共祖先$tree[a]+tree[b]-2*tree[lca(a,b)]$ ac代码: #include<bits/s…
Network Description Yixght is a manager of the company called SzqNetwork(SN). Now she's very worried because she has just received a bad news which denotes that DxtNetwork(DN), the SN's business rival, intents to attack the network of SN. More unfort…
题:https://codeforces.com/contest/1304/problem/E 题意:给定一颗树,边权为1,m次询问,每次询问给定x,y,a,b,k,问能否在原树上添加x到y的边,a到b的路径长度等于k,注意这里的点和边都是可以重复走的: 分析:注意到点边可以重复走,我们就可以推出一个条件就是a.b之间的长度len要满足>=k: 其次当路长大于k时,我们就要判断len和k是否同奇偶,因为要到达长度len要被分为:len=k+2*i(i>=0),否则无法构造出这么一条路径 然后添…
http://acm.hdu.edu.cn/showproblem.php?pid=3078 题意:给出n个点n-1条边m个询问,每个点有个权值,询问中有k,u,v,当k = 0的情况是将u的权值修改成v,当k不为0的情况是问u和v的路径中权值第k大的点的权值是多少. 思路:比较暴力的方法,可能数据太水勉强混过去了.对于每一个询问的时候保留两个点之间的lca,还有计算出两个点之间的点的个数(询问的时候如果点的个数小于k就不用算了),然后tarjan算完之后对每个询问再暴力路径上的每个点放进vec…
Cellular Network 题意: 给n个城市,m个加油站,要让m个加油站都覆盖n个城市,求最小的加油范围r是多少. 题解: 枚举每个城市,二分查找最近的加油站,每次更新答案即可,注意二分的时候不要越界oil数组,上下界都不要越.还有,int坑死人,以后绝对全用long long!!! 代码: #include <bits/stdc++.h> using namespace std; typedef long long ll; const int INF=0x3f3f3f3f; cons…
City Driving 题目连接: http://codeforces.com/gym/100015/attachments Description You recently started frequenting San Francisco in your free time and realized that driving in the city is a huge pain. There are only N locations in the city that interest yo…
题目链接:http://poj.org/problem?id=3694 题意:给一张图,每次加一条边,问割边数量. tarjan先找出所有割边,并且记录每个点的父亲和来自于哪一条边,然后询问的时候从两个点向上找lca,沿途更新割边数量和割边状态即可. AC代码 /* ━━━━━┒ギリギリ♂ eye! ┓┏┓┏┓┃キリキリ♂ mind! ┛┗┛┗┛┃\○/ ┓┏┓┏┓┃ / ┛┗┛┗┛┃ノ) ┓┏┓┏┓┃ ┛┗┛┗┛┃ ┓┏┓┏┓┃ ┛┗┛┗┛┃ ┓┏┓┏┓┃ ┛┗┛┗┛┃ ┓┏┓┏┓┃ ┃┃┃┃…
题目链接:http://poj.org/problem?id=3694 题意是给你一个无向图n个点,m条边,将m条边连接起来之后形成一个图,有Q个询问,问将u和v连接起来后图中还有多少个桥. 首先用tarjan标记点的low和dfn值,那么u和v相连的边是桥的条件是dfn[u] < low[v](说明v与u不在一个连通分量里面,v无法通过回溯到达u点,画个图模拟会清楚).那么bridge[v]++表示u与v相连的边是桥(若是标记bridge[u]++,则最后的答案可能会出错,亲测).要是u和v相…
C. Cellular Network time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output You are given n points on the straight line — the positions (x-coordinates) of the cities and m points on the same line…
Misha and Grisha are funny boys, so they like to use new underground. The underground has n stations connected with n - 1 routes so that each route connects two stations, and it is possible to reach every station from any other. The boys decided to h…
题目链接:http://poj.org/problem?id=3694 题意:一个无向图中本来有若干条桥,有Q个操作,每次加一条边(u,v),每次操作后输出桥的数目. 分析:通常的做法是:先求出该无向图的桥的数目count和边双连通分量,缩点,每次加边(u,v),判断若u,v属于同一个双连通分量,则桥的数目不变,否则,桥的数目必定会减少,这时桥减少的数目明显和最近公共祖先lca有关,用裸的lca就行了,每次u和v向父节点回退,如果该节点是桥的端点,则count--,直到u==v为止. 有个优化:…
题目链接: A. Brain's Photos 题意: 问是黑白还是彩色; 思路: 没有思路: AC代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <bits/stdc++.h> #include <stack> #include <map> u…