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

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

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

#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. 2017-3-26 webpack入门(一)

    2017-3-26 webpack入门(一) webpack 前端 打包 最近项目里用到了webpack特意总结一下.来源:http://webpackdoc.com 1 概念 1.1 webpack ...

  2. hdu 1253 胜利大逃亡(简单题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1253 题目大意:在所给的时间能顺利离开城堡. #include <iostream> #i ...

  3. 二叉查找树、平衡二叉树、红黑树、B-/B+树性能对比

    转载:https://blog.csdn.net/z702143700/article/details/49079107 前言:BST.AVL.RBT.B-tree都是动态结构,查找时间基本都在O(l ...

  4. 调用start()与run()的区别

    1.调用start()方法: 通知“线程规划器”当前线程已经准备就绪,等待调用线程对象的run()方法.这个过程就是让系统安排一个时间来调用Thread中的run()方法,使线程得到运行,启动线程,具 ...

  5. Socket与URL通信比较

    转至链接:http://blog.csdn.net/qq_15848173/article/details/46328399 利用URL通信和Socket进行通信有很多相似之处.他们都是利用建立连接. ...

  6. 共享变量 static

    一个类,有static变量counter,所有类实例共享 如果多个类实例,通过多线程访问static变量,就会产生覆盖的情况. 会发现counter偏小. 解决方法: AtomicLong count ...

  7. initWithFrame和initWithCoder的区别

    如果使用了Interface Builder 方式或nib,就不会调用initWithFrame方法,因为nib文件知道怎么初始化了, 但可以使用initWithCoder这一个更深层的init方法来 ...

  8. DBUtile

    DBUtils是Python的一个用于实现数据库连接池的模块. 模式: - 每个线程创建一个连接,关闭(默认不关闭),线程终止时,才关闭连接. - 创建共享连接池 应用:只要写原生SQL,用户数据框连 ...

  9. Multiply Strings——面试题

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  10. 微信小程序~触摸相关事件(拖拽操作、手势识别、多点触控)

    touchstart     手指触摸动作开始 touchmove    手指触摸后移动 touchcancel  手指触摸动作被打断,如来电提醒,弹窗 touchend      手指触摸动作结束 ...