Advertisement
KinDeR___

Untitled

Oct 12th, 2023
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. #define isz(x) int(x.size())
  4. #define all(x) x.begin(),x.end()
  5. #define ll long long
  6. #define pii pair<int,int>
  7. #define fi first
  8. #define se second
  9. #define isz(x) int(x.size())
  10. #define ull unsigned long long
  11. #define vi vector<int>
  12. #define vll vector<ll>
  13. #define vch vector<char>
  14. #define vvch vector<vch>
  15. #define vvi vector<vector<int>>
  16. #define vvvi vector<vvi>
  17. #define vvll vector<vector<ll>>
  18. #define vpii vector<pair<int,int>>
  19. #define vvpii vector<vpii>
  20. #define forn(i,n) for(int i=0;i<(int)n;++i)
  21. #define pb push_back
  22.  
  23. int n;
  24.  
  25. using namespace std;
  26.  
  27. const int UNDEF = -1;
  28. const int MOD = 1e9 + 7;
  29. const int INF = 1e9;
  30.  
  31. int main() {
  32.  
  33. ios::sync_with_stdio(false);
  34. cin.tie(nullptr);
  35. //freopen("input.txt", "rt", stdin);
  36. //freopen("output.txt", "wt", stdout);
  37.  
  38. int N;
  39. cin >> N;
  40.  
  41. vvpii g(1 + N);
  42. for (int i = 0; i < N-1; ++i) {
  43. int u, v;
  44. cin >> u >> v;
  45. g[u].pb({v,i});
  46. g[v].pb({u,i});
  47. }
  48.  
  49. int M;
  50. cin >> M;
  51. vpii m(M);
  52. forn(i, M) cin >> m[i].fi >> m[i].se; // путь из fi в se
  53.  
  54. vll m_edge(M);
  55. ll path=0; // путь содержит рёбра (a,b),где a<b
  56. function<void(int, int, int, int)> dfs = [&](int i, int u, int finish, int p) {
  57. if (u == finish) {
  58. m_edge[i]=path;
  59. }
  60.  
  61. for (pii x : g[u]) {
  62. int to=x.fi;
  63. int z=x.se;
  64. if (to == p) continue;
  65. path|=(1LL<<z);
  66. dfs(i, to, finish, u);
  67. path&=~(1LL<<z);
  68. }
  69. };
  70.  
  71. forn(i, M) dfs(i, m[i].fi, m[i].se, -1); // храню рёбра на пути от fi к se i-ого запроса
  72.  
  73.  
  74. ll bad = 0;
  75. for (int mask = 1; mask < (1 << M); ++mask) {
  76. int cnt_1 = 0;
  77. ll use_edge=0;
  78. for (int j = 0; j < M; ++j) {
  79. if (mask & (1 << j)) {
  80. cnt_1++;
  81. use_edge|=m_edge[j];
  82. }
  83. }
  84.  
  85. int cur_pow = (N - 1) - __builtin_popcountll(use_edge);
  86. if (cnt_1 & 1) bad += (1LL << cur_pow);
  87. else bad -= (1LL << cur_pow);
  88. }
  89.  
  90. ll all = (1LL << (N - 1));
  91. cout << all - bad;
  92.  
  93. return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement