Advertisement
Guest User

Untitled

a guest
Mar 25th, 2012
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <vector>
  7. #include <map>
  8. #include <set>
  9. #include <ctime>
  10. #include <queue>
  11. using namespace std;
  12. typedef long long ll;
  13. inline int sci() {int a; scanf("%d", &a); return a;}
  14. #define nm 1001
  15. vector<int> g[nm];
  16. ll d[nm][nm], cnt[nm][nm], sum[nm][nm];
  17. bool used[nm];
  18.  
  19. struct state {
  20.     int v;
  21.     int cnt;
  22.     ll sum;
  23.     ll w_sum;
  24. };
  25.  
  26.  
  27. int main() {
  28. #ifdef _DEBUG
  29.     freopen("input.txt", "r", stdin);
  30.     freopen("output.txt", "w", stdout);
  31. #endif
  32.     int n =sci(), m =sci();
  33.     for (int i =0 ; i < m; ++i) {
  34.         int a = sci(), b = sci();
  35.         g[a].push_back(b);
  36.         g[b].push_back(a);
  37.     }
  38.     for (int i = 1; i <= n; ++i) sort(g[i].begin(), g[i].end());
  39.     for (int root = 1; root <= n; ++root) {
  40.         memset(used, 0, sizeof used);
  41.         used[root] = 1;
  42.         queue<state> q;
  43.         state now = {root, 1, root, root};
  44.         q.push(now);
  45.         while (!q.empty()) {
  46.             state prev = q.front(); q.pop();
  47.             for (int j = 0, to; j < g[prev.v].size(); ++j) {
  48.                 to = g[prev.v][j];
  49.                 if (used[to]) continue;
  50.                 state now  = {to, prev.cnt + 1, prev.sum + to, prev.w_sum + (prev.cnt + 1) * to };
  51.                 used[to] = true;
  52.                 d[root][to] = now.w_sum;
  53.                 cnt[root][to] = now.cnt;
  54.                 sum[root][to] = now.sum;
  55.                 q.push(now);
  56.             }          
  57.         }
  58.     }
  59.     int R = sci();
  60.     map<ll, int> memo;
  61.     for (int i = 0; i < R; ++i) {
  62.         int k = sci();
  63.         ll def = 0;
  64.         int prv = sci(), nxt = sci();
  65.         def += d[prv][nxt];
  66.         ll c = cnt[prv][nxt] - 1;
  67.         prv = nxt;
  68.  
  69.         for (int i = 2; i< k; ++i) {
  70.             nxt = sci();
  71.             def -= (c + 1) * prv;
  72.             def += d[prv][nxt] + sum[prv][nxt] * c;
  73.             c += cnt[prv][nxt] - 1;
  74.             prv = nxt;
  75.         }
  76.         if (!memo.count(def)) {
  77.             printf("%lld\n", def);
  78.             memo[def]++;
  79.         } else {
  80.             memo[def]++;
  81.             printf("%lld#%d\n", def, memo[def]);
  82.         }
  83.     }
  84.     return 0;  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement