题目性质比较显然,相同颜色联通块可以合并成一个点,重新建树后,发现相邻两个点的颜色一定是不一样的。

然后发现,对于一条链来说,每次把一个点反色,实际上使点数少了2个。如下图

而如果一条链上面有分支,也是一样:

所以我们实际上只需要把最长链上的变成一种颜色就可以了。最长链就是直径,需要改动的点就是$\frac{tot+1}{2}$,$tot$就是直径的点数。

(话说$stl$好慢aaa!!!要克制住我自己少用$map$叻!

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<map>
  4. #include<cstring>
  5. #include<algorithm>
  6. using namespace std;
  7.  
  8. int n, a[];
  9. int stot, tov[], nex[], h[];
  10. map < pair < int, int >, int > G;
  11.  
  12. void add ( int u, int v ) {
  13. tov[++stot] = v;
  14. nex[stot] = h[u];
  15. h[u] = stot;
  16. }
  17.  
  18. int stot_co, tov_co[], nex_co[], h_co[];
  19. void add_co ( int u, int v ) {
  20. tov_co[++stot_co] = v;
  21. nex_co[stot_co] = h_co[u];
  22. h_co[u] = stot_co;
  23. }
  24.  
  25. int fa[];
  26. int find ( int x ) {
  27. if ( x != fa[x] ) fa[x] = find ( fa[x] );
  28. return x;
  29. }
  30.  
  31. void unionn ( int x, int y ) {
  32. int xx = find ( x ), yy = find ( y );
  33. fa[xx] = yy;
  34. }
  35.  
  36. int opt, color[];
  37. void dfs1 ( int u, int f ) {
  38. for ( int i = h[u]; i; i = nex[i] ) {
  39. int v = tov[i];
  40. if ( v == f ) continue;
  41. color[v] = ;
  42. if ( a[v] == a[u] ) color[v] = color[u];
  43. else color[v] = ++ opt;
  44. dfs1 ( v, u );
  45. int x = find ( color[v] ), y = find ( color[u] );
  46. if ( color[v] != color[u] && x != y ) {
  47. add_co ( color[v], color[u] );
  48. add_co ( color[u], color[v] );
  49. unionn ( x, y );
  50. }
  51. }
  52. }
  53.  
  54. int dis[], rt;
  55. void dfs ( int u, int f ) {
  56. for ( int i = h_co[u]; i; i = nex_co[i] ) {
  57. int v = tov_co[i];
  58. if ( v == f ) continue;
  59. dis[v] = dis[u] + ;
  60. dfs ( v, u );
  61. }
  62. if ( dis[u] > dis[rt] ) rt = u;
  63. }
  64.  
  65. int main ( ) {
  66. freopen ( "color.in", "r", stdin );
  67. freopen ( "color.out", "w", stdout );
  68. int T;
  69. scanf ( "%d", &T );
  70. while ( T -- ) {
  71. G.clear ( );
  72. stot = , stot_co = , opt = ;
  73. scanf ( "%d", &n );
  74. for ( int i = ; i <= n; i ++ ) h[i] = ;
  75. for ( int i = ; i <= n; i ++ ) h_co[i] = ;
  76. for ( int i = ; i <= n; i ++ ) color[i] = ;
  77. for ( int i = ; i <= n; i ++ ) scanf ( "%d", &a[i] );
  78. for ( int i = ; i < n; i ++ ) {
  79. int u, v;
  80. scanf ( "%d%d", &u, &v );
  81. add ( u, v );
  82. add ( v, u );
  83. }
  84. for ( int i = ; i <= n; i ++ ) fa[i] = i;
  85. color[] = ++opt;
  86. dfs1 ( , );
  87. rt = ;
  88. for ( int i = ; i <= opt; i ++ ) dis[i] = ;
  89. dfs ( , );
  90. for ( int i = ; i <= opt; i ++ ) dis[i] = ;
  91. dfs ( rt, );
  92. printf ( "%d\n", ( dis[rt] + ) / );
  93. }
  94. return ;
  95. }

小模拟,考试的时候觉得状态太多,不可能重复???然后就没有写$vis$打标记,下来一加就...(辛酸泪QAQ

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. #include<queue>
  5. #include<map>
  6. using namespace std;
  7.  
  8. int n, t[], G[][];
  9. map < int, int > mx, my;
  10.  
  11. struct node {
  12. int x, y, opt, id;
  13. node ( int x = , int y = , int opt = , int id = ) :
  14. x ( x ), y ( y ), opt ( opt ), id ( id ) { }
  15. };
  16. queue < node > q;
  17. int vis[][][][];
  18.  
  19. struct QAQ {
  20. int x, y;
  21. QAQ ( int x = , int y = ) :
  22. x ( x ), y ( y ) { }
  23. };
  24.  
  25. QAQ print ( int x, int y, int opt, int id ) {
  26. for ( int i = ; i <= t[id]; i ++ ) {
  27. G[x][y] = ;
  28. x = x + mx[opt]; y = y + my[opt];
  29. }
  30. return QAQ ( x - mx[opt], y - my[opt] );
  31. }
  32.  
  33. void BFS ( int sx, int sy ) {
  34. q.push ( node ( sx, sy + t[] - , , ) );
  35. QAQ a = print ( sx, sy, , );
  36. vis[sx][sy+t[]-][][] = ;
  37. while ( !q.empty ( ) ) {
  38. node x = q.front ( ); q.pop ( );
  39. if ( x.id == n ) break;
  40. if ( x.opt != - && x.opt != ) {
  41. int L = x.opt - , R = x.opt + ;
  42. int id = x.id + ;
  43. int lsx = x.x + mx[L], lsy = x.y + my[L];
  44. int rsx = x.x + mx[R], rsy = x.y + my[R];
  45. QAQ ll = print ( lsx, lsy, L, id );
  46. QAQ rr = print ( rsx, rsy, R, id );
  47. if ( !vis[ll.x][ll.y][id][L] ) {
  48. q.push ( node ( ll.x, ll.y, L, id ) );
  49. vis[ll.x][ll.y][id][L] = ;
  50. }
  51. if ( !vis[rr.x][rr.y][id][R] ) {
  52. q.push ( node ( rr.x, rr.y, R, id ) );
  53. vis[rr.x][rr.y][id][R] = ;
  54. }
  55. } else if ( x.opt == - ) {
  56. int L = , R = x.opt + ;
  57. int id = x.id + ;
  58. int lsx = x.x + mx[L], lsy = x.y + my[L];
  59. int rsx = x.x + mx[R], rsy = x.y + my[R];
  60. QAQ ll = print ( lsx, lsy, L, id );
  61. QAQ rr = print ( rsx, rsy, R, id );
  62. if ( !vis[ll.x][ll.y][id][L] ) {
  63. q.push ( node ( ll.x, ll.y, L, id ) );
  64. vis[ll.x][ll.y][id][L] = ;
  65. }
  66. if ( !vis[rr.x][rr.y][id][R] ) {
  67. q.push ( node ( rr.x, rr.y, R, id ) );
  68. vis[rr.x][rr.y][id][R] = ;
  69. }
  70. } else if ( x.opt == ) {
  71. int L = x.opt - , R = -;
  72. int id = x.id + ;
  73. int lsx = x.x + mx[L], lsy = x.y + my[L];
  74. int rsx = x.x + mx[R], rsy = x.y + my[R];
  75. QAQ ll = print ( lsx, lsy, L, id );
  76. QAQ rr = print ( rsx, rsy, R, id );
  77. if ( !vis[ll.x][ll.y][id][L] ) {
  78. q.push ( node ( ll.x, ll.y, L, id ) );
  79. vis[ll.x][ll.y][id][L] = ;
  80. }
  81. if ( !vis[rr.x][rr.y][id][R] ) {
  82. q.push ( node ( rr.x, rr.y, R, id ) );
  83. vis[rr.x][rr.y][id][R] = ;
  84. }
  85. }
  86. }
  87. }
  88.  
  89. int main ( ) {
  90. freopen ( "grow.in", "r", stdin );
  91. freopen ( "grow.out", "w", stdout );
  92. scanf ( "%d", &n );
  93. mx[] = , mx[] = , mx[] = , mx[] = , mx[] = , mx[-] = -, mx[-] = -, mx[-] = -;
  94. my[] = , my[] = , my[] = , my[] = -, my[] = -, my[-] = , my[-] = , my[-] = -;
  95. for ( int i = ; i <= n; i ++ ) scanf ( "%d", &t[i] );
  96. BFS ( , );
  97. int ans = ;
  98. for ( int i = ; i < ; i ++ )
  99. for ( int j = ; j < ; j ++ )
  100. if ( G[i][j] ) ans ++;
  101. printf ( "%d", ans );
  102. }

有关串用$dp$解决是很显然的(?$idy$题解原话),定义$dp[s][t]$表示从$s$状态转移到$t$状态最少的修改数。关于状态定义代码有注释。用线段树维护区间状态转移$dp$值,每个节点保存一个矩阵,节点合并时类似$floyed$,枚举断点转移。查询时查询区间即可。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #define oo 0x3f3f3f3f
  5. using namespace std;
  6.  
  7. // 0 1 2 3 4
  8. // $ 2 20 201 2017
  9.  
  10. const int N = ;
  11.  
  12. struct Info {
  13. int dp[][];
  14. void init ( int cur ) {
  15. memset ( dp, 0x3f, sizeof ( dp ) );
  16. if ( cur == || cur == || cur == || cur == || cur == ) {
  17. for ( int i = ; i <= ; i ++ )
  18. dp[i][i] = ;
  19. } else if ( cur == ) {
  20. dp[][] = ; dp[][] = ;
  21. dp[][] = dp[][] = dp[][] = dp[][] = ;
  22. } else if ( cur == ) {
  23. dp[][] = ;
  24. dp[][] = ;
  25. dp[][] = dp[][] = dp[][] = dp[][] = ;
  26. } else if ( cur == ) {
  27. dp[][] = ;
  28. dp[][] = ;
  29. dp[][] = dp[][] = dp[][] = dp[][] = ;
  30. } else if ( cur == ) {
  31. dp[][] = ;
  32. dp[][] = ;
  33. dp[][] = dp[][] = dp[][] = dp[][] = ;
  34. } else if ( cur == ) {
  35. dp[][] = ;
  36. dp[][] = ;
  37. dp[][] = dp[][] = dp[][] = ;
  38. }
  39. }
  40. };
  41.  
  42. Info operator + ( const Info &r, const Info &s ) {
  43. Info rt;
  44. memset ( &rt, 0x3f, sizeof ( rt ) );
  45. for ( int i = ; i <= ; i ++ )
  46. for ( int j = ; j <= ; j ++ )
  47. for ( int k = i; k <= j; k ++ ) {
  48. rt.dp[i][j] = min ( rt.dp[i][j], r.dp[i][k] + s.dp[k][j] );
  49. }
  50. return rt;
  51. }
  52.  
  53. struct node {
  54. Info info;
  55. node *ls, *rs;
  56. } pool[N*], *tail = pool, *root;
  57.  
  58. int a[N];
  59. char s[N];
  60. node *build ( int l, int r ) {
  61. node *nd = ++tail;
  62. if ( l == r ) {
  63. nd -> info.init ( a[l] );
  64. return nd;
  65. }
  66. int mid = ( l + r ) >> ;
  67. nd -> ls = build ( l, mid );
  68. nd -> rs = build ( mid + , r );
  69. nd -> info = nd -> ls -> info + nd -> rs -> info;
  70. return nd;
  71. }
  72.  
  73. Info query ( node *nd, int l, int r, int L, int R ) {
  74. if ( l >= L && r <= R ) return nd -> info;
  75. int mid = ( l + r ) >> ;
  76. if ( R <= mid ) return query ( nd -> ls, l, mid, L, R );
  77. else if ( L > mid ) return query ( nd -> rs, mid + , r, L, R );
  78. else return query ( nd -> ls, l, mid, L, R ) + query ( nd -> rs, mid + , r, L, R );
  79. }
  80.  
  81. int n, q;
  82. int query ( int l, int r ) {
  83. Info info = query ( root, , n, l, r );
  84. return info.dp[][] == oo ? - : info.dp[][];
  85. }
  86.  
  87. int main ( ) {
  88. freopen ( "year.in", "r", stdin );
  89. freopen ( "year.out", "w", stdout );
  90. scanf ( "%s", s + );
  91. scanf ( "%d", &q );
  92. n = strlen ( s + );
  93. for ( int i = ; i <= n; i ++ )
  94. a[i] = s[i] - '';
  95. root = build ( , n );
  96. while ( q -- ) {
  97. int l, r;
  98. scanf ( "%d%d", &l, &r );
  99. printf ( "%d\n", query ( l, r ) );
  100. }
  101. return ;
  102. }

【8.26校内测试】【重构树求直径】【BFS模拟】【线段树维护DP】的更多相关文章

  1. [CSP-S模拟测试]:椎(线段树维护区间最值和单调栈)

    题目描述 虽不能至,心向往之. $Treap=Tree+Heap$ 椎$=$树$+$堆 小$\pi$学习了计算机科学中的数据结构$Treap$. 小$\pi$知道$Treap$指的是一种树. 小$\p ...

  2. 7.18 NOI模拟赛 因懒无名 线段树分治 线段树维护直径

    LINK:因懒无名 20分显然有\(n\cdot q\)的暴力. 还有20分 每次只询问一种颜色的直径不过带修改. 容易想到利用线段树维护直径就可以解决了. 当然也可以进行线段树分治 每种颜色存一下直 ...

  3. bzoj 3551 kruskal重构树dfs序上的主席树

    强制在线 kruskal重构树,每两点间的最大边权即为其lca的点权. 倍增找,dfs序对应区间搞主席树 #include<cstdio> #include<cstring> ...

  4. 【8.23校内测试】【贪心】【线段树优化DP】

    $m$的数据范围看起来非常有问题??仔细多列几个例子可以发现,在$m<=5$的时候,只要找到有两行状态按位$&$起来等于$0$,就是可行方案,如果没有就不行. #include<i ...

  5. 倍增/线段树维护树的直径 hdu5993/2016icpc青岛L

    题意: 给一棵树,每次询问删掉两条边,问剩下的三棵树的最大直径 点10W,询问10W,询问相互独立 Solution: 考虑线段树/倍增维护树的直径 考虑一个点集的区间 [l, r] 而我们知道了有 ...

  6. Snow的追寻--线段树维护树的直径

    Snow终于得知母亲是谁,他现在要出发寻找母亲.王国中的路由于某种特殊原因,成为了一棵有n个节点的根节点为1的树,但由于"Birds are everywhere.",他得到了种种 ...

  7. HDU1255 覆盖的面积 —— 求矩形交面积 线段树 + 扫描线 + 离散化

    题目链接:https://vjudge.net/problem/HDU-1255 给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. Input输入数据的第一行是一个正整数T(1<= ...

  8. 【10.26校内测试】【状压?DP】【最小生成树?搜索?】

    Solution 据说正解DP30行??? 然后写了100行的状压DP?? 疯狂特判,一算极限时间复杂度过不了aaa!! 然而还是过了....QAQ 所以我定的状态是待转移的位置的前三位,用6位二进制 ...

  9. 【11.8校内测试】【倒计时2天】【状压DP】【随机化?/暴力小模拟】

    Solution 数据范围疯狂暗示状压,可是一开始发现状态特别难受. 将每一层的奇偶性状压,预处理所有状态的奇偶性.每一层的输入代表的其实可以是下一层某个点可以被从这一层哪些点转移到. 所以枚举每个状 ...

随机推荐

  1. ahttp

    # -*- coding: utf-8 -*- # @Time : 2018/8/20 14:35 # @Author : cxa # @File : chttp.py # @Software: Py ...

  2. 批量生成AWR报告(转载总结)

    [前提] 对Oracle进行性能分析其中一个“帮手”就是Oracle的AWR报告 PS:Oracle的企业版才有AWR报告,标准版是没有的{可以导出来,但是没有数据显示} [需求] 当需要针对某个月的 ...

  3. Mac——mac安装软件

    命令行: perl: curl -L http://xrl.us/installperlosx | bash 参考资料: https://blog.csdn.net/yuxin6866/article ...

  4. Zabbix3.0 API调用

    Zabbix API 是什么? API简单来说是服务对外开放的一个接口,用户通过该接口传递请求,完成操作.API的背后是一组方法的集合,这些方法实现了服务对应的不同功能,调用API实际上就是换了一种方 ...

  5. chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息

    chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接. 使用语法:chkconfig [--ad ...

  6. Floyd_Warshall(任意两点之间的最短路)

    /* O(V^3) 案例: 1 2 2 1 3 5 2 3 1 */ #include <cstdio>#include <iostream>using namespace s ...

  7. Shell语言系列之一:文件处理

    前言 &nbsp 标准输入/输出可能是软件工具设计原则里最基本的观念了.有很多UNIX程序都遵循这一设计历练.默认情况下,他们会读取标准输入,写入标准输出,并将错误信息传递给标准错误输出. & ...

  8. OPENSSL问题,使用fsockopen()函数提示错误

    环境配置 系统环境 CentOS7.2WDCP v3.2.2 lanmp PHP 多版本 指定使用5.6 OpenSSL 1.0.2h  3 May 2016 php.ini相关设置allow_url ...

  9. go-互斥锁及原子函数

    用于解决并发函数的竞争状态问题... package main import ( "fmt" "runtime" "sync" " ...

  10. js 利用事件委托解决mousedown中的click

    有一个需求是这样的: 父元素div绑定一个mousedown事件,子元素a绑定一个click事件. 看解构: <div id="nav"> <a href=&qu ...