题意:

给出一个n个点的图,现在构造一个有n^2个点的新图,新图每个点表示为(a,b)(a,b<=n),两个点$(a,b),(c,d)$之间有边当且仅当原图中ac之间有边,bd之间有边。

问新图中有几个联通块。

第一眼看上去似乎很不可做,想一想新图中两个点$(a,b),(c,d)$在同一个联通块其实就是原图中放着两个棋子,一个在a,一个在b,两个棋子同时走相同步后第一个棋子在c,第二个在d。

先考虑两个棋子在原图同一个联通块里的情况,如果这个联通块有奇环,那么这个联通块里所有点对在新图中是一个联通块,否则是两个,大概就是同时从黑\白点走或者从一黑一白走。

如果两个棋子在原图中不同联通块里,那么如果两个联通块中都没有奇环,那么形成两个联通块,否则形成一个。

需要特判原图中联通块大小为1的情况。

#include<bits/stdc++.h>
#define N 400005
#define ll long long
using namespace std;
int n,m;
int head[N],ver[N],nxt[N],tot;
void add(int a,int b)
{
tot++;nxt[tot]=head[a];head[a]=tot;ver[tot]=b;return ;
}
int v[N];
int cnt,flag,sz,szz,tp;
void dfs(int x,int c)
{
cnt++;v[x]=c;
for(int i=head[x];i;i=nxt[i])
{
if(v[ver[i]]==-1)dfs(ver[i],c^1);
else if(v[ver[i]]!=(c^1))flag=1;
}
return ;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int t1,t2;
scanf("%d%d",&t1,&t2);
add(t1,t2);add(t2,t1);
}
ll ans=0;memset(v,-1,sizeof(v));
for(int i=1;i<=n;i++)
{
cnt=0;flag=0;
if(v[i]==-1)dfs(i,0);
if(cnt)
{
if(flag||cnt==1)ans++;
else ans+=2;
if(cnt==1)tp++;
else
{
if(!flag)szz++;
sz++;
}
}
}
ans+=1LL*2*tp*(n-1);
ans-=1LL*tp*(tp-1);
ans+=1LL*sz*(sz-1);
ans+=1LL*szz*(szz-1);
cout<<ans<<endl;
return 0;
}

  

Atcoder Grand 011 C - Squared Graph的更多相关文章

  1. 【AtCoder】AGC011 C - Squared Graph

    题解 大意是给出一张图,然后建一张新图,新图的点标号是(a,b) 如果a和c有一条边,b和d有一条边,那么(a,b)和(c,d)之间有一条边 我们把这道题当成这道题来做,给出两张图,如果第一张图有边( ...

  2. AtCoder Grand Contest 011

    AtCoder Grand Contest 011 upd:这篇咕了好久,前面几题是三周以前写的... AtCoder Grand Contest 011 A - Airport Bus 翻译 有\( ...

  3. AtCoder Grand Contest 031 简要题解

    AtCoder Grand Contest 031 Atcoder A - Colorful Subsequence description 求\(s\)中本质不同子序列的个数模\(10^9+7\). ...

  4. AtCoder Grand Contest 012

    AtCoder Grand Contest 012 A - AtCoder Group Contest 翻译 有\(3n\)个人,每一个人有一个强大值(看我的假翻译),每三个人可以分成一组,一组的强大 ...

  5. AtCoder Grand Contest 010

    AtCoder Grand Contest 010 A - Addition 翻译 黑板上写了\(n\)个正整数,每次会擦去两个奇偶性相同的数,然后把他们的和写会到黑板上,问最终能否只剩下一个数. 题 ...

  6. AtCoder Grand Contest 009

    AtCoder Grand Contest 009 A - Multiple Array 翻译 见洛谷 题解 从后往前考虑. #include<iostream> #include< ...

  7. AtCoder Grand Contest 008

    AtCoder Grand Contest 008 A - Simple Calculator 翻译 有一个计算器,上面有一个显示按钮和两个其他的按钮.初始时,计算器上显示的数字是\(x\),现在想把 ...

  8. AtCoder Grand Contest 007

    AtCoder Grand Contest 007 A - Shik and Stone 翻译 见洛谷 题解 傻逼玩意 #include<cstdio> int n,m,tot;char ...

  9. AtCoder Grand Contest 006

    AtCoder Grand Contest 006 吐槽 这套题要改个名字,叫神仙结论题大赛 A - Prefix and Suffix 翻译 给定两个串,求满足前缀是\(S\),后缀是\(T\),并 ...

随机推荐

  1. vb用createprocess启动其他应用程序

    Option Explicit Private Type PROCESS_INFORMATION hProcess As Long hThread As Long dwProcessId As Lon ...

  2. Django Rest Framework源码剖析(三)-----频率控制

    一.简介 承接上篇文章Django Rest Framework源码剖析(二)-----权限,当服务的接口被频繁调用,导致资源紧张怎么办呢?当然或许有很多解决办法,比如:负载均衡.提高服务器配置.通过 ...

  3. JSON传输数组的基本操作

    目标JSON结果如下: 生成JSON的的过程如下: Document document; Document::AllocatorType& allocator = document.GetAl ...

  4. libgdx退出对话框

    package com.fxb.newtest; import com.badlogic.gdx.ApplicationListener; import com.badlogic.gdx.Gdx; i ...

  5. REST-framework快速构建API--权限

    我们在访问资源时,有些资源保密程度较高,需要特殊的人员才能访问.比如,获取公司的每日收入流水的API接口,只能CEO才能查看. 这时,我们就需要将资源设定权限了. REST-framework实现如下 ...

  6. 设计模式 笔记 中介者模式 Mediator

    //---------------------------15/04/27---------------------------- //Mediator 中介者模式----对象行为型模式 /* 1:意 ...

  7. 整理一些常用的前端CND加速库,VUE,Jquery,axios

    VUE https://cdn.staticfile.org/vue/2.2.2/vue.min.js Jquery https://cdn.bootcss.com/jquery/3.4.0/jque ...

  8. 使用tensorflow实现mnist手写识别(单层神经网络实现)

    import tensorflow as tf import tensorflow.examples.tutorials.mnist.input_data as input_data import n ...

  9. "软件"和"软件工程"一词最早被谁提出?

    1."softwar”:1953年Richard R.Carhart在备忘录中使用software一词2.软件工程 1968 年北大西洋公约组织在前联邦德国开会提出的 1968年秋季,NAT ...

  10. 《Linux内核 》MOOC 课程

    姬梦馨 原创微博 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 学习笔记 一:什么是冯诺依曼体系结构? ...