Codeforces Round #435 (Div. 2) - A B C


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);
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值