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;
- vvpii g(1 + N);
- for (int i = 1; i < N; ++i) { // храню ещё и номер ребра
- int u, v;
- cin >> u >> v;
- g[u].pb({v,i});
- g[v].pb({u,i});
- }
- int M;
- cin >> M;
- vpii m(M); //теперь путь рёбер не из пар(a,b),а из чисел
- forn(i, M) cin >> m[i].fi >> m[i].se; // путь из fi в se
- vvi m_edge(M);
- vi path; // путь содержит рёбра (a,b),где a<b
- function<void(int, int, int, int)> dfs = [&](int i, int u, int finish, int p) {
- if (u == finish) {
- for (int x : path) m_edge[i].pb(x);
- }
- for (pii x : g[u]) {
- int to=x.fi;
- int z=x.se;
- if (to == p) continue;
- path.pb(z);
- dfs(i, to, finish, u);
- path.pop_back();
- }
- };
- forn(i, M) dfs(i, m[i].fi, m[i].se, -1);
- ll bad = 0;
- for (int mask = 1; mask < (1 << M); ++mask) {
- int cnt_1 = 0;
- vector<int> used(N,0); // used[i]=0/1 -ребро исп/неисп
- for (int j = 0; j < M; ++j) {
- if (mask & (1 << j)) {
- cnt_1++;
- for (int x : m_edge[j]){
- used[x]=1;
- }
- }
- }
- int cur_pow = (N - 1) - accumulate(all(used),0);
- 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