链接:

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的更多相关文章

  1. Codeforces Round #584

    传送门 A. Paint the Numbers 签到. Code #include <bits/stdc++.h> using namespace std; typedef long l ...

  2. Codeforces Round #584 - Dasha Code Championship - Elimination Round (rated, open for everyone, Div. 1 + Div. 2)

    怎么老是垫底啊. 不高兴. 似乎 A 掉一道题总比别人慢一些. A. Paint the Numbers 贪心,从小到大枚举,如果没有被涂色,就新增一个颜色把自己和倍数都涂上. #include< ...

  3. 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个人,每个人都有喜欢的两个点心,现在给他们排个队,一个一个吃,每个人只要有自己喜欢的点心就会 ...

  4. Codeforces Round #584 E2. Rotate Columns (hard version)

    链接: https://codeforces.com/contest/1209/problem/E2 题意: This is a harder version of the problem. The ...

  5. Codeforces Round #584 C. Paint the Digits

    链接: https://codeforces.com/contest/1209/problem/C 题意: You are given a sequence of n digits d1d2-dn. ...

  6. Codeforces Round #584 B. Koala and Lights

    链接: https://codeforces.com/contest/1209/problem/B 题意: It is a holiday season, and Koala is decoratin ...

  7. Codeforces Round #584 A. Paint the Numbers

    链接: https://codeforces.com/contest/1209/problem/A 题意: You are given a sequence of integers a1,a2,-,a ...

  8. Codeforces Round 584 题解

    -- A,B 先秒切,C 是个毒瘤细节题,浪费了很多时间后终于过了. D 本来是个 sb 题,然而还是想了很久,不过幸好没什么大事. E1,看了一会就会了,然而被细节坑死,好久才过. 感觉 E2 很可 ...

  9. Codeforces Round #584 (Div. 1 + Div. 2)

    Contest Page A sol 每次选最小的,然后把它的所有倍数都删掉. #include<bits/stdc++.h> using namespace std; int read( ...

随机推荐

  1. [转帖]nginx location配置详细解释

    nginx location配置详细解释 http://outofmemory.cn/code-snippet/742/nginx-location-configuration-xiangxi-exp ...

  2. 1186: 零起点学算法93——改革春风吹满地(C)

    一.题目 http://acm.wust.edu.cn/problem.php?id=1186&soj=0 二.分析 多组输入,'0'结束: 顶点的个数在3至100之间: 一定顺序输入坐标: ...

  3. c++设计原则:继承与组合

    “优先使用对象组合,而不是继承”是面向对象设计的原则之一. 组合也叫“对象持有”,就是在类中定义另一类型的成员,继承会破坏类的独立性,增加系统的复杂性,一般系统的继承层次不超过3层.组合拥有良好的扩展 ...

  4. SAS学习笔记15 SAS导入数据(import txt csv xlsx spss)

  5. NodeJs 的Module.export 和 export

    NodeJs  的Module.export 和 export 是一样的. 但是Module.export ={....} 可以起效,.export ={....} 是失效的. 这里的export  ...

  6. pandas简介

  7. MYSQL日期相关操作

    *******MYSQL中取当前周/月/季/年的第一天与最后一天******* 当年第一天: SELECT DATE_SUB(CURDATE(),INTERVAL dayofyear(now())-1 ...

  8. linux shell数值比较和字符串比较

    说明: 1. 把字符串当成整型进行比较,由于abcd等字符对不上0123当程序尝试去转成二进制时无法完成转换,所以用于数值比较的运算不能用于字符串比较:但是把整型当成字符串进行比较,0123这些数值完 ...

  9. mysql打印用户权限的小技巧

    mysql5.5 5.6 SHOW GRANTS FOR mysql> SELECT DISTINCT CONCAT('SHOW GRANTS FOR ''',user,'''@''',host ...

  10. Vue下的package.json

    { "name": "demo2", "version": "1.0.0", "description&quo ...