|
| 1 | +/* |
| 2 | +USACO 2021 Sum of Distances |
| 3 | +- The distance to a K-tuple (v[1], v[2], ..., v[K]) from (1, 1, ..., 1) is |
| 4 | + min(max(1 -> v[i] where the path is even), max(1 -> v[i] where the path is odd)) |
| 5 | +- We can use a segtree/BIT + DP to compute sum(max(1 -> v[i] where the path is even) for each tuple) |
| 6 | + - First, sort the graphs by size |
| 7 | + - Let dp[i][j] be the number of i-tuples (i.e. from the first i graphs) with *even* distance j |
| 8 | + - First, compute dp[i][j] = sum(dp[i - 1][l] where l <= j) |
| 9 | + - Next, update dp[i][j] = (No. of even paths from node 1 in graph i of length < j) * dp[i - 1][j] |
| 10 | + - dp[K][j] will be the number of K-tuples with *even* distance j, so we can just iterate through dp[K] |
| 11 | +- Similar logic holds for the odd-distance tuples |
| 12 | +- What if a tuple can be reached in both even *and* odd distance though? |
| 13 | + - If that happens, then we overcount the path with length max(even[i], odd[i] for each i), so just do |
| 14 | + the above DP again but with maximums instead of evens/odds, and subtract that value from our answer |
| 15 | + - This is kinda like PIE |
| 16 | +- Complexity: O(sum(N) log sum(N)) |
| 17 | +*/ |
| 18 | + |
| 19 | +#include <bits/stdc++.h> |
| 20 | +typedef long long ll; |
| 21 | +using namespace std; |
| 22 | + |
| 23 | +const ll MOD = 1e9 + 7; |
| 24 | + |
| 25 | +int k, n[50001], order[50001]; |
| 26 | +vector<ll> segtree[50001]; |
| 27 | + |
| 28 | +ll query(int a, int b, int tree, int node, int l, int r) { |
| 29 | + if (l > b || r < a) return 0; |
| 30 | + if (l >= a && r <= b) return segtree[tree][node]; |
| 31 | + int mid = (l + r) / 2; |
| 32 | + return (query(a, b, tree, node * 2, l, mid) + query(a, b, tree, node * 2 + 1, mid + 1, r)) % MOD; |
| 33 | +} |
| 34 | + |
| 35 | +void update(int pos, ll val, int tree, int node, int l, int r) { |
| 36 | + if (l == r) segtree[tree][node] = (segtree[tree][node] + val) % MOD; |
| 37 | + else { |
| 38 | + int mid = (l + r) / 2; |
| 39 | + if (pos > mid) update(pos, val, tree, node * 2 + 1, mid + 1, r); |
| 40 | + else update(pos, val, tree, node * 2, l, mid); |
| 41 | + segtree[tree][node] = (segtree[tree][node * 2] + segtree[tree][node * 2 + 1]) % MOD; |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +void reset(int tree) { segtree[tree].clear(); segtree[tree].resize(8 * n[tree]); } |
| 46 | + |
| 47 | +ll compute_sum(vector<pair<int, int>> dists) { |
| 48 | + vector<vector<int>> graphs(k); |
| 49 | + for (pair<int, int> i : dists) graphs[i.second].push_back(i.first); |
| 50 | + |
| 51 | + reset(order[0]); |
| 52 | + for (int i : graphs[order[0]]) update(i, 1, order[0], 1, 0, 2 * n[order[0]] - 1); |
| 53 | + |
| 54 | + for (int i = 1; i < k; i++) { |
| 55 | + reset(order[i]); |
| 56 | + vector<int> pref(2 * n[order[i]], 0); |
| 57 | + for (int j : graphs[order[i]]) { |
| 58 | + update(j, query(0, j, order[i - 1], 1, 0, 2 * n[order[i - 1]] - 1), order[i], 1, 0, 2 * n[order[i]] - 1); |
| 59 | + pref[j]++; |
| 60 | + } |
| 61 | + for (int j = 1; j < 2 * n[order[i]]; j++) { |
| 62 | + pref[j] += pref[j - 1]; |
| 63 | + update(j, pref[j - 1] * query(j, j, order[i - 1], 1, 0, 2 * n[order[i - 1]] - 1) % MOD, order[i], 1, 0, 2 * n[order[i]] - 1); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + ll ans = 0; |
| 68 | + for (int i = 1; i < 2 * n[order[k - 1]]; i++) |
| 69 | + ans = (ans + query(i, 2 * n[order[k - 1]] - 1, order[k - 1], 1, 0, 2 * n[order[k - 1]] - 1)) % MOD; |
| 70 | + return ans; |
| 71 | +} |
| 72 | + |
| 73 | +int main() { |
| 74 | + cin.tie(0)->sync_with_stdio(0); |
| 75 | + cin >> k; |
| 76 | + vector<pair<int, int>> even, odd, mx; |
| 77 | + for (int i = 0; i < k; i++) { |
| 78 | + order[i] = i; |
| 79 | + int m; |
| 80 | + cin >> n[i] >> m; |
| 81 | + vector<vector<int>> graph(2 * n[i]); |
| 82 | + vector<int> visited(2 * n[i]); |
| 83 | + while (m--) { |
| 84 | + int u, v; |
| 85 | + cin >> u >> v; |
| 86 | + u--, v--; |
| 87 | + graph[u].push_back(n[i] + v); |
| 88 | + graph[v].push_back(n[i] + u); |
| 89 | + graph[n[i] + u].push_back(v); |
| 90 | + graph[n[i] + v].push_back(u); |
| 91 | + } |
| 92 | + // BFS to get even and odd distances |
| 93 | + visited[0] = 1; |
| 94 | + queue<int> q; |
| 95 | + q.push(0); |
| 96 | + while (q.size()) { |
| 97 | + int curr = q.front(); |
| 98 | + q.pop(); |
| 99 | + for (int j : graph[curr]) if (!visited[j]) { |
| 100 | + visited[j] = visited[curr] + 1; |
| 101 | + q.push(j); |
| 102 | + } |
| 103 | + } |
| 104 | + // Append to the distance lists |
| 105 | + for (int j = 0; j < n[i]; j++) { |
| 106 | + if (visited[j]) even.push_back({visited[j] - 1, i}); |
| 107 | + if (visited[n[i] + j]) odd.push_back({visited[n[i] + j] - 1, i}); |
| 108 | + if (visited[j] && visited[n[i] + j]) mx.push_back({max(visited[j], visited[n[i] + j]) - 1, i}); |
| 109 | + } |
| 110 | + } |
| 111 | + sort(order, order + k, [](int a, int b) { return n[a] < n[b]; }); |
| 112 | + cout << (compute_sum(even) + compute_sum(odd) - compute_sum(mx) + MOD) % MOD; |
| 113 | + return 0; |
| 114 | +} |
0 commit comments