Codeforces Round #584 D. Cow and Snacks
链接:
https://codeforces.com/contest/1209/problem/D
题意:
The legendary Farmer John is throwing a huge party, and animals from all over the world are hanging out at his house. His guests are hungry, so he instructs his cow Bessie to bring out the snacks! Moo!
There are n snacks flavors, numbered with integers 1,2,…,n. Bessie has n snacks, one snack of each flavor. Every guest has exactly two favorite flavors. The procedure for eating snacks will go as follows:
First, Bessie will line up the guests in some way.
Then in this order, guests will approach the snacks one by one.
Each guest in their turn will eat all remaining snacks of their favorite flavor. In case no favorite flavors are present when a guest goes up, they become very sad.
Help Bessie to minimize the number of sad guests by lining the guests in an optimal way.
思路:
考虑将零食看成点, 每个牛看成边,用边将喜欢的两点连起来, 如果x个边连成一个环,不管环内的谁先取最多只有x-1.
建图DFS.
代码:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5+10;
int n, k;
vector<int> G[MAXN];
int Vis[MAXN], l[MAXN], r[MAXN];
int cnt;
void Dfs(int u)
{
Vis[u] = 1;
for (int i = 0;i < G[u].size();i++)
{
if (Vis[G[u][i]])
continue;
cnt++;
Dfs(G[u][i]);
}
}
int main()
{
cin >> n >> k;
int u, v;
for (int i = 1;i <= k;i++)
{
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
l[i] = u, r[i] = v;
}
for (int i = 1;i <= k;i++)
{
if (!Vis[l[i]])
Dfs(l[i]);
if (!Vis[r[i]])
Dfs(r[i]);
}
cout << k-cnt << endl;
return 0;
}
Codeforces Round #584 D. Cow and Snacks的更多相关文章
- Codeforces Round #584
传送门 A. Paint the Numbers 签到. Code #include <bits/stdc++.h> using namespace std; typedef long l ...
- Codeforces Round #584 - Dasha Code Championship - Elimination Round (rated, open for everyone, Div. 1 + Div. 2)
怎么老是垫底啊. 不高兴. 似乎 A 掉一道题总比别人慢一些. A. Paint the Numbers 贪心,从小到大枚举,如果没有被涂色,就新增一个颜色把自己和倍数都涂上. #include< ...
- Cow and Snacks(吃点心--图论转换) Codeforces Round #584 - Dasha Code Championship - Elimination Round (rated, open for everyone, Div. 1 + Div. 2)
题意:https://codeforc.es/contest/1209/problem/D 有n个点心,有k个人,每个人都有喜欢的两个点心,现在给他们排个队,一个一个吃,每个人只要有自己喜欢的点心就会 ...
- Codeforces Round #584 E2. Rotate Columns (hard version)
链接: https://codeforces.com/contest/1209/problem/E2 题意: This is a harder version of the problem. The ...
- Codeforces Round #584 C. Paint the Digits
链接: https://codeforces.com/contest/1209/problem/C 题意: You are given a sequence of n digits d1d2-dn. ...
- Codeforces Round #584 B. Koala and Lights
链接: https://codeforces.com/contest/1209/problem/B 题意: It is a holiday season, and Koala is decoratin ...
- Codeforces Round #584 A. Paint the Numbers
链接: https://codeforces.com/contest/1209/problem/A 题意: You are given a sequence of integers a1,a2,-,a ...
- Codeforces Round 584 题解
-- A,B 先秒切,C 是个毒瘤细节题,浪费了很多时间后终于过了. D 本来是个 sb 题,然而还是想了很久,不过幸好没什么大事. E1,看了一会就会了,然而被细节坑死,好久才过. 感觉 E2 很可 ...
- Codeforces Round #584 (Div. 1 + Div. 2)
Contest Page A sol 每次选最小的,然后把它的所有倍数都删掉. #include<bits/stdc++.h> using namespace std; int read( ...
随机推荐
- MySQL和Oracle的区别与不同
一.mysql与Oracle的相同点: 1.都是关系型数据库管理系统(MySQL开放源码) 2.都是目前很流行的数据库(Oracle以分布式为核心): 二.MySQL.Oracle各自特点: 1.Or ...
- Redis 常用命令学四:有序集合类型命令
1.增加元素,修改存在元素的分数 127.0.0.1:6379> zadd score 99 a 88 b 66 c (integer) 3 127.0.0.1:6379> ZADD sc ...
- 分布式缓存 - hash环/一致性hash
一 引言 当前memcached,redis这类分布式kv缓存已经非常普遍.我们知道memcached的分布式其实是一种"伪分布式",也就是它的服务器节点之间其实是无关联的,之间没 ...
- C++中const关键字用法总结
看完了c++ primer的基础篇,对const还是有点陌生,在这里小小地总结一下吧. 1) const与变量 在变量的定义前加上 const 修饰符即可完成const对象的创建. const int ...
- 控制层解析post请求中json数据的时候,有些属性值为空
原因: 1.默认json数据解析的时候,值会赋给键的首字母是小写的封装的bean中的属性,如果没有首字母小写的属性,也不会报错.即bean中有getXXX方法时,从json到model会增加xxx属性 ...
- windows下java环境变量的一点心得
JAVA_HOME:D:\software\java\jdk1.8.0_121 CLASSPATH:.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar ...
- JVM OOM异常会导致JVM退出吗?
出处: https://mp.weixin.qq.com/s/8j8YTcr2qhVActLGzOqe7Q https://blog.csdn.net/h2604396739/article/de ...
- 部署java应用的几种方式
J2EE应用 该应用根目录下有好多文件夹和以jsp结尾的文件 部署时,需要在tomcat的conf目录下的server.xml文件中添加上<Context path="/" ...
- Android监听EditText输入字符串,删除指定的字符
alert_ed = (EditText) a.findViewById(R.id.alert_ed_queue_num); alert_ed.addTextChangedListener(mText ...
- java 框架-消息队列ActiveMQ
https://www.jianshu.com/p/ecdc6eab554c ActiveMQ从入门到精通(一) 22017.03.11 21:40:42字数 2650阅读 57286 这是关于消息中 ...