[关闭]
@ZCDHJ 2018-09-08T06:31:12.000000Z 字数 1269 阅读 763

PA2014 Kuglarz

最小生成树


BZOJ

为前 个杯子底下小球总数的奇偶性

那么每次告诉一个区间 中的总数的奇偶性就等于告诉了 ,这样子的话只要知道其中的一个就能知道另外一个。

为了能够猜出每个杯子底下是否有球,就得求出每一个 也就是求出所有的 。这样,对于每个 连一条边权为 的无向边,然后以 为根跑 MST 就行了。

貌似要用 Prim 以及要开 long long

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <queue>
  5. #define int long long
  6. const int INF = 0x3f3f3f3f;
  7. const int MAXN = 2000 + 5;
  8. int N, Ans, Edge;
  9. int D[MAXN], FT[MAXN];
  10. bool Vis[MAXN];
  11. std::priority_queue < std::pair < int, int > > Q;
  12. struct Linker
  13. {
  14. int to, w, nxt;
  15. Linker(){}
  16. Linker(int x, int y, int z)
  17. {
  18. to = y;
  19. w = z;
  20. nxt = FT[x];
  21. }
  22. } E[4200000 + 5];
  23. inline int read()
  24. {
  25. register int x = 0, v = 1;
  26. register char ch = getchar();
  27. while(!isdigit(ch))
  28. {
  29. if(ch == '-') v = -1;
  30. ch = getchar();
  31. }
  32. while(isdigit(ch))
  33. {
  34. x = x * 10 + ch - '0';
  35. ch = getchar();
  36. }
  37. return x * v;
  38. }
  39. inline void AddEdge(int x, int y, int z)
  40. {
  41. E[++Edge] = Linker(x, y, z);
  42. FT[x] = Edge;
  43. }
  44. void Prim()
  45. {
  46. memset(D, INF, sizeof(D));
  47. D[0] = 0;
  48. Q.push(std::make_pair(0, 0));
  49. for(int i = 1; i <= N; ++i)
  50. {
  51. std::pair < int, int > from;
  52. do
  53. {
  54. from = Q.top();
  55. Q.pop();
  56. }
  57. while(Vis[from.second]);
  58. Vis[from.second] = 1;
  59. for(int k = FT[from.second]; k; k = E[k].nxt)
  60. {
  61. int to = E[k].to, w = E[k].w;
  62. if(!Vis[to] && D[to] > w)
  63. {
  64. D[to] = w;
  65. Q.push(std::make_pair(-w, to));
  66. }
  67. }
  68. }
  69. for(int i = 1; i <= N; ++i) Ans += D[i];
  70. }
  71. signed main()
  72. {
  73. N = read();
  74. for(int i = 1; i <= N; ++i)
  75. {
  76. for(int j = i; j <= N; ++j)
  77. {
  78. int w = read();
  79. AddEdge(i - 1, j , w);
  80. AddEdge(j, i - 1, w);
  81. }
  82. }
  83. Prim();
  84. printf("%lld\n", Ans);
  85. return 0;
  86. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注