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 ...
随机推荐
- sql之cursor的简介和字符串拆分(split)与游标的使用
字符串拆分(split)与游标的使用 CREATE TABLE Plates ( ,), ) NOT NULL, [BusinessId] INT NOT NULL, ) ),),), SELECT ...
- Day 4-2 random模块
import random random.randint(1,100) # 从1到100中随机取出一个数.包含100 random.randrange(1,100) #功能和上面一样.只是不包含100 ...
- PMP三点
三点估算:悲观36天,可能21天,乐观6天.在16至26天内完成的概率是多少?这个算法是PERT估算最终估算结果=(悲观工期+乐观工期+4×最可能工期)/6=(36+6++4*21)/6=21标准差= ...
- CLOUD计算产品成本嵌套
1.产品入库单入库的半成品A (无单价) 2.其他出库单上(共耗的)出库的半成品A(无单价) 不管在同车间还是不同车间内都是认定为嵌套的,所以可以计算2遍成本,第1遍不考虑嵌套,第2遍就能计算进去了.
- SAP配置BOM的适用范围
配置BOM中定义属性,单纯的编码要搞死人: 适合小批量周期短多品种
- python学习笔记(7)--循环语句
循环语句如下: for i in range(start, end): //注意 前闭后开 coding for i in range(m,n,k): coding for c in s: codin ...
- mysql 数据库的主从同步
1.复制准备 操作系统 centOS 主库(mysql master): ip为123.56.94.1 port为3306 mysql 版本 5.7.16 从库(mysql slave): ...
- python 编码格式
1. 字符编码简介 1.1. ASCII ASCII(American Standard Code for Information Interchange),是一种单字节的编码.计算机世界里一开始只有 ...
- Java多线程之定时任务(Timer)
package org.study2.javabase.ThreadsDemo.schedule; import java.util.Date; import java.util.Timer; imp ...
- 转载 大话pcie
原文https://blog.csdn.net/abcamus/article/details/76167747 一.PCIe DMA机制 PCIe控制器也提供DMA(Direct Memory ac ...