Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <map>
- #include <set>
- #include <ctime>
- #include <queue>
- using namespace std;
- typedef long long ll;
- inline int sci() {int a; scanf("%d", &a); return a;}
- #define nm 1001
- vector<int> g[nm];
- ll d[nm][nm], cnt[nm][nm], sum[nm][nm];
- bool used[nm];
- struct state {
- int v;
- int cnt;
- ll sum;
- ll w_sum;
- };
- int main() {
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- int n =sci(), m =sci();
- for (int i =0 ; i < m; ++i) {
- int a = sci(), b = sci();
- g[a].push_back(b);
- g[b].push_back(a);
- }
- for (int i = 1; i <= n; ++i) sort(g[i].begin(), g[i].end());
- for (int root = 1; root <= n; ++root) {
- memset(used, 0, sizeof used);
- used[root] = 1;
- queue<state> q;
- state now = {root, 1, root, root};
- q.push(now);
- while (!q.empty()) {
- state prev = q.front(); q.pop();
- for (int j = 0, to; j < g[prev.v].size(); ++j) {
- to = g[prev.v][j];
- if (used[to]) continue;
- state now = {to, prev.cnt + 1, prev.sum + to, prev.w_sum + (prev.cnt + 1) * to };
- used[to] = true;
- d[root][to] = now.w_sum;
- cnt[root][to] = now.cnt;
- sum[root][to] = now.sum;
- q.push(now);
- }
- }
- }
- int R = sci();
- map<ll, int> memo;
- for (int i = 0; i < R; ++i) {
- int k = sci();
- ll def = 0;
- int prv = sci(), nxt = sci();
- def += d[prv][nxt];
- ll c = cnt[prv][nxt] - 1;
- prv = nxt;
- for (int i = 2; i< k; ++i) {
- nxt = sci();
- def -= (c + 1) * prv;
- def += d[prv][nxt] + sum[prv][nxt] * c;
- c += cnt[prv][nxt] - 1;
- prv = nxt;
- }
- if (!memo.count(def)) {
- printf("%lld\n", def);
- memo[def]++;
- } else {
- memo[def]++;
- printf("%lld#%d\n", def, memo[def]);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement