bzoj1045 洛谷P4016 洛谷P2512 bzoj3293 洛谷P3156 题解:https://www.luogu.org/blog/LittleRewriter/solution-p2512 #include<cstdio> #include<algorithm> #include<cstring> #include<vector> using namespace std; #define fi first #define se second #…
洛谷P2661 信息传递 最小环求解采用并查集求最小环. 只适用于本题的情况.对于新加可以使得两个子树合并的边,总有其中一点为其中一棵子树的根. 复杂度 \(O(n)\) . #include<bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int maxn = 200005; int f[maxn], dist[maxn]; int n, to, ans; int father(int x) { if…
题目链接 环形均分纸牌. 设平均数为\(ave\),\(g[i]=a[i]-ave\),\(s[i]=\sum_{j=1}^ig[i]\). 设\(s\)的中位数为\(s[k]\),则答案为\(\sum |s[i]-s[k]|\) 博主太菜,无法给出证明. #include <cstdio> #include <algorithm> using namespace std; const int MAXN = 1000010; long long sum, ans, a[MAXN],…
传送门啦 一个人要想知道自己的生日,就意味着信息的传递是成环的,因为每轮信息只能传递一个人,传递的轮数就等于环的大小 环的大小就等于环中的两个点到第三个点的距离之和加一,我们就可以在使用并查集时,维护每个点到某个确定点的距离 不妨令这个确定点为当前点的祖先,在同一个集合中,所有的点拥有共同的祖先,因此可以确定环的大小 有可能有多个环,最小的环就是最小的轮数 #include <bits/stdc++.h> using namespace std; int n,t; int f[200005];…