Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define isz(x) int(x.size())
- #define all(x) x.begin(),x.end()
- #define ll long long
- #define pii pair<int,int>
- #define fi first
- #define se second
- #define isz(x) int(x.size())
- #define ull unsigned long long
- #define vi vector<int>
- #define vll vector<ll>
- #define vch vector<char>
- #define vvch vector<vch>
- #define vvi vector<vector<int>>
- #define vvvi vector<vvi>
- #define vvll vector<vector<ll>>
- #define vpii vector<pair<int,int>>
- #define vvpii vector<vpii>
- #define forn(i,n) for(int i=0;i<(int)n;++i)
- #define pb push_back
- int n;
- using namespace std;
- const int UNDEF = -1;
- const int MOD = 1e9 + 7;
- const int INF = 1e9;
- int main() {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- //freopen("input.txt", "rt", stdin);
- //freopen("output.txt", "wt", stdout);
- int N;
- cin >> N;
- vvi g(1 + N);
- for (int i = 1; i < N; ++i) {
- int u, v;
- cin >> u >> v;
- g[u].pb(v);
- g[v].pb(u);
- }
- int M;
- cin >> M;
- vpii m(M);
- forn(i, M) cin >> m[i].fi >> m[i].se; // путь из fi в se
- vvpii m_edge(M);
- vpii path; // путь содержит рёбра (a,b),где a<b
- function<void(int, int, int, int)> dfs = [&](int i, int u, int finish, int p) {
- if (p != -1) {
- if (u > p) path.pb({ p,u });
- else path.pb({ u,p });
- }
- if (u == finish) {
- for (pii x : path) m_edge[i].pb(x);
- }
- for (int to : g[u]) {
- if (to == p) continue;
- dfs(i, to, finish, u);
- }
- if(!path.empty()) path.pop_back();
- };
- forn(i, M) dfs(i, m[i].fi, m[i].se, -1); // храню рёбра на пути от fi к se i-ого запроса
- ll bad = 0;
- set<pii> s; // s - будет содержать всё рёбра i,j,..k - запросов вместе
- for (int mask = 1; mask < (1 << M); ++mask) {
- int cnt_1 = 0;
- s.clear();
- for (int j = 0; j < M; ++j) {
- if (mask & (1 << j)) {
- cnt_1++;
- for (pii x : m_edge[j]) s.insert(x);
- }
- }
- int cur_pow = (N - 1) - isz(s);
- if (cnt_1 & 1) bad += (1LL << cur_pow);
- else bad -= (1LL << cur_pow);
- }
- ll all = (1LL << (N - 1));
- cout << all - bad;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement