当前位置:网站首页>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;
}
边栏推荐
- Yilian: Activating the Value Potential of Data Elements and Unleashing the Innovation Dividend of SAS SSD
- 数据中台建设(五):打破企业数据孤岛和提取数据价值
- 权威推荐!腾讯安全DDoS边缘安全产品获国际研究机构Omdia认可
- 学习笔记——七周成为数据分析师《第一周:数据分析思维》
- cpu / CS 和 IP
- libudev manual
- 12、 学习MySQL 排序
- jsArray数组复制方法性能测试2207300823
- 人社部公布“数据库运行管理员”成新职业,OceanBase参与制定职业标准
- 逻辑漏洞----权限类漏洞
猜你喜欢
SQL 26 calculation under 25 years of age or older and the number of users

Logic Vulnerability----Permission Vulnerability

第十五天笔记

权威推荐!腾讯安全DDoS边缘安全产品获国际研究机构Omdia认可

如何判断自己是否适合IT行业?方法很简单

jsArray array copy method performance test 2207300823

pytorch学习记录(六):循环神经网络 RNN & LSTM

学习笔记——七周成为数据分析师《第二周:业务》:业务分析指标

jsArray array copy method performance test 2207300040

MQTT网关读取西门子PLC数据传输到阿里云平台案例教程
随机推荐
Jenkins自动化部署项目
leetcode207.课程表(判断有向图是否有环)
Parallelized Quick Sort Ideas
【ROS进阶篇】第十一讲 基于Gazebo和Rviz的机器人联合仿真(运动控制与传感器)
R语言ggplot2可视化:使用ggpubr包的ggboxplot函数可视化分组箱图、使用ggpar函数改变图形化参数(xlab、ylab、改变可视化图像的坐标轴标签内容)
DeFi 巨头进军 NFT 领域 用户怎么看?
(HR面试)最常见的面试问题和技巧性答复
shell script flow control statement
jsArray array copy method performance test 2207300040
Markdown 3 - 流程图表
666666
[PostgreSQL] - explain SQL analysis introduction
逻辑漏洞----权限类漏洞
缓存
R语言ggplot2可视化:使用ggpubr包的ggboxplot函数可视化箱图、width参数自定义箱图中箱体的宽度
Eleven BUUCTF questions (06)
【VMware虚拟机安装mysql5.7教程】
Smart pointer implementation conjecture
These critical programs are missing or too old: ma
重保特辑|筑牢第一道防线,云防火墙攻防演练最佳实践