当前位置:网站首页>CF603E Pastoral Oddities
CF603E Pastoral Oddities
2022-07-30 13:41:00 【With a cool moon】
CF603E Pastoral Oddities
There exists a set of edges such that every point has an odd degree,A sufficient condition is that the number of points of connected blocks is even.
Consider necessity first,Add side degrees and increase 2 2 2 So the sum of degrees is even,And when the number of connected blocks that satisfy the condition is odd,The sum of degrees must be odd,矛盾.
证明充分性:for an even-sized connected block,Find one of its spanning trees,Starting from the leaf, consider whether to add it to the father's edge according to the degree to the son,Gradually satisfy,Finally, all conditions are met except the root,Obviously the condition is also satisfied according to the parity root,So sufficiency is guaranteed.
Therefore, it is possible to maintain whether the connected block satisfies the condition by merging and checking the set by rank.
Then consider the restriction that the largest edge weight in the edge set is the smallest,A minimum spanning tree is sufficient.
在线 LCT Maintain a minimum spanning tree forest,时间复杂度 O ( n log n ) \mathcal O(n\log n) O(nlogn).
But it's very hard to write and the constants are huge,考虑离线做法.
Observe the samples,时间升序,The answer is monotonically decreasing;还有“最大值最小”一类的词,考虑二分.
多次询问,考虑整体二分.
具体地,Discretize weights,Then divide and conquer the weights,令其为 m i d mid mid.
Add weights ≤ m i d \leq mid ≤mid side until legal,Divide and conquer according to the first legal position.
注意细节,The state behind the overall bisection ( l , r , v l , v r ) (l,r,vl,vr) (l,r,vl,vr) All numbers are required to be included in the merge set ≤ l \leq l ≤l 且权值 ≤ v l \leq vl ≤vl 的边,Consider maintaining an undoable and lookup set.
时间复杂度 O ( m log m log n ) \mathcal O(m\log m\log n) O(mlogmlogn).
#include<cstdio>
#include<algorithm>
typedef long long ll;
#define ha putchar(' ')
#define he putchar('\n')
inline int read() {
int x = 0;
char c = getchar();
while (c < '0' || c > '9')
c = getchar();
while (c >= '0' && c <= '9')
x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
return x;
}
inline void write(int x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9)
write(x / 10);
putchar(x % 10 + 48);
}
const int _ = 3e5 + 5;
int n, m, cnt, fa[_], sz[_], t, s[_], ans[_];
struct abc {
int u, v, w, id;
bool operator < (const abc &t) {
return w < t.w;
}
} e[_], b[_];
int find(int x) {
return fa[x] == x ? x : find(fa[x]);
}
void merge(int u, int v) {
u = find(u), v = find(v);
if (u == v) return;
if (sz[u] < sz[v]) std::swap(u, v);
if ((sz[u] & 1) && (sz[v] & 1)) cnt -= 2;
sz[u] += sz[v], fa[v] = u, s[++t] = v;
}
void ers(int lim) {
while (t > lim) {
int v = s[t--], u = fa[v];
fa[v] = v, sz[u] -= sz[v];
if ((sz[u] & 1) && (sz[v] & 1)) cnt += 2;
}
}
void solve(int l, int r, int vl, int vr) {
if (l > r || vl > vr) return;
if (vl == vr) {
for (int i = l; i <= r; ++i) ans[i] = b[vl].w;
return;
}
int mid = (vl + vr) >> 1, nw = t;
for (int i = vl; i <= mid; ++i)
if (b[i].id < l) merge(b[i].u, b[i].v);
int Id = r + 1, nw2 = t;
for (int i = l; i <= r; ++i) {
if (e[i].w <= b[mid].w) merge(e[i].u, e[i].v);
if (!cnt) {
Id = i;
break;
}
}
ers(nw2);
solve(l, Id - 1, mid + 1, vr);
ers(nw);
for (int i = l; i < Id; ++i)
if (e[i].w <= b[vl].w) merge(e[i].u, e[i].v);
solve(Id, r, vl, mid);
ers(nw);
}
signed main() {
n = cnt = read(), m = read();
for (int i = 1; i <= n; ++i) fa[i] = i, sz[i] = 1;
for (int i = 1; i <= m; ++i) {
e[i].u = read(), e[i].v = read(), e[i].w = read();
e[i].id = i, b[i] = e[i];
}
std::sort(b + 1, b + m + 1);
b[m + 1].w = -1;
solve(1, m, 1, m + 1);
for (int i = 1; i <= m; ++i) write(ans[i]), he;
}
边栏推荐
- 第十三天笔记
- ENVI Image Processing (6): NDVI and Vegetation Index
- Jenkins自动化部署项目
- 打破原则引入SQL,MongoDB到底想要干啥???
- These critical programs are missing or too old: ma
- Eleven BUUCTF questions (06)
- How to solve the problem that the page does not display the channel configuration after the EasyNVR is updated to (V5.3.0)?
- [C# 循环跳转]-C# 中的 while/do-while/for/foreach 循环结构以及 break/continue 跳转语句
- qq udp tcp机制
- Scala基础:数组(Array)、映射(Map)、元组(Tuple)、集合(List)
猜你喜欢
随机推荐
无代码开发平台应用可见权限设置入门教程
自动化测试的生命周期是什么?
TaskDispatcher source code parsing
grep时排除指定的文件和目录
jsArray array copy method performance test 2207300823
for循环的3个表达式执行顺序
Xms Xmx PermSize MaxPermSize 区别
群晖系统安装相关文件分享
leetcode207.课程表(判断有向图是否有环)
CF338E Optimize!
Learning notes - 7 weeks as data analyst "in the first week: data analysis of thinking"
ES6 Set与Map是什么,如何使用
创意loadingjs特效小点跳跃动画
Composer安装方式
缓存
学习笔记——七周成为数据分析师《第二周:业务》:业务分析指标
C语言学习练习题:汉诺塔(函数与递归)
How to display an Excel table in the body of an email?
外包干了七年,废了。。。
程序员修炼之道:务以己任,实则明心——通向务实的最高境界









