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个人,每个人都有喜欢的两个点心,现在给他们排个队,一个一个吃,每个人只要有自己喜欢的点心就会吃掉(不会留给后面的人)。
如果有人什么都没吃就会不开心,问怎么安排使不开心的人最少。
思路:
看成一个图的问题,点心是节点,人是一条边。对于每个连通块,总会有一个人吃两个点心,其他人吃一个(其中一个是其他人也就吃掉了的)。
可以保证这样是最优的,所有每个连通块的答案是连通数 x-1。
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#include <cstdio>//sprintf islower isupper
#include <cstdlib>//malloc exit strcat itoa system("cls")
#include <iostream>//pair
#include <fstream>//freopen("C:\\Users\\13606\\Desktop\\草稿.txt","r",stdin);
#include <bitset>
//#include <map>
//#include<unordered_map>
#include <vector>
#include <stack>
#include <set>
#include <string.h>//strstr substr
#include <string>
#include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
#include <cmath>
#include <deque>
#include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
#include <vector>//emplace_back
//#include <math.h>
//#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
#include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
#define rint register int
#define fo(a,b,c) for(rint a=b;a<=c;++a)
#define fr(a,b,c) for(rint a=b;a>=c;--a)
#define mem(a,b) memset(a,b,sizeof(a))
#define pr printf
#define sc scanf
#define ls rt<<1
#define rs rt<<1|1
typedef long long ll;
void swapp(int &a,int &b);
double fabss(double a);
int maxx(int a,int b);
int minn(int a,int b);
int Del_bit_1(int n);
int lowbit(int n);
int abss(int a);
const double E=2.718281828;
const double PI=acos(-1.0);
//const ll INF=(1LL<<60);
const int inf=(<<);
const double ESP=1e-;
const int mod=(int)1e9+;
const int N=(int)1e6+; bool f[N],is[N];
vector<vector<int> > G(N); int bfs(int start)
{
if(f[start])return ;
int sum=;
queue<int>q;
q.push(start);
while(!q.empty())
{
int now=q.front();q.pop();
if(f[now])continue;
f[now]=;
sum++;
int sz=G[now].size();
for(int i=;i<sz;++i)
q.push(G[now][i]);
}
return sum;
} int main()
{
int n,k;
sc("%d%d",&n,&k);
for(int i=;i<=k;++i)
{
int u,v;
sc("%d%d",&u,&v);
is[u]=is[v]=;
G[u].push_back(v);
G[v].push_back(u);
}
int ans=;
for(int i=;i<=n;++i)
if(is[i])
ans+=bfs(i)-;
pr("%d\n",k-ans);
return ;
} /**************************************************************************************/ int maxx(int a,int b)
{
return a>b?a:b;
} void swapp(int &a,int &b)
{
a^=b^=a^=b;
} int lowbit(int n)
{
return n&(-n);
} int Del_bit_1(int n)
{
return n&(n-);
} int abss(int a)
{
return a>?a:-a;
} double fabss(double a)
{
return a>?a:-a;
} int minn(int a,int b)
{
return a<b?a:b;
}
Cow and Snacks(吃点心--图论转换) Codeforces Round #584 - Dasha Code Championship - Elimination Round (rated, open for everyone, Div. 1 + Div. 2)的更多相关文章
- Codeforces Round #584 - Dasha Code Championship - Elimination Round (rated, open for everyone, Div. 1 + Div. 2)
怎么老是垫底啊. 不高兴. 似乎 A 掉一道题总比别人慢一些. A. Paint the Numbers 贪心,从小到大枚举,如果没有被涂色,就新增一个颜色把自己和倍数都涂上. #include< ...
- 状压DP--Rotate Columns (hard version)-- Codeforces Round #584 - Dasha Code Championship - Elimination Round (rated, open for everyone, Div. 1 + Div. 2)
题意:https://codeforc.es/problemset/problem/1209/E2 给你一个n(1-12)行m(1-2000)列的矩阵,每一列都可以上下循环移动(类似密码锁). 问你移 ...
- Codeforces Round #584 - Dasha Code Championship - Elimination Round (rated, open for everyone, Div. 1 + Div. 2) G1. Into Blocks (easy version)
题目:https://codeforc.es/contest/1209/problem/G1 题意:给你一个序列,要你进行一些操作后把他变成一个好序列,好序列的定义是,两个相同的数中间的数都要与他相同 ...
- Codeforces Round #584 - Dasha Code Championship - Elimination Round (rated, open for everyone, Div. 1 + Div. 2)C
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;string s;pair<int,in ...
- codeforces(Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) )(C,D)
C. Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Cow and Snacks
D. Cow and Snacks 参考:Codeforces 1209D. Cow and Snacks 思路:利用并查集,构建一个生成树,然后树的边数就是能够开心的客人的人数.用一个条件fin ...
- D. Cow and Snacks 并查集
D. Cow and Snacks 题意:有n种小吃,m个人,每个人有两种喜欢的小吃,当一个人遇到两种自己都喜欢的小吃,可以都吃掉,问在最优的吃小吃顺序下,不能吃到自己喜欢的小吃的人数最少是多少? 题 ...
- Codeforces Round #584
传送门 A. Paint the Numbers 签到. Code #include <bits/stdc++.h> using namespace std; typedef long l ...
- (AB)Codeforces Round #528 (Div. 2, based on Technocup 2019 Elimination Round
A. Right-Left Cipher time limit per test 1 second memory limit per test 256 megabytes input standard ...
随机推荐
- Django-自定义web框架
自定义web框架 1.准备登录的html <!DOCTYPE html> <html lang="en"> <head> <meta ch ...
- hdu5492
hdu5492 陈大哥的毒瘤题T1 题意: 差不多就是根据题意推式子,求最小方差. 解法: 首先,可以观察到,如果我们直接暴力去取平均数,很大概率会取出来小数,所以一个很直观的想法就是把平均数从式子里 ...
- 苹果系统OSX中Automator批量重命名
Automator,看字面意思就无比强大,[自动机器].有什么能比自动还让人着魔? 答案是没有✔ 如果你用的是mac,如果你有一堆文件要重新整理命名,如果你还在Goole什么"批量重命名软件 ...
- ARTS打卡计划第十五周
Algorithms: https://leetcode-cn.com/problems/single-number/submissions/ Review: “What Makes a Good D ...
- phpstorm配置了git后Terminal 不能使用显示:git' 不是内部或外部命令,也不是可运行的程序
问题:在phpstorm上配置好git后,将代码拉了下来 ,但是命令行无法使用显示如图 解决方法:①找到安装git的位置,然后在该目录的子目录下分别找到git-core.bin 两个目录,我的安装在了 ...
- 让 Nginx 支持 WAF 防护功能实战
ngx_lua_waf 安装说明文档 作者github地址: https://github.com/loveshell/ngx_lua_waf ———————————————————————————— ...
- python之scrapy的debug、shell、settings、pipelines
1.debug了解 2.scrapy shell了解 Scrapy shell是一个交互终端,我们可以在未启动spider的情况下尝试及调试代码,也可以用来测试XPath表达式 使用方法: scrap ...
- Python统计分析可视化库seaborn(相关性图,变量分布图,箱线图等等)
Visualization of seaborn seaborn[1]是一个建立在matplot之上,可用于制作丰富和非常具有吸引力统计图形的Python库.Seaborn库旨在将可视化作为探索和理 ...
- spring boot系列(七)spring boot 使用mongodb
1 pom.xml配置 增加包依赖:spring-boot-starter-data-mongodb <dependency> <groupId>org.springframe ...
- unity2d教程
https://segmentfault.com/a/1190000003965359