题意:给定一棵树中,让你计算它的直径,也就是两点间的最大距离. 析:就是一个树上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] !=…
题目链接: 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…
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…
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…
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…
题意:给定 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…
树的直径新求法 讲解题目 今天考了一道题目,下面的思路二是我在考场上原创,好像没人想到这种做法,最原始的题目,考场上的题目是这样的: 你现在有1 个节点,他的标号为1,每次加入一个节点,第i 次加入的节点标号为i+1,且每次加入的节点父亲为已经存在的节点.你需要在每次加入节点后输出当前树的直径. 大意:给你$n$个点,最开始时只有一个点,一号节点,然后每一次把第$i$号节点和编号比他小的节点相连,连接一个点后,求树的直径. 样例输入 第一行一个整数n.接下来n-1 行,第i+1 行一个数表示标号…
题目链接: 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 …
D. The Fair Nut and the Best Path 题意:给出一张图 点有权值 边也要权值 从任意点出发到任意点结束 到每个点的时候都可以获得每个点的权值,而从边走的时候都要消耗改边的边权,如果当前值小于边的边权,就走不通,问从任意点出发到任意点结束的可以获得的最大权多少(其中到一个点结束的时候也能获得改点的值) 思路:一个很明显的树上dp的问题 \(dp[i]\)表示以i为起点的可以获得的最高的权值是多少 \(dp[i]=w[i]+max(son(dp[j]))\) 其中j为i…
题目链接: 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…