记录编号 329075 评测结果 AAAAAAAAA
题目名称 暗之链锁 最终得分 100
用户昵称 GravatarTiny 是否通过 通过
代码语言 C++ 运行时间 0.851 s
提交时间 2016-10-24 20:32:27 内存使用 93.22 MiB
显示代码纯文本
  1. #include <stdio.h>
  2.  
  3. const int MAXS = 100 * 1024 * 1024;
  4. int buf_cnt = 0;
  5. char read_buf[MAXS];
  6.  
  7. template <typename T> inline void Read_buf(T &num) {
  8. num = 0; bool minus = false;
  9. while (read_buf[buf_cnt] < '-' || read_buf[buf_cnt] > '9') ++buf_cnt;
  10. if (read_buf[buf_cnt] == '-') { minus = true; ++buf_cnt; }
  11. while (read_buf[buf_cnt] >= '0' && read_buf[buf_cnt] <= '9')
  12. num = num * 10 + read_buf[buf_cnt++] - '0';
  13. if (minus) num = -num;
  14. }
  15.  
  16. template <typename T> inline void Read(T &num) {
  17. num = 0; char ch; bool minus = false;
  18. while (ch = getchar(), ch < '-' || ch > '9');
  19. if (ch == '-') { minus = true; ch = getchar(); }
  20. while (num = num * 10 + ch - '0', ch = getchar(), ch >= '0' && ch <= '9');
  21. if (minus) num = -num;
  22. }
  23.  
  24. const size_t MAXN = 100000 + 10;
  25.  
  26. int n, m;
  27. struct Edge { int v, ne; } E[MAXN << 1];
  28. int head[MAXN], tot_e = 0;
  29. struct Node { int fa, ch, sz, dep, dfn, top; } N[MAXN];
  30. int s[MAXN];
  31.  
  32. inline void Insert(const int &u, const int &v) {
  33. E[++tot_e] = (Edge){v, head[u]}; head[u] = tot_e;
  34. }
  35.  
  36. inline void DFS(const int &rt) {
  37. N[rt].sz = 1; N[rt].ch = 0;
  38. for (int i = head[rt], v; i; i = E[i].ne) {
  39. v = E[i].v;
  40. if (N[v].dep) continue;
  41. N[v].fa = rt;
  42. N[v].dep = N[rt].dep + 1;
  43. DFS(v);
  44. N[rt].sz += N[v].sz;
  45. if (N[rt].sz > N[N[rt].ch].sz) N[rt].ch = v;
  46. }
  47. }
  48.  
  49. inline void DFS(const int &rt, const int &t) {
  50. static int dfs_time;
  51. N[rt].top = t; N[rt].dfn = ++dfs_time;
  52. if (N[rt].ch) DFS(N[rt].ch, t);
  53. for (int i = head[rt], v; i; i = E[i].ne) {
  54. v = E[i].v;
  55. if (v != N[rt].fa && v != N[rt].ch) DFS(v, v);
  56. }
  57. }
  58.  
  59. inline void Set_Mark(int a, int b) {
  60. while (N[a].top != N[b].top) {
  61. if (N[N[a].top].dep < N[N[b].top].dep)
  62. a ^= b ^= a ^= b;
  63. ++s[N[N[a].top].dfn]; --s[N[a].dfn + 1];
  64. a = N[N[a].top].fa;
  65. }
  66. if (a == b) return;
  67. if (N[a].dep > N[b].dep) a ^= b ^= a ^= b;
  68. ++s[N[N[a].ch].dfn]; --s[N[b].dfn + 1];
  69. }
  70.  
  71. int main() {
  72. #define SUBMIT
  73. #ifdef SUBMIT
  74. freopen("yam.in", "r", stdin);
  75. freopen("yam.out", "w", stdout);
  76. fread(read_buf, 1, MAXS, stdin);
  77. #endif
  78.  
  79. Read_buf(n); Read_buf(m);
  80. int u, v;
  81. for (int i = 1; i < n; ++i) {
  82. Read_buf(u); Read_buf(v);
  83. Insert(u, v); Insert(v, u);
  84. }
  85. N[1].dep = 1; DFS(1); DFS(1, 1);
  86. for (int i = 1; i <= m; ++i) {
  87. Read_buf(u); Read_buf(v);
  88. Set_Mark(u, v);
  89. }
  90. int ans = 0;
  91. for (int i = 2; i <= n; ++i) {
  92. s[i] += s[i - 1];
  93. if (s[i] == 0) ans += m;
  94. else if (s[i] == 1) ++ans;
  95. }
  96. printf("%d\n", ans);
  97.  
  98. #ifndef SUBMIT
  99. puts("\n--------------------");
  100. getchar(); getchar();
  101. #else
  102. fclose(stdin); fclose(stdout);
  103. #endif
  104. return 0;
  105. }