图论
D. Weird journey
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Little boy Igor wants to become a traveller. At first, he decided to visit all the cities of his motherland — Uzhlyandia.

It is widely known that Uzhlyandia has n cities connected with m bidirectional roads. Also, there are no two roads in the country that connect the same pair of cities, but roads starting and ending in the same city can exist. Igor wants to plan his journey beforehand. Boy thinks a path is good if the path goes over m - 2 roads twice, and over the other 2 exactly once. The good path can start and finish in any city of Uzhlyandia.

Now he wants to know how many different good paths are in Uzhlyandia. Two paths are considered different if the sets of roads the paths goes over exactly once differ. Help Igor — calculate the number of good paths.

Input

The first line contains two integers nm (1 ≤ n, m ≤ 106) — the number of cities and roads in Uzhlyandia, respectively.

Each of the next m lines contains two integers u and v (1 ≤ u, v ≤ n) that mean that there is road between cities u and v.

It is guaranteed that no road will be given in the input twice. That also means that for every city there is no more than one road that connects the city to itself.

Output

Print out the only integer — the number of good paths in Uzhlyandia.

Examples
input
5 4
1 2
1 3
1 4
1 5
output
6
input
5 3
1 2
2 3
4 5
output
0
input
2 2
1 1
1 2
output
1
Note

In first sample test case the good paths are:

  • 2 → 1 → 3 → 1 → 4 → 1 → 5,
  • 2 → 1 → 3 → 1 → 5 → 1 → 4,
  • 2 → 1 → 4 → 1 → 5 → 1 → 3,
  • 3 → 1 → 2 → 1 → 4 → 1 → 5,
  • 3 → 1 → 2 → 1 → 5 → 1 → 4,
  • 4 → 1 → 2 → 1 → 3 → 1 → 5.

There are good paths that are same with displayed above, because the sets of roads they pass over once are same:

  • 2 → 1 → 4 → 1 → 3 → 1 → 5,
  • 2 → 1 → 5 → 1 → 3 → 1 → 4,
  • 2 → 1 → 5 → 1 → 4 → 1 → 3,
  • 3 → 1 → 4 → 1 → 2 → 1 → 5,
  • 3 → 1 → 5 → 1 → 2 → 1 → 4,
  • 4 → 1 → 3 → 1 → 2 → 1 → 5,
  • and all the paths in the other direction.

Thus, the answer is 6.

In the second test case, Igor simply can not walk by all the roads.

In the third case, Igor walks once over every road.

题意:

n个点,m条无向边,要求走遍所有的点并且其中有两条边走过一次,m-2条边走过两次,问方案数是多少。

代码:

//将每条无向边变成两条有向边,题目就变成了从2*m条边中去掉两条能够形成哈密顿图。
//可以选两条相邻的边(非自环)或者两条自环,或者一条自环和一条非自环边
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,m,vis[],loop[];
vector<int>v[];
void dfs(int x){
if(vis[x]) return ;
vis[x]=;
for(int i=;i<(int)v[x].size();i++){
int y=v[x][i];
dfs(y);
}
}
int main()
{
scanf("%d%d",&n,&m);
int x,y,lp=;
memset(vis,,sizeof(vis));
for(int i=;i<m;i++){
scanf("%d%d",&x,&y);
if(x==y) loop[x]++,lp++;
else{
v[x].push_back(y);
v[y].push_back(x);
}
}
for(int i=;i<=n;i++)
if(!v[i].empty()||loop[i]){
dfs(i);break;
}
for(int i=;i<=n;i++){
if(!vis[i]&&(!v[i].empty()||loop[i]))
return *printf("0\n");
}
ll ans=;
for(int i=;i<=n;i++){
int tmp=(int)v[i].size();
ans+=1LL*tmp*(tmp-)/;//C(tmp,2)
}
ans+=(1LL*lp*(lp-)/+1LL*lp*(m-lp));
return *printf("%I64d\n",ans);
}

BFS

E. The Great Mixing
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Sasha and Kolya decided to get drunk with Coke, again. This time they have k types of Coke. i-th type is characterised by its carbon dioxide concentration . Today, on the party in honour of Sergiy of Vancouver they decided to prepare a glass of Coke with carbon dioxide concentration . The drink should also be tasty, so the glass can contain only integer number of liters of each Coke type (some types can be not presented in the glass). Also, they want to minimize the total volume of Coke in the glass.

Carbon dioxide concentration is defined as the volume of carbone dioxide in the Coke divided by the total volume of Coke. When you mix two Cokes, the volume of carbon dioxide sums up, and the total volume of Coke sums up as well.

Help them, find the minimal natural number of liters needed to create a glass with carbon dioxide concentration . Assume that the friends have unlimited amount of each Coke type.

Input

The first line contains two integers nk (0 ≤ n ≤ 1000, 1 ≤ k ≤ 106) — carbon dioxide concentration the friends want and the number of Coke types.

The second line contains k integers a1, a2, ..., ak (0 ≤ ai ≤ 1000) — carbon dioxide concentration of each type of Coke. Some Coke types can have same concentration.

Output

Print the minimal natural number of liter needed to prepare a glass with carbon dioxide concentration , or -1 if it is impossible.

Examples
input
400 4
100 300 450 500
output
2
input
50 2
100 25
output
3
Note

In the first sample case, we can achieve concentration  using one liter of Coke of types  and .

In the second case, we can achieve concentration  using two liters of  type and one liter of  type: .

题意:
有k个浓度的可乐s[1~k]/1000(浓度可能有相同的),每种无限多,和一个目标浓度n/1000,问最少用几升可以配出目标浓度的可乐。

代码:

初始值x置为0,用bfs算出x值再次等于0的步数就是最少需要几升。

//
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[];
map<int,int>mp;
int main()
{
int n,k,x,cnt=;
queue<int>q;
scanf("%d%d",&n,&k);
for(int i=;i<k;i++){
scanf("%d",&x);
a[cnt++]=x-n;
}
sort(a,a+cnt);
cnt=unique(a,a+cnt)-a;
q.push();
while(!q.empty()){
x=q.front();q.pop();
for(int i=;i<cnt;i++){
int y=x+a[i];
if(abs(y)>) continue;//剪枝
if(y==){
return *printf("%d\n",mp[x]+);
}
if(!mp[y]){
mp[y]=mp[x]+;
q.push(y);
}
}
}
return *printf("-1\n");
}

Codeforces Round #407 (Div. 2) D,E的更多相关文章

  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. 1)

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

  3. Codeforces Round #407 (Div. 2)

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

  4. 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 ...

  5. 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 ...

  6. 【分类讨论】Codeforces Round #407 (Div. 2) D. Weird journey

    考虑这个二元组中有一者是自环,则必然合法. 考虑这两条边都不是自环,如果它们不相邻,则不合法,否则合法. 坑的情况是,如果它是一张完整的图+一些离散的点,则会有解,不要因为图不连通,就误判成无解. # ...

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

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

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

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

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

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

随机推荐

  1. 利用maven进行项目管理

    下面为maven项目管理的一个结构 首先pom是路径文件,我们在编译或是运行程序时调用到jdk或一些自己写的jar包时会需要指明物理路径,这里的pom是一样的道理,同时在maven的管理下多出来了一些 ...

  2. 【转载】android 常用开源框架

    对于Android初学者以及对于我们菜鸟,这些大神们开发的轻量级框架非常有用(更别说开源的了). 下面转载这10个框架的介绍:(按顺序来吧没有什么排名). 一.  Afinal 官方介绍: Afina ...

  3. Java动态代码模式

    java动态代理(JDK和cglib) JAVA的动态代理 代理模式 代理模式是常用的java设计模式,他的特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理消息.过滤消息.把消息转发给委 ...

  4. JS原型与面向对象总结

    ECMAScript有两种开发模式:1.函数式(过程化),2.面向对象(OOP).面向对象的语言有一个标志,那就是类的概念,而通过类可以创建任意多个具有相同属性和方法的对象.但 是,ECMAScrip ...

  5. 在Excel里面,单元格里输入公式后只显示公式本身,不显示结果,怎么办

    这种情况是对Excel进行了设置,设置的就是在单元格中只显示公式,不显示结果,解决的办法有两个: 1 用快捷键CTR+~ 2 点击"公式"选项卡,然后反选里面的"显示公式 ...

  6. 软工2017团队协作第七周——个人PSP

    10.27 --11.2本周例行报告 1.PSP(personal software process )个人软件过程. 类型 任务 开始时间                结束时间 中断时间 实际用时 ...

  7. WPF+数据库+三层

    1.计算类 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespac ...

  8. JavaScript初探系列之面向对象

    面向对象的语言有一个标志,即拥有类的概念,抽象实例对象的公共属性与方法,基于类可以创建任意多个实例对象,一般具有封装.继承.多态的特性!但JS中对象与纯面向对象语言中的对象是不同的,ECMA标准定义J ...

  9. 第一部分shell编程1基础知识

    ls etc/init.d/ shell脚本的路径 ls /usr/local/apache2/ ls /usr/local/apache2/bin/apachectl 1. shell特性命令历史 ...

  10. 微信小程序 功能函数 计时器

    let lovetime = setInterval(function () { let str = '(' + n + ')' + '重新获取' that.setData({ getText2: s ...