A. Mahmoud and Ehab and the MEX
题意:
MEX of the set就是求set里面没有的最小自然数。现在给你 n个数和目标mex。
求出最少要改几次。
POINT:
数据很小,记录数有没有出现过,比x小且没有出现过久ans++。
等于x且出现过就ans++。大于x不用考虑。
#include <iostream>
#include <stdio.h>
#include <stack>
#include <vector>
using namespace std;
#define LL long long
const int maxn = 110;
int flag[maxn];
int main()
{
int n,x;
cin>>n>>x;
int a[maxn];
for(int i=1;i<=n;i++) cin>>a[i],flag[a[i]]=1;
int ans=0;
for(int i=0;i<=100;i++)
{
if(i==x)
{
if(flag[i]==1) ans++;
break;
}
else
{
if(flag[i]==0) ans++;
}
}
printf("%d\n",ans);
}
B. Mahmoud and Ehab and the bipartiteness
题意:
给你一颗树,这棵树可以分成一个二分图。让你求还能连几条边,不破坏这个二分图。
POINT:
按照二分图定理给点染色。黑色数*白色数就是可以连的最多数量的边。
在用这个值减去n-1就是答案。
注意longlong。
#include <iostream>
#include <stdio.h>
#include <stack>
#include <vector>
using namespace std;
#define LL long long
const LL maxn = 1e5+6;
vector<LL>G[maxn];
LL flag[maxn];
LL num[2];
void dfs(LL u,LL x,LL pre)
{
flag[u]=x;
num[x]++;
for(LL i=0;i<G[u].size();i++)
{
if(pre==G[u][i]) continue;
LL v=G[u][i];
dfs(v,x^1,u);
}
}
int main()
{
LL n;
cin>>n;
num[0]=0,num[1]=0;
for(LL i=1;i<n;i++)
{
LL u,v;
cin>>u>>v;
G[u].push_back(v);
G[v].push_back(u);
}
dfs(1,1,0);
LL sum=num[0]*num[1];
printf("%lld\n",sum-n+1);
}
C. Mahmoud and Ehab and the xor
题意:
给你n和x,让你求出n个数,他们异或起来等于x。
这n个数只要不大于1e6且各不相同就行了。
x,n<=1e5.
POINT:
a^a=0 ------ a^b^c^(a^b^c)^x=x。
我们在(1,1e5)里随便拿n-3个数。
c=这些数的异或值。
a=1<<18. 大于1e5且小于1e6就行。
b=1<<17。
那么答案便是,这n-3个数 + a , b , a^b^c^x。
#include <iostream>
#include <stdio.h>
#include <stack>
#include <vector>
#include <math.h>
using namespace std;
#define LL long long
const LL maxn = 1e5+6;
int main()
{
int n,x;cin>>n>>x;
if(n==1)
{
printf("YES\n%d\n",x);
}
else if(n==2)
{
if(x==0) printf("NO\n");
else printf("YES\n0 %d\n",x);
}
else
{
int a=1<<18;
int b=1<<17;
printf("YES\n");
int temp=0;
for(int i=1;i<=n-3;i++)
{
printf("%d ",i);
temp=temp^i;
}
printf("%d %d %d",a,b,a^b^temp^x);
}
}