【分类讨论】Codeforces Round #407 (Div. 2) D. Weird journey
考虑这个二元组中有一者是自环,则必然合法。
考虑这两条边都不是自环,如果它们不相邻,则不合法,否则合法。
坑的情况是,如果它是一张完整的图+一些离散的点,则会有解,不要因为图不连通,就误判成无解。
#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的更多相关文章
- 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 ...
- 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 ...
- Codeforces Round #407 (Div. 1)
人傻不会B 写了C正解结果因为数组开小最后RE了 疯狂掉分 AC:A Rank:392 Rating: 2191-92->2099 A. Functions again 题目大意:给定一个长度为 ...
- 【分类讨论】【set】Codeforces Round #407 (Div. 2) B. Masha and geometric depression
模拟一下那个过程,直到绝对值超过l,或者出现循环为止. 如果结束之后,绝对值是超过l的,就输出当前写在黑板上的数量. 如果出现循环,则如果写在黑板上的数量非零,则输出inf(注意!如果陷入的循环是一个 ...
- Codeforces Round #407 (Div. 2)
来自FallDream的博客,未经允许,请勿转载,谢谢. ------------------------------------------------------ A.Anastasia and ...
- 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 ...
- Codeforces Round #407 (Div. 2) D,E
图论 D. Weird journey time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 【预处理】Codeforces Round #407 (Div. 2) C. Functions again
考虑枚举每个子串开头的位置,然后答案转化成询问这个位置之后 哪个位置的前缀和 - 这位置的前缀和 最大(当然是已经把绝对值和正负的情况处理好了,可以发现按奇偶,这个序列只有两种情况),只需要预处理这两 ...
- 【贪心】Codeforces Round #407 (Div. 2) A. Anastasia and pebbles
贪心地一个一个尽可能往口袋里放,容易发现和顺序无关. #include<cstdio> #include<iostream> using namespace std; type ...
随机推荐
- MongoDB安装配置及使用
1.安装配置:https://www.cnblogs.com/ymwangel/p/5859453.html 2.使用 from pymongo import MongoClient #连接 conn ...
- 5.0docer 网络链接
docker0 :linux的虚拟网桥 虚拟网桥特点: 1.可以设置ip地址 2.相当于拥一个隐藏的虚拟网卡 安装网桥工具 apt-get install bridge-utils brctl ...
- php中的parse_ini_file函数
作用:parse_ini_file() 函数解析一个配置文件,并以数组的形式返回其中的设置 格式:parse_ini_file(file,true)// (第二个参数为可选参数.如果设置为 true, ...
- Bit banging
Bit banging Bit banging is a technique for serial communications using software instead of dedicated ...
- 007 Java并发编程:Callable、Future和FutureTask
原文https://www.cnblogs.com/dolphin0520/p/3949310.html Java并发编程:Callable.Future和FutureTask 在前面的文章中我们讲述 ...
- linux指令和文件系统
linux root用户的主目录是 /root , 其余用户在 /home 中: tar 常用 tar -zxvf : 安装使用 yum or wget website: mv a.g b.g 重命名 ...
- 经典卷积网络模型 — LeNet模型笔记
LeNet-5包含于输入层在内的8层深度卷积神经网络.其中卷积层可以使得原信号特征增强,并且降低噪音.而池化层利用图像相关性原理,对图像进行子采样,可以减少参数个数,减少模型的过拟合程度,同时也可以保 ...
- node.js3
第一部分:express(MVC) 1.下载express npm install express --save 2.引入express require('express') 中间件 body-par ...
- Java单线程多实例和多线程多实例
最近写了一个程序,是采用多线程往redis里面写入数据,想统计一下一共写了多少条数据,于是用了一个static的全局变量count来累加,这块代码抽象出来就是这样的: public class Mul ...
- ReentrantLock 分析
带着疑问去分析 ReentrantLock是如何实现锁管理的. ReentrantLock是如何实现重入的. ReentrantLock是如何实现公平锁与非公平锁. ReentantLock的公平锁为 ...