Codeforces Round #435 (Div. 2)

A. Mahmoud and Ehab and the MEX

题目链接:http://codeforces.com/contest/862/problem/A

题目意思:现在一个数列中有n个数,每个数小于等于100,现在要让这个数列的met=k,意思是如果从1-100中第一个未出现的数字为met。

题目思路:在[0,k)区间内所有在数列中不存在的数的数量+check(k),check()表示判断k是否存在,如果存在返回1,不存在返回0;

代码:

 1 //Author: xiaowuga
 2 #include <bits/stdc++.h>
 3 using namespace std;
 4 #define inf 0x3f3f3f3f
 5 #define MAX INT_MAX
 6 #define mem(s,ch) memset(s,ch,sizeof(s))
 7 const long long N=100000; 
 8 const long long mod=1e9+7; 
 9 typedef long long LL;
10 typedef int II;
11 typedef unsigned long long ull;
12 #define nc cout<<"nc"<<endl
13 #define endl "
"
14 II a[101]={0};
15 int main() {
16     ios::sync_with_stdio(false);cin.tie(0);
17     II n,x;
18     cin>>n>>x;
19     for(II i=0;i<n;i++){
20         II t;
21         cin>>t;
22         a[t]++;
23     }
24     II ans=0;
25     for(II i=0;i<x;i++){
26         if(a[i]==0) ans++;
27     }
28     if(a[x]) ans++;
29     cout<<ans<<endl;
30     return 0;
31 }
View Code

B. Mahmoud and Ehab and the bipartiteness

题目链接:http://codeforces.com/contest/862/problem/B

题目意思: 给定一个n 个节点  n-1 条边的图,求最多还能加几条边,保证 这个图不存在重边,自环,并且是一个二分图

题目思路:首先一个树是一个二分图,我们可以通过dfs可以把一个树的节点push进两个vector中,这样就构造了一个二分图,那么答案就是size1×size2-n+1.

代码:

 1 //Author: xiaowuga
 2 #include <bits/stdc++.h>
 3 using namespace std;
 4 #define inf 0x3f3f3f3f
 5 #define MAX INT_MAX
 6 #define mem(s,ch) memset(s,ch,sizeof(s))
 7 const long long N=100000; 
 8 const long long mod=1e9+7; 
 9 typedef long long LL;
10 typedef int II;
11 typedef unsigned long long ull;
12 #define nc cout<<"nc"<<endl
13 #define endl "
"
14 II n;
15 vector<int>G[100000+10];
16 vector<int>a[2];
17 void dfs(II u,II fa,II s){
18     a[s].push_back(u);
19     for(II i=0;i<G[u].size();i++){
20         II v=G[u][i];
21         if(v==fa) continue;
22         dfs(v,u,s^1);
23     }
24 }
25 int main() {
26     ios::sync_with_stdio(false);cin.tie(0);
27     cin>>n;
28     if(n==1){cout<<0<<endl;return 0;}
29     for(II i=0;i<n-1;i++){
30         II x,y;
31         cin>>x>>y;
32         G[x].push_back(y);
33         G[y].push_back(x);
34     }
35     dfs(1,0,0);
36     LL sz1=a[1].size();
37     LL sz2=a[0].size();
38     cout<<sz1*sz2-n+1<<endl;
39     return 0;
40 }
View Code
原文地址:https://www.cnblogs.com/xiaowuga/p/7686686.html