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( ...
随机推荐
- 【51nod】1407 与与与与
[51nod]1407 与与与与 设\(f(x)\) 为\(A_{i} \& x == x\)的\(A_{i}\)的个数 设\(g(x)\)为\(x\)里1的个数 \(\sum_{i = 0} ...
- 【C++札记】动态分配内存(malloc,free)
介绍 操作系统中存在一个内存管理器(Memory Manager),简称MM,它负责管理内存. MM提供的服务:应用程序可以向MM申请一块指定大小的内存(借出),用完之后应用程序应该释放(还回). 所 ...
- 利用Python进行数据分析_Pandas_处理缺失数据
申明:本系列文章是自己在学习<利用Python进行数据分析>这本书的过程中,为了方便后期自己巩固知识而整理. 1 读取excel数据 import pandas as pd import ...
- windows通过gcc编译代码
1.将gcc添加到环境变量 2.检查gcc是否安装成功 cmd下输入gcc –v 3.cd进入需要编译源文件的目录 4.dir查看当前目录下是否有需要编译的文件(linux下用ls) 5.编译文件(H ...
- Django之ORM相关操作
一般操作 常用的13个操作 <1> all(): 查询所有结果 <2> filter(**kwargs): 它包含了与所给筛选条件相匹配的对象 <3> get(** ...
- SAS学习笔记31 SAS随机分组方法及实现
随机分组方法包括: 简单随机化(simple randomization) 区组随机化(block randomization) 分层随机化(stratified randomization) 分层区 ...
- asp.net core-7.在Core Mvc中使用Options
1,添加asp.net core mvc应用程序 2,添加Controllers控制器文件夹,Views视图文件夹 然后在Startup类中ConfigureServices方法中注册一下servic ...
- Mysql slave 延迟故障一列(无主键)
首先还是给出我见过的一些延迟可能: 大事物延迟 延迟略为2*执行时间 状态为:reading event from the relay log 大表DDL延迟 延迟略为1*执行时间 状态为:alter ...
- Java Fibonacci 斐波那契亚
Java Fibonacci 斐波那契亚 /** * <html> * <body> * <P> Copyright 1994-2018 JasonInternat ...
- js中undefined的几种情况
1.变量声明且没有赋值: 2.获取对象中不存在的属性时: 3.函数需要实参,但是调用时没有传值,形参是undefined: 4.函数调用没有返回值或者return后没有数据,接收函数返回的变量是und ...