Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.68 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include<cstdio>
  3. #include<vector>
  4. #include<string>
  5. #include<iostream>
  6. #include<algorithm>
  7. #include<map>
  8. #include<iterator>
  9. #include<set>
  10. #include<stack>
  11. #include<queue>
  12. #include<fstream>
  13. #include<iomanip>
  14. #include <unordered_map>
  15. #include <unordered_set>
  16. #include <numeric>
  17. #include<cmath>
  18. #include<list>
  19. #include <sstream>
  20. using namespace std;
  21.  
  22. #define FOR(i,a,b) for (int i = (a); i < (b); i++)
  23. #define RFOR(i,b,a) for (int i = (b)-1; i >= (a); i--)
  24. #define ITER(it,a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); it++)
  25. #define FILL(a,value) memset(a, value, sizeof(a))
  26.  
  27. #define SZ(a) (int)a.size()
  28. #define ALL(a) a.begin(), a.end()
  29. #define PB push_back
  30. #define MP make_pair
  31.  
  32. typedef long long LL;
  33. typedef vector<int> VI;
  34. typedef pair<int, int> PII;
  35.  
  36. const double PI = acos(-1.0);
  37. const int INF = 1000 * 1000 * 1000 + 7;
  38. const LL LINF = INF * (LL)INF;
  39.  
  40. const int SIZE = 1000 * 1000;
  41. vector<int> g[SIZE];
  42. int mit[SIZE];
  43. int numb[SIZE][2];
  44. PII e[SIZE];
  45. int n, m;
  46.  
  47. void dfs(int v)
  48. {
  49.     if (mit[v])
  50.     {
  51.         return;
  52.     }
  53.  
  54.     mit[v] = 1;
  55.     FOR(i, 0, g[v].size())
  56.     {
  57.         dfs(g[v][i]);
  58.     }
  59. }
  60.  
  61. bool isBinded()
  62. {
  63.     dfs(0);
  64.     FOR(i, 0, n)
  65.     {
  66.         if (!mit[i]) {
  67.             return false;
  68.         }
  69.     }
  70.     return true;
  71. }
  72.  
  73. int main()
  74. {
  75.     //freopen("in.txt", "r", stdin);
  76.     ios::sync_with_stdio(false); cin.tie(0);
  77.  
  78.     cin >> n >> m;
  79.     FOR(i, 0, m)
  80.     {
  81.         int x, y;
  82.         cin >> x >> y;
  83.         x--; y--;
  84.         e[i] = MP(x, y);
  85.         g[x].push_back(y);
  86.         g[y].push_back(x);
  87.     }
  88.  
  89.     if (!isBinded())
  90.     {
  91.         cout << 0 << endl;
  92.         return 0;
  93.     }
  94.  
  95.     int numbOdd = 0;
  96.     FOR(i, 0, n)
  97.     {
  98.         bool hasLoop = 0;
  99.         FOR(j, 0, g[i].size())
  100.         {
  101.             if (i == g[i][j])
  102.             {
  103.                 hasLoop = 1;
  104.             }
  105.  
  106.             bool odd = g[g[i][j]].size() & 1;
  107.             numb[i][odd]++;
  108.         }
  109.  
  110.         if (hasLoop)
  111.         {
  112.             numb[i][g[i].size() & 1]--;
  113.         }
  114.  
  115.         if (g[i].size() & 1){
  116.             numbOdd++;
  117.         }
  118.     }
  119.  
  120.     vector<int> edgs(3, 0);
  121.     FOR(i, 0, m)
  122.     {
  123.         int sa = g[e[i].first].size() & 1,
  124.             sb = g[e[i].second].size() & 1;
  125.         edgs[sa + sb]++;
  126.     }
  127.  
  128.     LL res = 0;
  129.     //FOR(i, 0, n)
  130.     //{
  131.     //  cout << i << ": " << numb[i][0] << " " << numb[i][1] << endl;
  132.     //}
  133.  
  134.     //FOR(i, 0, 3)
  135.     //{
  136.     //  cout << edgs[i] << endl;
  137.     //}
  138.  
  139.     FOR(i, 0, m)
  140.     {
  141.         //FOR(j, 0, n)
  142.         //{
  143.         //  cout << j << ": " << numb[j][0] << " " << numb[j][1] << endl;
  144.         //}
  145.         auto temp = edgs;
  146.         int tempNumbOdd = numbOdd;
  147.  
  148.         int sa = g[e[i].first].size() & 1,
  149.             sb = g[e[i].second].size() & 1;
  150.  
  151.         if (e[i].first != e[i].second)
  152.         {
  153.             tempNumbOdd += (sa ? -1 : 1);
  154.         }
  155.  
  156.         if (e[i].first != e[i].second)
  157.         {
  158.             tempNumbOdd += (sb ? -1 : 1);
  159.         }
  160.         numb[e[i].first][sb]--;
  161.         numb[e[i].second][sa]--;
  162.         FOR(j, 0, 2)
  163.         {
  164.             temp[sa + j] -= numb[e[i].first][j];
  165.             temp[sb + j] -= numb[e[i].second][j];
  166.         }
  167.  
  168.         temp[sa + sb]--;
  169.         //cout << sa << " " << sb << endl;
  170.         //cout << e[i].first << " " << e[i].second << ": " << endl;
  171.         //FOR(j, 0, 3)
  172.         //{
  173.         //  cout << temp[j] << " ";
  174.         //}
  175.         //cout << endl;
  176.  
  177.         //cout << tempNumbOdd << endl;
  178.         sa ^= 1;
  179.         sb ^= 1;
  180.         //cout << sa << " " << sb << endl;
  181.         FOR(j, 0, 2)
  182.         {
  183.             temp[sa + j] += numb[e[i].first][j];
  184.             temp[sb + j] += numb[e[i].second][j];
  185.         }
  186.  
  187.         //temp[sa + sb ^ 1]--;
  188.         //temp[sb + sa ^ 1]--;
  189.  
  190.         //cout << e[i].first << " " << e[i].second << ": " << endl;
  191.         //FOR(j, 0, 3)
  192.         //{
  193.         //  cout << temp[j] << " ";
  194.         //}
  195.         //cout << endl;
  196.  
  197.         if (tempNumbOdd == 4)
  198.         {
  199.             res += temp[2];
  200.         }
  201.         else {
  202.             if (tempNumbOdd == 2) {
  203.                 res += temp[2] + temp[1];
  204.             }
  205.             else {
  206.                 if (tempNumbOdd == 0) {
  207.                     res += temp[0] + temp[1];
  208.                 }
  209.             }
  210.         }
  211.  
  212.         sa ^= 1;
  213.         sb ^= 1;
  214.         numb[e[i].first][sb]++;
  215.         numb[e[i].second][sa]++;
  216.     }
  217.  
  218.     cout << res /2 << endl;
  219.     return 0;
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement