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

题意:给你一颗n节点的树,有m个操作,每次向xi和lca(xi,yi)连边,然后每次zi就是对于新的图在删除每一个点后连通块个数的异或和。然后求的是m次操作后x,y的值。

题解:看这个问题看了好久我都完全无从下手,题意也理解了半天,只知道有环prprpr,然后和x到lca这条链上的点有关系。但是感觉怎么都会T,就只能暴力更新。然后就看别人的题解,并且打开了画图软件,首先,对于一颗树每个点删除后产生的联通块个数就是它的入度和出度的和。然后异或一下就好。也就是和它度数有关。然后对于每次加的那条边,可以发现这条边的两个点的删除后个数不变,而那条链上的其余点联通块个数减减。然后就是最关键的,对于每条边,最多只会更新一次,因为成环后,新加的边所形成的新环,如果更新的链也通过之前存在的环走过的链,此时对于这条链上的点是无影响的,因为原来的这条边已经被减减过了。画图是这样,写博客中间又仔细想了一想,应该是这样理解的?也就是我们可以跳过这些环,缩环为点,用并查集缩环???第一次听说,然后写法上挺有讲究的吧,它可能并查集跳到的点会超过lca,所以要用深度判断一下。如果写的不完全对,以后懂了来改好了

  1. #include<bits/stdc++.h>
  2. #define ll long long
  3. #define pb push_back
  4. #define _mp make_pair
  5. #define ldb long double
  6. using namespace std;
  7. const int maxn=5005;
  8. inline ll read()
  9. {
  10. ll x=0,f=1;char ch=getchar();
  11. while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
  12. while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
  13. return x*f;
  14. }
  15. int lca[maxn][20];
  16. int bcg[maxn],depth[maxn];
  17. int n,m,a,b,x,y;
  18. int fir[maxn],nxt[maxn*2],to[maxn*2];
  19. int du[maxn];
  20. int cnt;
  21. int ans;
  22. void add_e(int x,int y)
  23. {
  24. ++cnt;nxt[cnt]=fir[x];fir[x]=cnt;to[cnt]=y;
  25. ++cnt;nxt[cnt]=fir[y];fir[y]=cnt;to[cnt]=x;
  26. }
  27. int findd(int x)
  28. {
  29. return bcg[x]==x?bcg[x]:bcg[x]=findd(bcg[x]);
  30. }
  31. int LCA(int x,int y)
  32. {
  33. if(depth[x]<depth[y])swap(x,y);
  34. int dd=depth[x]-depth[y];
  35. for(int i=18;i>=0;i--)
  36. {
  37. if(dd&(1<<i))x=lca[x][i];
  38. }
  39. if(x==y)return y;
  40. for(int i=18;i>=0;i--)
  41. {
  42. if(lca[x][i]!=lca[y][i])
  43. {
  44. x=lca[x][i];
  45. y=lca[y][i];
  46. }
  47. }
  48. return lca[x][0];
  49. }
  50. void dfs(int x,int fa)
  51. {
  52. lca[x][0]=fa;
  53. depth[x]=depth[fa]+1;
  54. for(int i=fir[x];i;i=nxt[i])
  55. {
  56. int pp=to[i];
  57. if(pp==fa)continue;
  58. dfs(pp,x);
  59. }
  60. }
  61. void lca_init()
  62. {
  63. dfs(1,0);
  64. depth[0]=0;
  65. for(int k=1;k<=18;k++)
  66. {
  67. for(int i=1;i<=n;i++)
  68. {
  69. lca[i][k]=lca[lca[i][k-1]][k-1];
  70. }
  71. }
  72. }
  73. void init()
  74. {
  75. memset(depth,0,sizeof(depth));
  76. memset(lca,0,sizeof(lca));
  77. for(int i=1;i<=n;i++)bcg[i]=i;
  78. for(int i=1;i<=n;i++)du[i]=0;
  79. cnt=0;
  80. memset(fir,0,sizeof(fir));
  81. }
  82. void update(int x,int y)
  83. {
  84. x=findd(x);
  85. if(depth[lca[x][0]]<=depth[y]||lca[x][0]==0)
  86. {
  87. return ;
  88. }
  89. ans=ans^du[lca[x][0]]^(--du[lca[x][0]]);
  90. bcg[x]=lca[x][0];
  91. update(lca[x][0],y);
  92. }
  93. int main()
  94. {
  95. while(~scanf("%d%d%d%d%d%d",&n,&m,&a,&b,&x,&y))
  96. {
  97. init();
  98. int p,q;
  99. for(int i=1;i<n;i++)
  100. {
  101. scanf("%d%d",&p,&q);
  102. p++,q++;
  103. add_e(p,q);
  104. du[p]++,du[q]++;
  105. }
  106. lca_init();
  107. ans=0;
  108. for(int i=1;i<=n;i++)
  109. {
  110. ans^=du[i];
  111. }
  112. for(int i=0;i<m;i++)
  113. {
  114. int nx=(a*x+b*y+ans)%n;
  115. int ny=(b*x+a*y+ans)%n;
  116. x=nx;
  117. y=ny;
  118. update(x+1,LCA(x+1,y+1));
  119. }
  120. printf("%d %d\n",x,y);
  121.  
  122. }
  123. }

  

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. 五、es6 Set

    一.特点 1.是一个构造函数 2.类数组,元素唯一.没有重复 二.new Set(); 二.构造函数接受数组将数组转换成Set数据结构,[...new Set(1,3)],转化成对象: console ...

  2. Es6数值拓展

    Es6数值拓展 一,Number扩展 1,ES6 提供了二进制和八进制数值的新的写法,分别用前缀0b(或0B)和0o(或0O)表示. 将0b和0o前缀的字符串数值转为十进制,要使用Number方法 N ...

  3. WebService实例-CRM系统提供WebService实现用户注册功能

    <—start—> 编写crm的webservice接口,实现客户信息保存操作.在CustomerService接口中新增一个服务接口,用于添加客户注册的信息. @Path("/ ...

  4. Day1 基础知识

    数据类型,字符编码 二进制: 定义:二进制数据是用0和1两个数码来表示的数.它的基数为2,进位规则是“逢二进一”,借位规则是“借一当二”.当前的计算机系统使用的基本上是二进制系统,数据在计算机中主要是 ...

  5. Oracle 中sql文件的导入导出

    导出 一般导入的时候我用的是命令行 imp c##zs/@orcl fromuser=c##zs touser=c##zs file=D:\java\.dmp ignore=y c##zs 是创建的用 ...

  6. h5简易手写板

    ............. 我该说点什么呢,开头居然不知道想说点什么!好吧不知道说什么,我们就来说说这个手写板吧,虽然这个手写板现在没什么用,但是.....,好像的确没什么用啊! 只是存粹哪里练手的的 ...

  7. Django--ORM和单表查询

    一 . ORM ORM是“对象-关系-映射”的简称.(Object Relational Mapping,简称ORM) 二. 单表操作 要想将模型转为mysql数据库中的表,需要在setting里面写 ...

  8. DAY09、函数

    一.函数的定义:跟变量名的定义大同小异 1.声明函数的关键词:def 2.函数(变量)名:使用函数的依据 3.参数列表:()   参数个数可以为0到n个,但()一定不能丢,完成功能的必要条件 4.函数 ...

  9. thinkphp视图中插入php代码

    性别: <?php if($item['sex'] == 1):?> 男 <?php else:?> 女 <?php endif;?> 错误:<?php ec ...

  10. 【python练习题】程序9

    #题目:暂停一秒输出. import time for i in range(5): print (i) time.sleep(1)