fork(1) download
  1. // ~~ icebear ~~
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. typedef pair<int, int> ii;
  7. typedef pair<int, ii> iii;
  8.  
  9. template<class T>
  10. bool minimize(T &a, const T &b) {
  11. if (a > b) return a = b, true;
  12. return false;
  13. }
  14.  
  15. template<class T>
  16. bool maximize(T &a, const T &b) {
  17. if (a < b) return a = b, true;
  18. return false;
  19. }
  20.  
  21. #define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
  22. #define FORR(i,a,b) for(int i=(a); i>=(b); --i)
  23. #define REP(i, n) for(int i=0; i<(n); ++i)
  24. #define RED(i, n) for(int i=(n)-1; i>=0; --i)
  25. #define MASK(i) (1LL << (i))
  26. #define BIT(S, i) (((S) >> (i)) & 1)
  27. #define mp make_pair
  28. #define pb push_back
  29. #define fi first
  30. #define se second
  31. #define all(x) x.begin(), x.end()
  32. #define task "icebear"
  33. /*END OF TEMPLATE. ICEBEAR AND THE CAT WILL WIN VOI26 */
  34.  
  35. const int MOD = 1e9 + 7;
  36. const int inf = 1e9 + 27092008;
  37. const ll INF = 1e18 + 27092008;
  38. const int N = 2e5 + 5;
  39. int numNode, numQuery;
  40. vector<int> G[N];
  41. vector<ii> virG[N];
  42. int P[N][20], h[N], f[N], s[N], tin[N], tout[N], timer;
  43.  
  44. void dfs(int u) {
  45. tin[u] = ++timer;
  46. for(int v : G[u]) if (v != P[u][0]) {
  47. h[v] = h[u] + 1;
  48. P[v][0] = u;
  49. dfs(v);
  50. }
  51. tout[u] = timer;
  52. }
  53.  
  54. int LCA(int u, int v) {
  55. if (h[u] < h[v]) swap(u, v);
  56. int s = h[u] - h[v];
  57. RED(j, 20) if (BIT(s, j))
  58. u = P[u][j];
  59. if (u == v) return u;
  60. RED(j, 20) if (P[u][j] != P[v][j]) {
  61. u = P[u][j];
  62. v = P[v][j];
  63. }
  64. return P[u][0];
  65. }
  66.  
  67. bool inSubtree(int u, int v) {
  68. return tin[u] <= tin[v] && tout[v] <= tout[u];
  69. }
  70.  
  71. int ans, sumNode;
  72. void calcTree(int u) {
  73. for(ii v : virG[u]) {
  74. calcTree(v.fi);
  75. f[u] = (f[u] + f[v.fi]) % MOD;
  76. s[u] = (s[u] + s[v.fi]) % MOD;
  77. ans = (ans + 1LL * (sumNode - f[v.fi]) * f[v.fi] % MOD * v.se % MOD) % MOD;
  78. }
  79. }
  80.  
  81. int buildTree(int K, vector<int> &query) {
  82. sort(all(query), [&](const int &x, const int &y){return tin[x] < tin[y];});
  83. query.resize(unique(all(query)) - query.begin());
  84. K = (int)query.size();
  85.  
  86. sumNode = 0;
  87. vector<int> initNode = query;
  88. for(int &u : initNode) {
  89. f[u] = u;
  90. s[u] = 1;
  91. sumNode += u;
  92. }
  93.  
  94. FOR(i, 0, K - 2) query.pb(LCA(query[i], query[i + 1]));
  95. sort(all(query), [&](const int &x, const int &y){return tin[x] < tin[y];});
  96. query.resize(unique(all(query)) - query.begin());
  97.  
  98. stack<int> st;
  99. st.push(query[0]);
  100. FOR(i, 1, (int)query.size() - 1) {
  101. while(!inSubtree(st.top(), query[i])) st.pop();
  102. virG[st.top()].emplace_back(query[i], h[query[i]] - h[st.top()]);
  103. st.push(query[i]);
  104. }
  105.  
  106. ans = 0;
  107. calcTree(query[0]);
  108.  
  109. for(int &u : query) {
  110. f[u] = s[u] = 0;
  111. virG[u].clear();
  112. }
  113.  
  114. cerr << ans << '\n';
  115. return ans;
  116. }
  117.  
  118. void init(void) {
  119. cin >> numNode >> numQuery;
  120. FOR(i, 2, numNode) {
  121. int u, v;
  122. cin >> u >> v;
  123. G[u].pb(v);
  124. G[v].pb(u);
  125. }
  126. }
  127.  
  128. void process(void) {
  129. dfs(1);
  130. FOR(j, 1, 19) FOR(i, 1, numNode)
  131. P[i][j] = P[P[i][j - 1]][j - 1];
  132. while(numQuery--) {
  133. int K; cin >> K;
  134. vector<int> query(K);
  135. for(int &u : query) cin >> u;
  136. cout << buildTree(K, query) << '\n';
  137. }
  138. }
  139.  
  140. int main() {
  141. ios_base::sync_with_stdio(0);
  142. cin.tie(0); cout.tie(0);
  143. if (fopen(task".inp", "r")) {
  144. freopen(task".inp", "r", stdin);
  145. freopen(task".out", "w", stdout);
  146. }
  147. int tc = 1;
  148. // cin >> tc;
  149. while(tc--) {
  150. init();
  151. process();
  152. }
  153. return 0;
  154. }
  155.  
Success #stdin #stdout 0.01s 14164KB
stdin
Standard input is empty
stdout
Standard output is empty