下午打了湘潭邀请赛,好像缓解了一下北京网络赛超强的自闭感。补一下这个图论题。(补了很久)

题意:给你一颗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的更多相关文章

  1. HDU 6280 From Tree to Graph(2018 湘潭邀请 E题,树的返祖边)

    其实打返祖边就相当于$x$到祖先这一段点(不包括两端)答案都要减$1$. 然后每个点最多减$1$次$1$. #include <bits/stdc++.h> using namespace ...

  2. 湘潭邀请赛 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)^... ...

  3. 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. ...

  4. Graph图总结

    将COMP20003中关于Graph的内容进行总结,内容来自COMP20003,中文术语并不准确,以英文为准. Graph G = {V, E} 顶Vertices V: can contain in ...

  5. CF375D Tree and Queries

    题意翻译 给出一棵 n 个结点的树,每个结点有一个颜色 c i . 询问 q 次,每次询问以 v 结点为根的子树中,出现次数 ≥k 的颜色有多少种.树的根节点是1. 感谢@elijahqi 提供的翻译 ...

  6. UVALive 6910 Cutting Tree 并查集

    Cutting Tree 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8& ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Nginx三部曲(2)性能

    我们会告诉你 Nginx 如何工作及其背后的理念,还有如何优化以加快应用的性能,如何安装启动和保持运行. 这个教程有三个部分: 基本概念 —— 这部分需要去了解 Nginx 的一些指令和使用场景,继承 ...

  2. [转帖]NUMA架构的CPU -- 你真的用好了么?

    NUMA架构的CPU -- 你真的用好了么? 本文从NUMA的介绍引出常见的NUMA使用中的陷阱,继而讨论对于NUMA系统的优化方法和一些值得关注的方向. 文章欢迎转载,但转载时请保留本段文字,并置于 ...

  3. [转帖]前端-chromeF12 谷歌开发者工具详解 Sources篇

    前端-chromeF12 谷歌开发者工具详解 Sources篇 原贴地址:https://blog.csdn.net/qq_39892932/article/details/82498748 cons ...

  4. [转帖]浏览器的F5和Ctrl+F5

    浏览器的F5和Ctrl+F5 https://www.cnblogs.com/xiangcode/p/5369084.html 在浏览器里中,按F5键和按F5同时按住Ctrl键(简称Ctrl+F5), ...

  5. Mybatis测试用例

    package cn.zhangxueliang.mybatis.mapper; import static org.junit.Assert.*; import java.io.InputStrea ...

  6. vue图片被加了盗链

    https://www.cnblogs.com/dongcanliang/archive/2017/04/01/6655061.html <meta name="referrer&qu ...

  7. python之路--MySQL 库,表的详细操作

    一 库操作 数据库命名规则 可以由数字,字母,下划线,@, #, $ 区分大小写 唯一性 不能使用关键字如 create  select 不能单独使用数字 最长128位 # 这些是对上次的补充. 二 ...

  8. umount -fl用法

    umount, 老是提示:device is busy, 服务又不能停止的.可以用"umount -fl"解决! 挂载: mount - mount a filesystem mo ...

  9. Jmeter安装与使用(压测)

    一.介绍 Apache JMeter是100%纯JAVA桌面应用程序,被设计为用于测试客户端/服务端结构的软件(例如web应用程序).它可以用来测试静态和动态资源的性能,例如:静态文件,Java Se ...

  10. 设计模式笔记:单一职责原则(SRP, Single Responsibility Principle)

    1. 单一职责原则核心思想 一个类应该有且只有一个变化的原因. 2. 为什么引入单一职责原则 单一职责原则将不同的职责分离到单独的类,每一个职责都是一个变化的中心. 在SRP中,把职责定义为变化的原因 ...