当前位置:网站首页>Cf603e pastoral oddities [CDQ divide and conquer, revocable and search set]
Cf603e pastoral oddities [CDQ divide and conquer, revocable and search set]
2022-07-06 03:44:00 【QuantAsk】
On the subject
Topic link :https://www.luogu.com.cn/problem/CF603E
The main idea of the topic
There was... At the beginning n n n A little bit , There is no side .
Add in sequence m m m Edge of strip weight , After each addition, ask whether there is an edge set , The degree satisfying each point is odd , Seek to minimize the maximum weight of this edge set .
1 ≤ n ≤ 1 0 5 , 1 ≤ m ≤ 3 × 1 0 5 1\leq n\leq 10^5,1\leq m\leq 3\times 10^5 1≤n≤105,1≤m≤3×105
Their thinking
First, consider the condition for the existence of this edge set , It can be proved that there is an edge set satisfying the condition if and only if the size of the connected blocks is even .
The need for : For a connection block , Because each edge contributes an even number of degrees , And if this connected block is an odd number of points , Then if the total legal degree is Odd number × Odd number = Odd number , Obviously, it can't be even , So there is no such situation .
adequacy : If there is a point whose degree is odd , Then the degree of at least one point in the Unicom fast is odd , We can adjust to the legal situation by deleting the edge on the path of these two points on the way .
And it is definitely the best if we can connect the edges , Because there is no edge connection, the number of odd connected blocks will increase .
Then consider using CDQ Divide and conquer to solve this problem , Notice that the answer must be monotonous , Our process is to record the current interval [ l , r ] [l,r] [l,r] Answer range of { L , R } \{L,R\} { L,R}.
First calculate a n s m i d ans_{mid} ansmid, Then at this time we can be divided into [ l , m i d − 1 ] { a n s m i d , R } [l,mid-1]\{ans_{mid},R\} [l,mid−1]{ ansmid,R} and [ m i d + 1 , r ] { L , a n s m i d } [mid+1,r]\{L,ans_{mid}\} [mid+1,r]{ L,ansmid}
At this time, the two intervals are separated , This suggests that the violent enumeration of these intervals is the complexity of normal partition .
Then it's obvious , We calculated a n s m i d ans_{mid} ansmid After the left and right sides of recursive processing , With revocable and search set processing .
Time complexity : O ( m log m log n ) O(m\log m\log n) O(mlogmlogn)
code
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=3e5+10;
struct node{
int x,y,w,id;
}a[N],b[N];
struct clnode{
int x,y,siz,dep;
}cl[N];
int n,m,sum,clt,fa[N],siz[N],dep[N];
int ans[N],rk[N];
int find(int x)
{
return (fa[x]==x)?x:find(fa[x]);}
void unionn(int x,int y){
x=find(x);y=find(y);
if(x==y)return;
if(dep[x]>dep[y])swap(x,y);
cl[++clt]=(clnode){
x,y,siz[y],dep[y]};
sum-=(siz[x]&1)&(siz[y]&1);
fa[x]=y;siz[y]+=siz[x];
dep[y]=max(dep[y],dep[x]+1);
}
void clearto(int d){
while(clt>d){
int x=cl[clt].x,y=cl[clt].y;
siz[y]=cl[clt].siz;
dep[y]=cl[clt].dep;
sum+=(siz[x]&1)&(siz[y]&1);
fa[x]=x;clt--;
}
return;
}
void cdq(int l,int r,int L,int R){
if(l>r)return;
int mid=(l+r)>>1,now=clt;
for(int i=l;i<=mid;i++)
if(rk[i]<L)unionn(a[i].x,a[i].y);
int mow=clt;
for(int i=L;i<=R;i++){
if(b[i].id<=mid)unionn(b[i].x,b[i].y);
if(!sum){
ans[mid]=i;break;}
}
if(!ans[mid]){
clearto(mow);
cdq(mid+1,r,L,R);
return;
}
clearto(mow);
cdq(mid+1,r,L,ans[mid]);
clearto(now);
for(int i=L;i<ans[mid];i++)
if(b[i].id<l)unionn(b[i].x,b[i].y);
cdq(l,mid-1,ans[mid],R);
clearto(now);return;
}
bool cmp(node x,node y)
{
return x.w<y.w;}
int main()
{
scanf("%d%d",&n,&m);sum=n/2;
if(n&1){
for(int i=1;i<=m;i++)
puts("-1");
return 0;
}
for(int i=1;i<=n;i++)fa[i]=i,siz[i]=1;
for(int i=1;i<=m;i++){
scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].w);
a[i].id=i;b[i]=a[i];
}
sort(b+1,b+1+m,cmp);
for(int i=1;i<=m;i++)rk[b[i].id]=i;
cdq(1,m,1,m);
for(int i=1;i<=m;i++)
if(!ans[i])puts("-1");
else printf("%d\n",b[ans[i]].w);
return 0;
}
边栏推荐
- KS008基于SSM的新闻发布系统
- [slam] orb-slam3 parsing - track () (3)
- 教你用Pytorch搭建一个自己的简单的BP神经网络( 以iris数据集为例 )
- SAP ALV color code corresponding color (finishing)
- 潘多拉 IOT 开发板学习(HAL 库)—— 实验9 PWM输出实验(学习笔记)
- 2.1 rtthread pin device details
- Record the process of reverse task manager
- JVM的手术刀式剖析——一文带你窥探JVM的秘密
- Image super resolution using deep revolutionary networks (srcnn) interpretation and Implementation
- Pointer written test questions ~ approaching Dachang
猜你喜欢
How to standardize the deployment of automated testing?
Flask learning and project practice 9: WTF form verification
An article will give you a comprehensive understanding of the internal and external components of "computer"
[slam] orb-slam3 parsing - track () (3)
Cubemx transplantation punctual atom LCD display routine
阿里测试师用UI自动化测试实现元素定位
Idea push rejected solution
如何修改表中的字段约束条件(类型,default, null等)
KS008基于SSM的新闻发布系统
Align items and align content in flex layout
随机推荐
JVM的手术刀式剖析——一文带你窥探JVM的秘密
BUAA calculator (expression calculation - expression tree implementation)
施努卡:视觉定位系统 视觉定位系统的工作原理
1.16 - 校验码
Mysqldump data backup
C language judgment, ternary operation and switch statement usage
Mapping between QoE and KQI
Cubemx transplantation punctual atom LCD display routine
[prediction model] difference method model
在 .NET 6 中使用 Startup.cs 更简洁的方法
潘多拉 IOT 开发板学习(HAL 库)—— 实验9 PWM输出实验(学习笔记)
3.2 rtthread 串口设备(V2)详解
Quick sort function in C language -- qsort
Multi project programming minimalist use case
3.1 detailed explanation of rtthread serial port device (V1)
1、工程新建
Blue style mall website footer code
BUAA计算器(表达式计算-表达式树实现)
11. Container with the most water
SWC introduction