Advertisement
KinDeR___

Untitled

Oct 11th, 2023
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 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. vvi g(1 + N);
  42. for (int i = 1; i < N; ++i) {
  43. int u, v;
  44. cin >> u >> v;
  45. g[u].pb(v);
  46. g[v].pb(u);
  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. vvpii m_edge(M);
  55. vpii path; // путь содержит рёбра (a,b),где a<b
  56. function<void(int, int, int, int)> dfs = [&](int i, int u, int finish, int p) {
  57. if (p != -1) {
  58. if (u > p) path.pb({ p,u });
  59. else path.pb({ u,p });
  60. }
  61. if (u == finish) {
  62. for (pii x : path) m_edge[i].pb(x);
  63. }
  64.  
  65. for (int to : g[u]) {
  66. if (to == p) continue;
  67. dfs(i, to, finish, u);
  68. }
  69.  
  70. if(!path.empty()) path.pop_back();
  71. };
  72.  
  73. forn(i, M) dfs(i, m[i].fi, m[i].se, -1); // храню рёбра на пути от fi к se i-ого запроса
  74.  
  75.  
  76. ll bad = 0;
  77. set<pii> s; // s - будет содержать всё рёбра i,j,..k - запросов вместе
  78. for (int mask = 1; mask < (1 << M); ++mask) {
  79. int cnt_1 = 0;
  80. s.clear();
  81. for (int j = 0; j < M; ++j) {
  82. if (mask & (1 << j)) {
  83. cnt_1++;
  84. for (pii x : m_edge[j]) s.insert(x);
  85. }
  86. }
  87.  
  88. int cur_pow = (N - 1) - isz(s);
  89. if (cnt_1 & 1) bad += (1LL << cur_pow);
  90. else bad -= (1LL << cur_pow);
  91. }
  92.  
  93. ll all = (1LL << (N - 1));
  94. cout << all - bad;
  95.  
  96. return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement