Advertisement
CooBin

cliquegraph

Jul 23rd, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void read(int &X){
  5.     char c;
  6.     do { c = getchar(); } while (!isalnum(c));
  7.     X = c - '0';
  8.     while (isalnum(c = getchar())) X = X * 10 + c - '0';
  9. }
  10.  
  11. const int N = 2500 + 10;
  12. const int M = 5000 + 10;
  13.  
  14. int n, m;
  15. vector<int > adj[N + M];
  16.  
  17. int dist[N + M];
  18. queue<int > Q;
  19.  
  20. void bfs(int root){
  21.     memset(dist, 0x3f3f3f3f, sizeof dist);
  22.     dist[root] = 0;
  23.     while (!Q.empty()) Q.pop();
  24.     Q.push(root);
  25.  
  26.     while (!Q.empty()){
  27.         int u = Q.front(); Q.pop();
  28.         for (int v : adj[u]){
  29.             if (dist[v] != 0x3f3f3f3f) continue;
  30.             dist[v] = dist[u] + 1;
  31.             Q.push(v);
  32.         }
  33.     }
  34. }
  35.  
  36. void solve(){
  37.     long long ans = 0;
  38.  
  39.     for (int i = 1; i <= n; i++){
  40.         bfs(i);
  41.         for (int j = 1; j <= n; j++)
  42.             ans += 1LL * dist[j];
  43.     }
  44.     ans /= 4;
  45.     cout << ans << '\n';
  46. }
  47.  
  48. int main(){
  49.     // freopen("cliquegraph.in", "r", stdin);
  50.     // freopen("cliquegraph.out", "w", stdout);
  51.     read(n);
  52.     read(m);
  53.     for (int i = 1; i <= m; i++){
  54.         int s;
  55.         read(s);
  56.         for (int j = 1; j <= s; j++){
  57.             int u; read(u); u++;
  58.             adj[u].push_back(i + n);
  59.             adj[i + n].push_back(u);
  60.         }
  61.     }
  62.     solve();
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement