2021.11.14 CF1583E Moment of Bloom(LCA+图上构造)
2021.11.14 CF1583E Moment of Bloom(LCA+图上构造)
https://www.luogu.com.cn/problem/CF1583E
题意:
She does her utmost to flawlessly carry out a person's last rites and preserve the world's balance of yin and yang.
Hu Tao, being the little prankster she is, has tried to scare you with this graph problem! You are given a connected undirected graph of n nodes with m edges. You also have q queries. Each query consists of two nodes a and b .
Initially, all edges in the graph have a weight of 00 . For each query, you must choose a simple path starting from a and ending at b . Then you add 1 to every edge along this path. Determine if it's possible, after processing all qq queries, for all edges in this graph to have an even weight. If so, output the choice of paths for each query.
If it is not possible, determine the smallest number of extra queries you could add to make it possible. It can be shown that this number will not exceed \(10^{18}\) under the given constraints.
A simple path is defined as any path that does not visit a node more than once.
An edge is said to have an even weight if its value is divisible by 2 .
分析:
https://www.luogu.com.cn/blog/dream-of-Au/solution-cf1583e
如果一条边的边权是偶数,那么这条边的边权对两端端点点权的贡献也是偶数。如果这条边的边权是奇数,那么这条边的边权对两端端点的点权的贡献也是奇数。一条路径两个端点的点权每增加1,它对路径上其他端点点权的贡献为2,毕竟一个端点连着两条边。图上最终每出现两个点权为奇数的点,想要把点权变为偶数,只需要增加1条边。根据题上要求,奇数点的个数一定为偶数。
代码如下:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int N=3e5+10;
int n,m,q,fa[N][20],cnt,head[N],dep[N],val[N],belong[N],stacki[N];
struct node{
int to,next;
}a[N];
struct queryi{
int x,y;
}query[N];
struct mapii{
int u,v;
}mapi[N];
inline void add(int u,int v){
++cnt;
a[cnt].to=v;
a[cnt].next=head[u];
head[u]=cnt;
}
inline void dfs(int x,int fai){
dep[x]=dep[fai]+1;
fa[x][0]=fai;
for(int i=head[x];i;i=a[i].next){
int v=a[i].to;
if(dep[v])continue;
dfs(v,x);
}
}
inline int lca(int x,int y){
if(dep[x]<dep[y])swap(x,y);
for(int i=19;i>=0;i--)if(fa[x][i]&&dep[fa[x][i]]>=dep[y])x=fa[x][i];
if(x==y)return x;
for(int i=19;i>=0;i--)if(fa[x][i]!=fa[y][i])x=fa[x][i],y=fa[y][i];
return fa[x][0];
}
inline int find(int x){
return belong[x]==x?x:belong[x]=find(belong[x]);
}
inline void print(int x,int y,int lcai){
int now=x;
while(now!=lcai)cout<<now<<" ",now=fa[now][0];
cout<<lcai<<" ";
now=y;
int top=0;
while(now!=lcai)stacki[++top]=now,now=fa[now][0];
for(int i=top;i>=1;i--)cout<<stacki[i]<<" ";cout<<endl;
}
int main(){
cin>>n>>m;
for(int i=1;i<=m;i++)cin>>mapi[i].u>>mapi[i].v;
cin>>q;
for(int i=1;i<=q;i++){
cin>>query[i].x>>query[i].y;
++val[query[i].x];++val[query[i].y];
}
int ans=0;
for(int i=1;i<=n;i++)if(val[i]%2)++ans;
if(ans)return cout<<"NO"<<endl<<ans/2,0;
cout<<"YES"<<endl;
for(int i=1;i<=n;i++)belong[i]=i;
for(int i=1;i<=m;i++){
int ui=find(mapi[i].u),vi=find(mapi[i].v);
if(ui==vi)continue;
belong[ui]=vi;
add(mapi[i].u,mapi[i].v);add(mapi[i].v,mapi[i].u);
//cout<<mapi[i].u<<" "<<mapi[i].v<<endl;//
}
//cout<<" Case 1"<<endl;
dfs(1,0);
//cout<<" Case 2"<<endl;
for(int i=1;i<=19;i++)for(int j=1;j<=n;j++)fa[j][i]=fa[fa[j][i-1]][i-1];
for(int i=1;i<=q;i++){
int x=query[i].x,y=query[i].y;
int lcai=lca(x,y);
//cout<<x<<" "<<y<<" "<<lcai<<endl;
cout<<dep[x]+dep[y]-2*dep[lcai]+1<<endl;
print(x,y,lcai);
}
return 0;
}
2021.11.14 CF1583E Moment of Bloom(LCA+图上构造)的更多相关文章
- 日常Java测试 2021/11/14
课堂测试三 package word_show; import java.io.*;import java.util.*;import java.util.Map.Entry; public clas ...
- 2021.9.14考试总结[NOIP模拟53]
T1 ZYB和售货机 容易发现把每个物品都买成$1$是没有影响的. 然后考虑最后一个物品的方案,如果从$f_i$向$i$连边,发现每个点有一个出度多个入度,可以先默认每个物品都能买且最大获利,这样可以 ...
- 日常Java 2021/11/18
用idea实现Javaweb登录页面 <%-- Created by IntelliJ IDEA. User: Tefuir Date: 2021/11/18 Time: 18:14 To ch ...
- JavaSE_ API常用对象 总目录(11~14)
JavaSE学习总结第11天_开发工具 & API常用对象111.01 常见开发工具介绍11.02 Eclipse和MyEclipse的概述11.03 Eclipse的下载安装及卸载11.04 ...
- Intel Artificial Intelligence Conference(2018.11.14)
时间:2018.11.14地点:北京国贸大酒店
- 第26次Scrum会议(11/14)【欢迎来怼】
一.小组信息 队名:欢迎来怼小组成员队长:田继平成员:李圆圆,葛美义,王伟东,姜珊,邵朔,阚博文 小组照片 二.开会信息 时间:2017/11/14 11:35~11:57,总计22min.地点:东北 ...
- Notes of Daily Scrum Meeting(11.14)
Notes of Daily Scrum Meeting(11.14) 今天是项目第三周的周五,按原计划这时我们的项目应该已经要进入尾声进行组装调试了,但由于之前放假还有队员们的 效率比较低的原因,我 ...
- C++11/14笔记
目录 语言层面 模板表达式中的空格 nullptr和std::nullptr_t 自动推导类型----auto 一致性初始化----Uniform Initialization 初始化列表(initi ...
- 基数排序的可复用实现(C++11/14/17/20)
基数排序,是对整数类型的一种排序方法,有MSD (most significant digit)和LSD (least significant digit)两种.MSD将每个数按照高位分为若干个桶(按 ...
随机推荐
- 07模块化设计之top_down
一设计功能:(一)用两个分频模块,实现16分频,且让输入a 和b在16个系统时钟内,相与一次. (二)模块化设计思想(结构化思维) 拆分,即把一个系统划分成多个功能模块,控制模块,组合模块.然后从功能 ...
- BUAA 2021-2022毛概复习资料
2021-2022年毛概期末主观题复习范围,参考2022版教材和课程组官方PPT,原文太过敏感,所以贴出代码大家自己run #include <stdio.h> unsigned arti ...
- Java中会存在内存泄漏吗,请简单描述?
为了搞清楚Java程序是否有内存泄露存在,我们首先了解一下什么是内存泄露:程序运行过程中会不断地分配内存空间:那些不再使用的内存空间应该即时回收它们,从而保证系统可以再次使用这些内存.如果存在无用的内 ...
- 请描述一下Struts2的值栈结构,以及它是如何工作的?
值栈 Value Stack 值栈是Struts2框架的核心概念.所有的核心组件都以某种方式与之进行交互,它提供对上下文信息和执行环境中元素的访问机制.值栈的内容由如下4个层级组成. 1.临时对象 这 ...
- Java并发机制(3)--volatile关键字与内存模型
Java并发编程:volatile关键字解析及内存模型 个人整理自:博客园-海子-http://www.cnblogs.com/dolphin0520/p/3920373.html 1.线程内存模型: ...
- 线程池提交任务的两种方式:execute与submit的区别
Java中的线程池在进行任务提交时,有两种方式:execute和submit方法. 一.execute和submit的区别 execute只能提交Runnable类型的任务,无返回值.submit既可 ...
- 链接克隆的设置和kvm 安装
1.重新定义udev规则 删除 /etc/udev/rules.d/70-persistent-net.rules 网卡信息 # > /etc/udev/rules.d/70-p ...
- Python学习—(windows系统下)安装pygame
浏览器搜索pygame的windows安装程序,下载与python版本相匹配的文件. 如果.exe文件直接运行: 如果.whl文件,将其复制到要用到的项目文件夹中,在cmd窗口中切换到该文件所在的文件 ...
- (stm32f103学习总结)—can总线
参考:CAN总线的位时序与参数设置 CAN总线位同步 1 CAN总线介绍 CAN 是Controller Area Network 的缩写,中文意思是控制器局域网 络,是ISO国际标准化的串行通信协议 ...
- 【C语言】预处理、宏定义、内联函数
一.由源码到可执行程序的过程 1. 预处理: 源码经过预处理器的预处理变成预处理过的.i中间文件 1 gcc -E test.c -o test.i 2. 编译: 中间文件经过编译器编译形成.s的汇编 ...