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 ...
随机推荐
- css让文字,字母折行
加上如下的CSS设置,就是设定好宽度width,然后设置合适的word-wrap和word-break属性: ul li{ width: 100px; word-wrap: break-word; w ...
- Python基础之基本数据类型的总结
基本数据类型的总结 1. 按照存储空间的占用分(从低到高) 数字 字符串 集合:无序,即无序存索引相关信息 元组:有序,需要存索引相关信息,不可变 列表:有序,需要存索引相关信息,可变,需要处理数据的 ...
- ew做代理 进一步内网渗透
0x00 前言 最近搞站的时候有内网穿透的需求,大佬向我推荐了EW,本文模拟一个攻击场景对Earthworm的使用方法做一个简单的介绍.其实相应的内容EW的官网已经说得很详细了,我这里纯粹是作为个人笔 ...
- Ajax传递复杂对象报415
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- Linux设备驱动程序 之 顺序锁
当要保护的资源很小,很简单,会频繁的被访问而且写入访问很少的且必须快速时(即读不允许让写饥饿),就可以使用顺序锁(seqlock):从本质上讲,顺序锁会允许读取者对资源的自由访问,但需要读取者检查是否 ...
- 搬运 centos7.2 apache 绑定二级目录 访问依然是apache页面
<VirtualHost *:80>ServerName xx.comDocumentRoot /var/www/html/xx</VirtualHost>
- Nginx之 Location 的生成
1. Location 的生成 location 的生成大致有三种: 由 location 指令直接生成 命令 location:仅用于 server 内部跳转,如 rewrite 就是命名 loca ...
- Python —— sklearn.feature_selection模块
Python —— sklearn.feature_selection模块 sklearn.feature_selection模块的作用是feature selection,而不是feature ex ...
- pytorch-卷积基本网络结构-提取网络参数-初始化网络参数
基本的卷积神经网络 from torch import nn class SimpleCNN(nn.Module): def __init__(self): super(SimpleCNN, self ...
- 手把手教你把5V的Arduino改造成3.3V版本
Arduino Pro Mini是基于ATmega328的微控制板,支持5V/3.3V电压.但是我们比较容易买到的是5V供电的版本,有的时候需要用到3.3V版本(比如我们的软控DAC).怎样才能让这款 ...