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+图上构造)的更多相关文章

  1. 日常Java测试 2021/11/14

    课堂测试三 package word_show; import java.io.*;import java.util.*;import java.util.Map.Entry; public clas ...

  2. 2021.9.14考试总结[NOIP模拟53]

    T1 ZYB和售货机 容易发现把每个物品都买成$1$是没有影响的. 然后考虑最后一个物品的方案,如果从$f_i$向$i$连边,发现每个点有一个出度多个入度,可以先默认每个物品都能买且最大获利,这样可以 ...

  3. 日常Java 2021/11/18

    用idea实现Javaweb登录页面 <%-- Created by IntelliJ IDEA. User: Tefuir Date: 2021/11/18 Time: 18:14 To ch ...

  4. JavaSE_ API常用对象 总目录(11~14)

    JavaSE学习总结第11天_开发工具 & API常用对象111.01 常见开发工具介绍11.02 Eclipse和MyEclipse的概述11.03 Eclipse的下载安装及卸载11.04 ...

  5. Intel Artificial Intelligence Conference(2018.11.14)

    时间:2018.11.14地点:北京国贸大酒店

  6. 第26次Scrum会议(11/14)【欢迎来怼】

    一.小组信息 队名:欢迎来怼小组成员队长:田继平成员:李圆圆,葛美义,王伟东,姜珊,邵朔,阚博文 小组照片 二.开会信息 时间:2017/11/14 11:35~11:57,总计22min.地点:东北 ...

  7. Notes of Daily Scrum Meeting(11.14)

    Notes of Daily Scrum Meeting(11.14) 今天是项目第三周的周五,按原计划这时我们的项目应该已经要进入尾声进行组装调试了,但由于之前放假还有队员们的 效率比较低的原因,我 ...

  8. C++11/14笔记

    目录 语言层面 模板表达式中的空格 nullptr和std::nullptr_t 自动推导类型----auto 一致性初始化----Uniform Initialization 初始化列表(initi ...

  9. 基数排序的可复用实现(C++11/14/17/20)

    基数排序,是对整数类型的一种排序方法,有MSD (most significant digit)和LSD (least significant digit)两种.MSD将每个数按照高位分为若干个桶(按 ...

随机推荐

  1. UIautomatorviewer连接设备报错Unexpected error while obtaining UI hierarchy

    先来看下现象哈,点击sdk/tools下uiautomatorviever.bat,点击连接设备的图标,本以为就这么简单,那你就错了: 是不是看到这个瞬间心情就不好了,那么我们该怎么解决这个问题呢,归 ...

  2. IIS将应用程序池配置为在计划时间执行回收 (IIS 7)

    将应用程序池配置为在计划时间执行回收 您可以通过以下方法执行此过程:使用用户界面 (UI).在命令行窗口中运行 Appcmd.exe 命令.直接编辑配置文件或编写 WMI 脚本. 如下只介绍用户界面U ...

  3. 在Unity中用UGUI制作可输入下拉框

    Unity中UGUI制作可输入下拉框 目录 Unity中UGUI制作可输入下拉框 前言 组件分析 制作流程 总结 前言 在搜索引擎以及一些网页中我们常常可以看见这样一种UI控件,看上去是一个输入框,在 ...

  4. python-binasscii模块学习

    作用 binascii模块包含很多在二进制和ASCII编码的二进制表示转换的方法.通常情况不会直接使用这些功能,而是使用像UU,base64编码,或BinHex封装模块. binascii模块包含更高 ...

  5. zookeeper有几种部署模式? zookeeper 怎么保证主从节点的状态同步?

    一.zookeeper的三种部署模式 Zookeeper 有三种部署模式分别是单机模式.伪集群模式.集群模式.这三种模式在不同的场景下使用: 单机部署:一般用来检验 Zookeeper 基础功能,熟悉 ...

  6. elasticsearch 的倒排索引是什么 ?

    面试官:想了解你对基础概念的认知. 解答:通俗解释一下就可以. 传统的我们的检索是通过文章,逐个遍历找到对应关键词的位置. 而倒排索引,是通过分词策略,形成了词和文章的映射关系表,这种词典+映射表 即 ...

  7. java-jdbc-all

    jdbc相关解析 JDBC(Java DataBase Connectivity,Java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语 ...

  8. Effective Java —— 多字段下考虑使用建造者模式构建实例

    本文参考 本篇文章参考自<Effective Java>第三版第二条"Consider a builder when faced with many constructor pa ...

  9. OpenCV - Add Noise的一些方法

    噪声常用有两种:一种椒盐噪声,一种高斯噪声. import numpy as np def pepper_and_salt(src, proportion): """ : ...

  10. String工具类之“四个判空方式”StringUtils.isNotBlank和StringUtils.isEmpty和StringUtils.isBlank和StringUtils.isNotEmpty

    一.判断str字符串都不为空==>StringUtils.isNotBlank(String str); 1 /** 2 * <p>检查一个字符串是否非空("") ...