Advertisement
KinDeR___

Untitled

Oct 11th, 2023
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 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 = 1; i < N; ++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); //теперь путь рёбер не из пар(a,b),а из чисел
  52. forn(i, M) cin >> m[i].fi >> m[i].se; // путь из fi в se
  53.  
  54. vvi m_edge(M);
  55. vi path; // путь содержит рёбра (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. for (int x : path) m_edge[i].pb(x);
  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.pb(z);
  66. dfs(i, to, finish, u);
  67. path.pop_back();
  68. }
  69. };
  70.  
  71. forn(i, M) dfs(i, m[i].fi, m[i].se, -1);
  72.  
  73. ll bad = 0;
  74. for (int mask = 1; mask < (1 << M); ++mask) {
  75. int cnt_1 = 0;
  76. vector<int> used(N,0); // used[i]=0/1 -ребро исп/неисп
  77. for (int j = 0; j < M; ++j) {
  78. if (mask & (1 << j)) {
  79. cnt_1++;
  80. for (int x : m_edge[j]){
  81. used[x]=1;
  82. }
  83. }
  84. }
  85.  
  86. int cur_pow = (N - 1) - accumulate(all(used),0);
  87. if (cnt_1 & 1) bad += (1LL << cur_pow);
  88. else bad -= (1LL << cur_pow);
  89. }
  90.  
  91. ll all = (1LL << (N - 1));
  92. cout << all - bad;
  93.  
  94. return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement