Guest User

Untitled

a guest
Jan 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #define N 111111
  4. #define M 2222222
  5.  
  6. using namespace std;
  7.  
  8. int ef[M],es[M],first[N],next[M],st[N],c,n,m,k;
  9. bool b[N],cicle;
  10. long long ans;
  11.  
  12. void add(int x,int y){
  13.     next[++c]=first[x];first[x]=c;
  14.     ef[c]=x;es[c]=y;++st[x];
  15.     swap(x,y);
  16.     next[++c]=first[x];first[x]=c;
  17.     ef[c]=x;es[c]=y;++st[x];
  18. }
  19.  
  20. long long sum(long long x){
  21.     if (x<=0) return 0;
  22.     return (x*(x+1))/2;
  23. }
  24.  
  25. void init(void){
  26.     freopen("input.txt","r",stdin);
  27.     freopen("output.txt","w",stdout);
  28.     cin>>n>>m;
  29.     for (int i=1;i<=m;i++){
  30.         int x,y;
  31.         cin>>x>>y;
  32.         add(x,y);
  33.     }
  34. }
  35.  
  36. void dfs(int v){
  37.     if (st[v]==1) cicle=false;
  38.     ++k;
  39.     b[v]=1;
  40.     for (int h=first[v];h;h=next[h]) if (!b[es[h]]) dfs(es[h]);
  41. }
  42.  
  43. void sol(int v){
  44.     k=0;
  45.     cicle=true;
  46.     dfs(v);
  47.     if (!cicle){
  48.         int k1=0,k2=k-1;
  49.         for (int i=1;i<=k;i++){
  50.             ans+=sum((long long)(k1-1));
  51.             ans+=sum((long long)(k2-1));
  52.             ++k1;--k2;
  53.         }
  54.         return;
  55.     }
  56.     ans+=2*k*sum((long long)((k-1)/2-1));
  57.     if (!(k&1)) ans+=(long long)k*((long long)k/2-1);
  58. }
  59.  
  60. int main(void){
  61.     init();
  62.     for (int i=1;i<=n;i++) if (!b[i]) sol(i);
  63.     cout<<ans;
  64.     return 0;
  65. }
Add Comment
Please, Sign In to add comment