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

题意:给你一颗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. 【Python3练习题 014】 一个数如果恰好等于它的因子之和,这个数就称为“完数”。例如6=1+2+3。编程找出1000以内的所有完数。

    a.b只要数字a能被数字b整除,不论b是不是质数,都算是a的因子.比如:8的质因子是 2, 2, 2,但8的因子就包括 1,2,4. import math   for i in range(2, 1 ...

  2. 转《vue引入第三方js库》

    一.绝对路径直接引入,全局可用 二.绝对路径直接引入,配置后,import 引入后再使用 三.webpack中配置 alias,import 引入后再使用 四.webpack 中配置 plugins, ...

  3. 图解Python的直接赋值与浅拷贝和深度拷贝三者区别

    直接赋值:其实就是对象的引用(别名). 浅拷贝(copy):拷贝父对象,不会拷贝对象的内部的子对象. 深拷贝(deepcopy): copy 模块的 deepcopy 方法,完全拷贝了父对象及其子对象 ...

  4. python爬虫之win7Mongod安装使用

    1.下载地址:https://www.mongodb.com/download-center#community 下载完成以后下一步下一步安装. 安装路径 还需要建立一个数据库存储位置C:\mongo ...

  5. WPF如何实现TreeView节点重命名

    我们经常看到一些软件比如酷狗音乐,在对列表右键进行重命名的时候,当前列表会泛白并且进入可编辑状态,当我们更改完成后就会并进入非编辑状态,这些具体是怎么实现的呢?下面的方法也许会提供一些思路,下面的Tr ...

  6. Gatsby & React & NPX & NVM

    Gatsby & React Gatsby is a blazing fast modern site generator for React. https://www.gatsbyjs.or ...

  7. 库存盘点打印功能生成PDF速度太慢使用页面缓存

    一.业务需求 二.产品设计 三.UI设计 四.程序设计 1.使用behavior配置页面缓存 class WmsCheckController extends Controller { /** * @ ...

  8. 解决 Redis 只读不可写的问题

    本文转载:https://blog.csdn.net/han_cui/article/details/54767208?tdsourcetag=s_pcqq_aiomsg 解决 Redis 只读不可写 ...

  9. orcale建表脚本

    declare v_cnt number; V_SQL VARCHAR2 (500) := '';begin select count(*) into v_cnt from dual where ex ...

  10. hdu-3068(最长回文子串-manacher)

    题意:求一个字符串#include<iostream>#include<algorithm>#include<cstring>using namespace std ...