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。

  1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
  2 #include <cstdio>//sprintf islower isupper
  3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
  4 #include <iostream>//pair
  5 #include <fstream>//freopen("C:\\Users\\13606\\Desktop\\草稿.txt","r",stdin);
  6 #include <bitset>
  7 //#include <map>
  8 //#include<unordered_map>
  9 #include <vector>
 10 #include <stack>
 11 #include <set>
 12 #include <string.h>//strstr substr
 13 #include <string>
 14 #include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
 15 #include <cmath>
 16 #include <deque>
 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
 18 #include <vector>//emplace_back
 19 //#include <math.h>
 20 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
 21 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
 22 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
 23 #define rint register int
 24 #define fo(a,b,c) for(rint a=b;a<=c;++a)
 25 #define fr(a,b,c) for(rint a=b;a>=c;--a)
 26 #define mem(a,b) memset(a,b,sizeof(a))
 27 #define pr printf
 28 #define sc scanf
 29 #define ls rt<<1
 30 #define rs rt<<1|1
 31 typedef long long ll;
 32 void swapp(int &a,int &b);
 33 double fabss(double a);
 34 int maxx(int a,int b);
 35 int minn(int a,int b);
 36 int Del_bit_1(int n);
 37 int lowbit(int n);
 38 int abss(int a);
 39 const double E=2.718281828;
 40 const double PI=acos(-1.0);
 41 //const ll INF=(1LL<<60);
 42 const int inf=(1<<30);
 43 const double ESP=1e-9;
 44 const int mod=(int)1e9+7;
 45 const int N=(int)1e6+10;
 46
 47 bool f[N],is[N];
 48 vector<vector<int> > G(N);
 49
 50 int bfs(int start)
 51 {
 52     if(f[start])return 1;
 53     int sum=0;
 54     queue<int>q;
 55     q.push(start);
 56     while(!q.empty())
 57     {
 58         int now=q.front();q.pop();
 59         if(f[now])continue;
 60         f[now]=1;
 61         sum++;
 62         int sz=G[now].size();
 63         for(int i=0;i<sz;++i)
 64             q.push(G[now][i]);
 65     }
 66     return sum;
 67 }
 68
 69 int main()
 70 {
 71     int n,k;
 72     sc("%d%d",&n,&k);
 73     for(int i=1;i<=k;++i)
 74     {
 75         int u,v;
 76         sc("%d%d",&u,&v);
 77         is[u]=is[v]=1;
 78         G[u].push_back(v);
 79         G[v].push_back(u);
 80     }
 81     int ans=0;
 82     for(int i=1;i<=n;++i)
 83         if(is[i])
 84             ans+=bfs(i)-1;
 85     pr("%d\n",k-ans);
 86     return 0;
 87 }
 88
 89 /**************************************************************************************/
 90
 91 int maxx(int a,int b)
 92 {
 93     return a>b?a:b;
 94 }
 95
 96 void swapp(int &a,int &b)
 97 {
 98     a^=b^=a^=b;
 99 }
100
101 int lowbit(int n)
102 {
103     return n&(-n);
104 }
105
106 int Del_bit_1(int n)
107 {
108     return n&(n-1);
109 }
110
111 int abss(int a)
112 {
113     return a>0?a:-a;
114 }
115
116 double fabss(double a)
117 {
118     return a>0?a:-a;
119 }
120
121 int minn(int a,int b)
122 {
123     return a<b?a:b;
124 }

原文地址:https://www.cnblogs.com/--HPY-7m/p/11529740.html

时间: 2024-08-08 13:23:57

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<bits/stdc++.h> #define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to) #define dbg(...) fprintf(stderr, __VA_ARGS__) #define F

D. Cow and Snacks 并查集

D. Cow and Snacks 题意:有n种小吃,m个人,每个人有两种喜欢的小吃,当一个人遇到两种自己都喜欢的小吃,可以都吃掉,问在最优的吃小吃顺序下,不能吃到自己喜欢的小吃的人数最少是多少? 题解:把n种小吃当作n个点,m个人当作m条边,每个连通图里面第一个吃的人,一定是可以吃两种自己喜欢的小吃.每次判断这条边是否在已有的联通图里面,对已经在连通图里面的边,是一定不能吃到小吃,若不在连通图里面,则一定可以吃到小吃,用cnt统计可以吃到小吃的人数,最后m-cnt就是答案 #include<i

Codeforces Round #621 (Div. 1 + Div. 2)

Codeforces Round #621 (Div. 1 + Div. 2) A. Cow and Haybales 贪心,移到第一堆的代价即为与第一堆的距离. #include <bits/stdc++.h> using namespace std; void solve(){ int n,d;cin>>n>>d; int sum;cin>>sum; for(int i=1;i<n;i++){ int t;cin>>t; if(d>

Codeforces 754E:Dasha and cyclic table

Codeforces 754E:Dasha and cyclic table 题目链接:http://codeforces.com/problemset/problem/754/E 题目大意:$A$矩阵($size(A)=n \times m$,仅含'a'-'z')在整个平面做周期延拓,问$B$矩阵($size(B)=r \times c$,包含'a'-'z'及'?','?'为通配符)在哪些位置能与$A$矩阵匹配.输出$n \times m$的01矩阵,1表示在该位置匹配. 枚举+bitset常

CodeForces 614B Gena&#39;s Code

#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <math.h> using namespace std; int flag; char s[100000+10]; int zero; char q[100000+10]; bool Perfect() { int len=strlen(s); for(int i=1;

Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) A. Single Wildcard Pattern Matching B. Pair of Toys C. Bracket Subsequence D. Array Restoration-区间查询最值(RMQ(ST))

Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) A. Single Wildcard Pattern Matching 题意就是匹配字符的题目,打比赛的时候没有看到只有一个" * ",然后就写挫了,被hack了,被hack的点就是判一下只有一个" * ". 1 //A 2 #include<iostream> 3 #include<cstdio&g

Codeforces 1209D. Cow and Snacks

传送门 考虑构建图论模型,每个客人看成边,菜看成点,那么每个客人连接他喜欢的两个菜 对于某个客人,如果他要开心,它连接的两点至少要有一个还未被选择 考虑一个显然的贪心,我们要尽量让每个客人只吃到一种菜 考虑构建一个生成树,每次从树上一个节点往外延伸,连向一个新的点,那之间的边就是新的一个客人 并且这样构建以后,除了第一个客人吃到了两种菜以外,其他客人都只吃到一种菜,显然是最优的 发现代码实现的时候生成树连边的顺序是没影响的,所以直接构建生成树森林,那么森林中的边数就是最多可以开心的客人 答案即为

图论/暴力 Codeforces Beta Round #94 (Div. 2 Only) B. Students and Shoelaces

题目传送门 1 /* 2 图论/暴力:这是个连通的问题,每一次把所有度数为1的砍掉,把连接的点再砍掉,总之很神奇,不懂:) 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <algorithm> 7 #include <cmath> 8 using namespace std; 9 10 const int MAXN = 1e2 + 10; 11 const int INF = 0x3f3f3

暴力/进制转换 Codeforces Round #308 (Div. 2) C. Vanya and Scales

题目传送门 1 /* 2 题意:问是否能用质量为w^0,w^1,...,w^100的砝码各1个称出重量m,砝码放左边或在右边 3 暴力/进制转换:假设可以称出,用w进制表示,每一位是0,1,w-1.w-1表示砝码与物品放在一起,模拟判断每位是否ok 4 详细解释:http://blog.csdn.net/u011265346/article/details/46556361 5 总结:比赛时压根没往进制去想,连样例也不知道是怎么回事..中文不行啊:( 6 */ 7 #include <cstdi