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 }

猜你喜欢

转载自www.cnblogs.com/--HPY-7m/p/11529740.html
今日推荐