Advertisement
Guest User

Untitled

a guest
Jan 15th, 2012
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.17 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <cstdarg>
  6. #include <cassert>
  7. #include <algorithm>
  8. #include <vector>
  9. #include <string>
  10.  
  11. using namespace std;
  12.  
  13. #define eprintf(...) fprintf(stderr, __VA_ARGS__)
  14. #define pb push_back
  15. #define mp make_pair
  16. #define TASKNAME ""
  17. #define sz(x) ((int)(x).size())
  18.  
  19. typedef long long ll;
  20. typedef vector<ll> vll;
  21. typedef vector<int> vi;
  22. typedef vector<vi> vvi;
  23. typedef vector<bool> vb;
  24. typedef pair<int, int> pii;
  25.  
  26. #ifdef _WIN32
  27. #define LLD "%I64d"
  28. #elif linux
  29. #define LLD "%lld"
  30. #else
  31. #error Invalid OS...
  32. #endif
  33.  
  34. const int MAXN = 5000;
  35. int n;
  36. char g[MAXN][MAXN];
  37.  
  38. int ty[MAXN];
  39. ll tcnt[4];
  40. ll tg[2][4][4];
  41.  
  42. int main() {
  43.   #ifdef DEBUG
  44.   freopen("std.in", "r", stdin);
  45.   freopen("std.out", "w", stdout);
  46.   #endif
  47.  
  48.   int n, m;
  49.   while (scanf("%d%d", &n, &m) >= 1) {
  50.     memset(g, 1, sizeof g);
  51.  
  52.     vi as(m), bs(m);
  53.     for (int i = 0; i < m; i++) {
  54.       scanf("%d%d", &as[i], &bs[i]), as[i]--, bs[i]--;
  55.       if (as[i] > bs[i]) swap(as[i], bs[i]);
  56.  
  57.       g[as[i]][bs[i]] = g[bs[i]][as[i]] = false;
  58.     }
  59.  
  60.     ll ans = n;
  61.     ans += ll(n) * (n - 1) / 2 - m;
  62.  
  63.     if (n >= 3) {
  64.       ll c3 = ll(n) * (n - 1) * (n - 2) / 6;
  65.       for (int i = 0; i < m; i++) {
  66.         int a = as[i], b = bs[i];
  67.  
  68.         for (int c = 0; c < n; c++) if (a != c && b != c) {
  69.           if (!g[a][c] && !g[b][c]) {
  70.             if (b >= c) continue;
  71.           } else if (!g[a][c]) {
  72.             if (b >= c) continue;
  73.           } else if (!g[b][c]) {
  74.             if (a >= c) continue;
  75.           }
  76.           c3--;
  77.         }
  78.       }
  79.       ans += c3;
  80.     }
  81.  
  82.     if (n >= 4) {
  83.       ll cnts[7];
  84.       memset(cnts, 0, sizeof cnts);
  85.       for (int ed = 0; ed < m; ed++) {
  86.         int a = as[ed], b = bs[ed];
  87.  
  88.         memset(ty, -1, sizeof ty);
  89.         memset(tcnt, 0, sizeof tcnt);
  90.         for (int i = 0; i < n; i++) if (i != a && i != b) {
  91.           ty[i] = !g[a][i] + !g[b][i];
  92.           tcnt[ty[i]]++;
  93.         }
  94.  
  95.         memset(tg, 0, sizeof tg);
  96.         for (int e2 = 0; e2 < m; e2++) {
  97.           int t1 = ty[as[e2]], t2 = ty[bs[e2]];
  98.           if (t1 > t2) swap(t1, t2);
  99.  
  100.           if (t1 >= 0 && t2 >= 0) {
  101.             tg[1][t1][t2]++;
  102.           }
  103.         }
  104.         for (int a = 0; a < 4; a++) {
  105.           ll ccnt = tcnt[a] * (tcnt[a] - 1) / 2;
  106.           tg[0][a][a] = ccnt - tg[1][a][a];
  107.  
  108.           for (int b = a + 1; b < 4; b++) {
  109.             ll ccnt = tcnt[a] * tcnt[b];
  110.             tg[0][a][b] = ccnt - tg[1][a][b];
  111.           }
  112.         }
  113.  
  114.         for (int conn = 0; conn < 2; conn++)
  115.         for (int t1 = 0; t1 < 3; t1++)
  116.         for (int t2 = t1; t2 < 3; t2++) {
  117.           int ccnt = 1 + t1 + t2 + conn;
  118.           assert(1 <= ccnt && ccnt <= 6);
  119.           cnts[ccnt] += tg[conn][t1][t2];
  120.         }
  121.       }
  122.  
  123.       for (int i = 1; i <= 6; i++) {
  124.         assert(cnts[i] % i == 0);
  125.         cnts[i] /= i;
  126.       }
  127.  
  128.       ll c4 = ll(n) * (n - 1) * (n - 2) * (n - 3) / 24;
  129.       for (int i = 1; i <= 6; i++)
  130.         c4 -= cnts[i];
  131.       assert(c4 >= 0);
  132.       ans += c4;
  133.     }
  134.     printf(LLD"\n", ans);
  135.  
  136.     #ifndef DEBUG
  137.     break;
  138.     #endif
  139.   }
  140.   return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement