HDU6280 From Tree to Graph
下午打了湘潭邀请赛,好像缓解了一下北京网络赛超强的自闭感。补一下这个图论题。(补了很久)
题意:给你一颗n节点的树,有m个操作,每次向xi和lca(xi,yi)连边,然后每次zi就是对于新的图在删除每一个点后连通块个数的异或和。然后求的是m次操作后x,y的值。
题解:看这个问题看了好久我都完全无从下手,题意也理解了半天,只知道有环prprpr,然后和x到lca这条链上的点有关系。但是感觉怎么都会T,就只能暴力更新。然后就看别人的题解,并且打开了画图软件,首先,对于一颗树每个点删除后产生的联通块个数就是它的入度和出度的和。然后异或一下就好。也就是和它度数有关。然后对于每次加的那条边,可以发现这条边的两个点的删除后个数不变,而那条链上的其余点联通块个数减减。然后就是最关键的,对于每条边,最多只会更新一次,因为成环后,新加的边所形成的新环,如果更新的链也通过之前存在的环走过的链,此时对于这条链上的点是无影响的,因为原来的这条边已经被减减过了。画图是这样,写博客中间又仔细想了一想,应该是这样理解的?也就是我们可以跳过这些环,缩环为点,用并查集缩环???第一次听说,然后写法上挺有讲究的吧,它可能并查集跳到的点会超过lca,所以要用深度判断一下。如果写的不完全对,以后懂了来改好了
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define _mp make_pair
#define ldb long double
using namespace std;
const int maxn=5005;
inline ll read()
{
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int lca[maxn][20];
int bcg[maxn],depth[maxn];
int n,m,a,b,x,y;
int fir[maxn],nxt[maxn*2],to[maxn*2];
int du[maxn];
int cnt;
int ans;
void add_e(int x,int y)
{
++cnt;nxt[cnt]=fir[x];fir[x]=cnt;to[cnt]=y;
++cnt;nxt[cnt]=fir[y];fir[y]=cnt;to[cnt]=x;
}
int findd(int x)
{
return bcg[x]==x?bcg[x]:bcg[x]=findd(bcg[x]);
}
int LCA(int x,int y)
{
if(depth[x]<depth[y])swap(x,y);
int dd=depth[x]-depth[y];
for(int i=18;i>=0;i--)
{
if(dd&(1<<i))x=lca[x][i];
}
if(x==y)return y;
for(int i=18;i>=0;i--)
{
if(lca[x][i]!=lca[y][i])
{
x=lca[x][i];
y=lca[y][i];
}
}
return lca[x][0];
}
void dfs(int x,int fa)
{
lca[x][0]=fa;
depth[x]=depth[fa]+1;
for(int i=fir[x];i;i=nxt[i])
{
int pp=to[i];
if(pp==fa)continue;
dfs(pp,x);
}
}
void lca_init()
{
dfs(1,0);
depth[0]=0;
for(int k=1;k<=18;k++)
{
for(int i=1;i<=n;i++)
{
lca[i][k]=lca[lca[i][k-1]][k-1];
}
}
}
void init()
{
memset(depth,0,sizeof(depth));
memset(lca,0,sizeof(lca));
for(int i=1;i<=n;i++)bcg[i]=i;
for(int i=1;i<=n;i++)du[i]=0;
cnt=0;
memset(fir,0,sizeof(fir));
}
void update(int x,int y)
{
x=findd(x);
if(depth[lca[x][0]]<=depth[y]||lca[x][0]==0)
{
return ;
}
ans=ans^du[lca[x][0]]^(--du[lca[x][0]]);
bcg[x]=lca[x][0];
update(lca[x][0],y);
}
int main()
{
while(~scanf("%d%d%d%d%d%d",&n,&m,&a,&b,&x,&y))
{
init();
int p,q;
for(int i=1;i<n;i++)
{
scanf("%d%d",&p,&q);
p++,q++;
add_e(p,q);
du[p]++,du[q]++;
}
lca_init();
ans=0;
for(int i=1;i<=n;i++)
{
ans^=du[i];
}
for(int i=0;i<m;i++)
{
int nx=(a*x+b*y+ans)%n;
int ny=(b*x+a*y+ans)%n;
x=nx;
y=ny;
update(x+1,LCA(x+1,y+1));
}
printf("%d %d\n",x,y); }
}
HDU6280 From Tree to Graph的更多相关文章
- HDU 6280 From Tree to Graph(2018 湘潭邀请 E题,树的返祖边)
其实打返祖边就相当于$x$到祖先这一段点(不包括两端)答案都要减$1$. 然后每个点最多减$1$次$1$. #include <bits/stdc++.h> using namespace ...
- 湘潭邀请赛 2018 E From Tree to Graph
题意: 给出一棵树以及m,a,b,x0,y0.之后加m条边{(x1,LCA(x1,y1)),(x2,LCA(x2,y2))...(xm,LCA(xm,ym))}.定义z = f(0)^f(1)^... ...
- Clone Graph leetcode java(DFS and BFS 基础)
题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...
- Graph图总结
将COMP20003中关于Graph的内容进行总结,内容来自COMP20003,中文术语并不准确,以英文为准. Graph G = {V, E} 顶Vertices V: can contain in ...
- CF375D Tree and Queries
题意翻译 给出一棵 n 个结点的树,每个结点有一个颜色 c i . 询问 q 次,每次询问以 v 结点为根的子树中,出现次数 ≥k 的颜色有多少种.树的根节点是1. 感谢@elijahqi 提供的翻译 ...
- UVALive 6910 Cutting Tree 并查集
Cutting Tree 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8& ...
- CodeForces - 963B Destruction of a Tree (dfs+思维题)
B. Destruction of a Tree time limit per test 1 second memory limit per test 256 megabytes input stan ...
- codeforces 963B Destruction of a Tree
B. Destruction of a Tree time limit per test 1 second memory limit per test 256 megabytes input stan ...
- 963B:Destruction of a Tree
You are given a tree (a graph with n vertices and n - 1 edges in which it's possible to reach any ve ...
随机推荐
- MySQL 5.7默认ONLY_FULL_GROUP_BY语义介绍
mysql 5.7版本 出现 ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corre ...
- Angular 过滤器
<!DOCTYPE html><html ng-app="myApp"><head lang="en"> <meta ...
- vue.js 添加 fastclick的支持
fastclick:处理移动端click事件300毫秒延迟 1.兼容性 iOS 3及更高版本的移动Safari iOS 5及更高版本的Chrome Android上的Chrome(ICS) Opera ...
- Python基础知识2-内置数据结构(上)
分类 数值型 用浮点型的时候注意别和"=="一起使用. 数字的处理函数 注意round()函数的特殊:四舍六入五取偶 类型判断 列表list 列表list定义 初始化 列表索引访 ...
- Java变量类型识别的3种方式
内容导览 反射方式,成员变量的类型判断 isInstance用法 利用泛型识别类型 测试类: package com.cxyapi.oo; import java.util.Date; import ...
- 部署wp 的时候遭遇的google字体问题
国内WP CDN问题昨天下午到今天中午 总算解决了WORDPRESS 在 谷歌字体被枪毙之后带来的一系列问题..在天朝想当个 安静的美男子设计师也是那么烦恼的事情捏! 解决办法将所有的带fonts.g ...
- uname -r查询版本不是安装的版本的问题
uname -r 查出来的版本与/lib/modules下面的内核版本不匹配.啥原因? 第一步,先strace uname -r看看这个uname -r到底从哪里获取的版本. strace没有看出来 ...
- Java集合和数组的区别
参考:Java集合和数组的区别 集合和容器都是Java中的容器. 区别 数组特点:大小固定,只能存储相同数据类型的数据 集合特点:大小可动态扩展,可以存储各种类型的数据 转换 数组转换为集合: A ...
- has invalid type <class 'numpy.ndarray'>, must be a string or Tensor
转自: https://blog.csdn.net/jacke121/article/details/78833922 has invalid type <class 'numpy.ndarra ...
- orcale增加列脚本
--编号declare v_cnt number; V_SQL VARCHAR2 (500) := '';begin select count(*) into v_cnt from dual wher ...