考虑这个二元组中有一者是自环,则必然合法。

考虑这两条边都不是自环,如果它们不相邻,则不合法,否则合法。

坑的情况是,如果它是一张完整的图+一些离散的点,则会有解,不要因为图不连通,就误判成无解。

#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;
ll ans;
int n,m,zihuan;
int v[2000010],next[2000010],first[2000010],e;
void AddEdge(int U,int V){
v[++e]=V;
next[e]=first[U];
first[U]=e;
}
bool vis[2000010];
void dfs(int U){
vis[U]=1;
for(int i=first[U];i;i=next[i]){
if(!vis[v[i]]){
dfs(v[i]);
}
}
}
int main(){
// freopen("d.in","r",stdin);
int x,y;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;++i){
scanf("%d%d",&x,&y);
if(x==y){
++zihuan;
}
AddEdge(x,y);
AddEdge(y,x);
}
for(int i=1;i<=n;++i){
if(first[i]){
dfs(i);
break;
}
}
for(int i=1;i<=n;++i){
if((!vis[i]) && first[i]){
puts("0");
return 0;
}
}
ans=(ll)(zihuan-1)*(ll)(zihuan)/2ll+(ll)zihuan*(ll)(m-zihuan);
for(int i=1;i<=n;++i){
int cnt=0;
for(int j=first[i];j;j=next[j]){
if(v[j]!=i){
++cnt;
}
}
ans+=(ll)cnt*(ll)(cnt-1)/2ll;
}
cout<<ans<<endl;
return 0;
}

【分类讨论】Codeforces Round #407 (Div. 2) D. Weird journey的更多相关文章

  1. Codeforces Round #407 (Div. 1) B. Weird journey —— dfs + 图

    题目链接:http://codeforces.com/problemset/problem/788/B B. Weird journey time limit per test 2 seconds m ...

  2. Codeforces Round #407 (Div. 2) D. Weird journey(欧拉路)

    D. Weird journey time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  3. Codeforces Round #407 (Div. 1)

    人傻不会B 写了C正解结果因为数组开小最后RE了 疯狂掉分 AC:A Rank:392 Rating: 2191-92->2099 A. Functions again 题目大意:给定一个长度为 ...

  4. 【分类讨论】【set】Codeforces Round #407 (Div. 2) B. Masha and geometric depression

    模拟一下那个过程,直到绝对值超过l,或者出现循环为止. 如果结束之后,绝对值是超过l的,就输出当前写在黑板上的数量. 如果出现循环,则如果写在黑板上的数量非零,则输出inf(注意!如果陷入的循环是一个 ...

  5. Codeforces Round #407 (Div. 2)

    来自FallDream的博客,未经允许,请勿转载,谢谢. ------------------------------------------------------ A.Anastasia and ...

  6. Codeforces Round #407 (Div. 2)A B C 水 暴力 最大子序列和

    A. Anastasia and pebbles time limit per test 1 second memory limit per test 256 megabytes input stan ...

  7. Codeforces Round #407 (Div. 2) D,E

    图论 D. Weird journey time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. 【预处理】Codeforces Round #407 (Div. 2) C. Functions again

    考虑枚举每个子串开头的位置,然后答案转化成询问这个位置之后 哪个位置的前缀和 - 这位置的前缀和 最大(当然是已经把绝对值和正负的情况处理好了,可以发现按奇偶,这个序列只有两种情况),只需要预处理这两 ...

  9. 【贪心】Codeforces Round #407 (Div. 2) A. Anastasia and pebbles

    贪心地一个一个尽可能往口袋里放,容易发现和顺序无关. #include<cstdio> #include<iostream> using namespace std; type ...

随机推荐

  1. python基础===装饰器@property 的扩展

    以下来自Python 3.6.0 Document: class property(fget=None, fset=None, fdel=None, doc=None) Return a proper ...

  2. python基础===利用PyCharm进行Python远程调试(转)

    原文链接:利用PyCharm进行Python远程调试 背景描述 有时候Python应用的代码在本地开发环境运行十分正常,但是放到线上以后却出现了莫名其妙的异常,经过再三排查以后还是找不到问题原因,于是 ...

  3. 内核抢占实现(preempt) 【转】

    转自:http://blog.chinaunix.net/uid-12461657-id-3353217.html 一.什么叫抢占所谓抢占,说白了就是进程切换.linux的用户空间,进程A在执行中,来 ...

  4. C#比较两个list集合,两集合同时存在或A集合存在B集合中无

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. Docker壳的配置笔记

    docker 就是一个运行容器,在这个盒子里,他的端口,路径可以虚拟到另一个实际的磁盘上,运行空间独立,更安全! yum install -y docker docker-client service ...

  6. dos命令连接mysql并且查看编码方式

    打开cmd: 输入:mysql -hlocalhost -uroot -p 然后: show variables like 'char%';

  7. Context-Aware Network Embedding for Relation Modeling

    Context-Aware Network Embedding for Relation Modeling 论文:http://www.aclweb.org/anthology/P17-1158 创新 ...

  8. maven设置打jar包并引入依赖包

    --------------------------------------------------------方法一:将jar包和项目打在一起---------------------------- ...

  9. POJ-2398

    Toy Storage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4243   Accepted: 2517 Descr ...

  10. Python Flask SQLALchemy基础知识

    一.介绍 SQLAlchemy是一个基于Python实现的ORM框架.该框架建立在 DB API之上,使用关系对象映射进行数据库操作,简言之便是:将类和对象转换成SQL,然后使用数据API执行SQL并 ...