Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #define N 111111
- #define M 2222222
- using namespace std;
- int ef[M],es[M],first[N],next[M],st[N],c,n,m,k;
- bool b[N],cicle;
- long long ans;
- void add(int x,int y){
- next[++c]=first[x];first[x]=c;
- ef[c]=x;es[c]=y;++st[x];
- swap(x,y);
- next[++c]=first[x];first[x]=c;
- ef[c]=x;es[c]=y;++st[x];
- }
- long long sum(long long x){
- if (x<=0) return 0;
- return (x*(x+1))/2;
- }
- void init(void){
- freopen("input.txt","r",stdin);
- freopen("output.txt","w",stdout);
- cin>>n>>m;
- for (int i=1;i<=m;i++){
- int x,y;
- cin>>x>>y;
- add(x,y);
- }
- }
- void dfs(int v){
- if (st[v]==1) cicle=false;
- ++k;
- b[v]=1;
- for (int h=first[v];h;h=next[h]) if (!b[es[h]]) dfs(es[h]);
- }
- void sol(int v){
- k=0;
- cicle=true;
- dfs(v);
- if (!cicle){
- int k1=0,k2=k-1;
- for (int i=1;i<=k;i++){
- ans+=sum((long long)(k1-1));
- ans+=sum((long long)(k2-1));
- ++k1;--k2;
- }
- return;
- }
- ans+=2*k*sum((long long)((k-1)/2-1));
- if (!(k&1)) ans+=(long long)k*((long long)k/2-1);
- }
- int main(void){
- init();
- for (int i=1;i<=n;i++) if (!b[i]) sol(i);
- cout<<ans;
- return 0;
- }
Add Comment
Please, Sign In to add comment