链接:

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. laravel中间件的实现原理

    中间件的实现原理 运用 array_reduce 以及 call_user_func 实现 interface Middleware { public static function handle(C ...

  2. jenkins sonarqube 代码检测部署

    install pgsql and sonarqube docker run --name postgresqldb -e POSTGRES_USER=sonar -e POSTGRES_PASSWO ...

  3. python第一个浏览器的自动执行程序

    1.目标:简单点,百度搜索“美丽的程序员” 2.操作方法: a.python已经安装完成 b.安装PIP:在windows的cmd窗口下输入easy_install pip      c.安装sele ...

  4. python学习-16 列表list

    list 1.由[ ]括住,中括号内各元素由逗号隔开,各元素可以是数字,字符串,列表,布尔值等等. 例如: li = [521,"love",["john",& ...

  5. image analogies笔记

    Image Analogies 个人学习笔记, 根基尚浅, 免不得颇多纰漏, 望批评指教. 这是一篇2001年的文章, 其核心主要讲了如何将一对图片之间的"转换模式"应用到其他图片 ...

  6. app实现长按出现弹窗 或者 出现 删除

    <!-- Kpi --> <template> <div class="kpi_container"> <nav-bar :title=& ...

  7. 3.ASP.NET Core Docker学习-构建单机多容器环境

    基于docker Docker运行 : docker run -p 8001:80 -d --name name1 name2:1.0 其中-p 8001:80 8001是主机的端口,80是容器的端口 ...

  8. python处理Excel文件的几个模块

    在python中简单地处理excel文件,有几个相关的模块,各有千秋,本文将不定时收录. Python Excel网站收集了关于python处理excel文件的各种信息. [注意]使用python处理 ...

  9. 十一、微信小程序-var、let、const用法详解

    let命令 基本用法 ES6 新增了let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效. { let a = 10; var b = 1; } a // ...

  10. [书籍翻译] 《JavaScript并发编程》第六章 实用的并发

    本文是我翻译<JavaScript Concurrency>书籍的第六章 实用的并发,该书主要以Promises.Generator.Web workers等技术来讲解JavaScript ...