Cow and Snacks(吃点心--图论转换) Codeforces Round #584 - Dasha Code Championship - Elimination Round (rated, open for everyone, Div. 1 + Div. 2)

Meaning of the questions: https://codeforc.es/contest/1209/problem/D

There are n dessert, there are k individuals, everyone has two favorite snacks, and now give them ranked teams, a person to eat, as long as everyone has their favorite snacks will be eaten by people (will not be left behind ).

If someone is not happy will be nothing to eat, ask how to arrange the unhappy people at least.

Ideas:

As a graph problem, dessert is the node that man is an edge. For each block in communication, there is always a two eat snacks, others eat a (where a is the other person will eaten).

This ensures optimal, all the answers are each connected communication block number 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 }

 

Guess you like

Origin www.cnblogs.com/--HPY-7m/p/11529740.html