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;
}

最新文章

  1. SalesForce 记录级别安全性
  2. Mysql使用workbench迁移数据
  3. Node Server管理
  4. python 逐行读取文件的三种方法
  5. Corelocation及地图控件学习笔记
  6. php 获取数组第一个值的方法分享
  7. 网站sqlserver提权操作
  8. HDU 4727 The Number Off of FFF 2013年四川省赛题
  9. 【转】asp.net 利用Global.asax 捕获整个解决方案中的异常错误
  10. android——拍照,相册图片剪切其实就这么简单
  11. Julia语言:让高性能科学计算人人可用
  12. 2018年手机应用UI设计趋势预测
  13. UML常用关系
  14. Problem 4: Largest palindrome product
  15. CRLF Injection漏洞的利用与实例分析
  16. 【nodejs】让nodejs像后端mvc框架(asp.net mvc )一样处理请求--路由限制及选择篇(2/8)【route】
  17. R 的内部机制
  18. 解决C3P0在Linux下Failed to get local InetAddress for VMID问题
  19. mysql percona安装
  20. poj2778 DNA Sequence【AC自动机】【矩阵快速幂】

热门文章

  1. ServletConfig对象和ServletContext对象有什么区别?
  2. CentOS7防火墙开启与关闭以及开放6379,3306,80等端口
  3. vmware克隆Centos虚拟机网卡无法启动问题
  4. 定时任务__@Xxl-JOB的使用
  5. c语言中的字面量
  6. MySQL索引机制(详细+原理+解析)
  7. display:inline-block两端对齐 实现列表
  8. nodejs和树莓派开发以及点亮RGB的LED灯代码
  9. 可想实现一个自己的简单jQuery库?(五)
  10. 使用Egret插件压缩代码包体积,减少请求数量的实战教程