Codeforces_959D_E. Mahmoud and Ehab and the xor-MST

原创 2018年04月16日 20:08:36

传送门
思路:很有趣的题目,比赛的时候一直找规律,想着可以晚点再打表,结果GG。
其实对于一个数字,它连接的点一定是lowbit(x),这样就是满足边的权值最小。
所以:这道题最后的规律就是统计1的数量。
打表:

#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
#include<fstream>
using namespace std;

const int MAXN=505;//最大点数
const int MAXM=250005;//最大边数
int F[MAXN];//并查集使用

struct Edge
{
    int u,v,w;
}edge[MAXM];//储存边的信息,包括起点/终点/权值

int tol;//边数,加边前赋值为0

void addedge(int u,int v,int w)
{
    edge[tol].u=u;
    edge[tol].v=v;
    edge[tol++].w=w;
}

bool cmp(Edge a,Edge b)//排序函数,边按照权值从小到大排序
{
    return a.w<b.w;
}

int Find(int x)
{
    if(F[x]==-1)
        return x;
    else
        return F[x]=Find(F[x]);
}

int Kruskal(int n)//传入点数,返回最小生成树的权值,如果不连通返回-1
{
    memset(F,-1,sizeof(F));
    sort(edge,edge+tol,cmp);
    int cnt=0;//计算加入的边数
    int ans=0;
    for(int i=0;i<tol;i++)
    {
        int u=edge[i].u;
        int v=edge[i].v;
        int w=edge[i].w;
        int t1=Find(u);
        int t2=Find(v);
        if(t1!=t2)
        {
            ans+=w;
            F[t1]=t2;
            cnt++;
        }
        if(cnt==n-1)
            break;
    }
    if(cnt<n-1)
        return -1;//不连通
    else
        return ans;
}

int main()
{
    for(int n=2;n<=100;n++)
    {
        tol=0;
        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                addedge(i,j,i^j);
                addedge(j,i,i^j);
            }
        }
        cout<<n<<"="<<Kruskal(n)<<endl;
    }
    return 0;
}

#include<bits/stdc++.h>
#define debug(a) cout << #a << " " << a << endl
#define LL long long
#define ull unsigned long long
#define PI acos(-1.0)
#define eps 1e-6
const int N=1e5+7;
using namespace std;
LL solve(LL x)
{
    if(x==0) return 0;
    return 2ll*solve(x>>1)+(x+1)/2;
}
int main ()
{
    //yyy_3y
    //freopen("1.in","r",stdin);
    LL n; scanf("%lld",&n);
    printf("%lld\n",solve(n-1));
    return 0;
}
版权声明: https://blog.csdn.net/yyy_3y/article/details/79965652

codeforces 959E Mahmoud and Ehab and the xor-MST

http://www.elijahqi.win/archives/2945 题目描述 Ehab is interested in the bitwise-xor operation and the...
  • elijahqi
  • elijahqi
  • 2018-04-07 10:16:58
  • 71

Codeforces 862 C. Mahmoud and Ehab and the xor (技巧)

Description Mahmoud and Ehab are on the third stage of their adventures now. As you know, Dr. Evil...
  • qq_28954601
  • qq_28954601
  • 2017-09-20 18:06:18
  • 395

Codeforces - 862C Mahmoud and Ehab and the xor

C. Mahmoud and Ehab and the xor Mahmoud and Ehab are on the third stage of their adventures...
  • lzc504603913
  • lzc504603913
  • 2017-09-20 15:55:20
  • 654

Codeforces Round #435 (Div. 2) C. Mahmoud and Ehab and the xor

Mahmoud and Ehab are on the third stage of their adventures now. As you know, Dr. Evil likes sets. T...
  • SSimpLe_Y
  • SSimpLe_Y
  • 2017-09-20 08:58:35
  • 626

Codeforces Round #435 (Div. 2): C. Mahmoud and Ehab and the xor

C. Mahmoud and Ehab and the xor time limit per test 2 seconds memory limit per test 256 ...
  • Jaihk662
  • Jaihk662
  • 2017-09-20 11:11:32
  • 224

Codeforces 282C XOR and OR【思维】

C. XOR and OR time limit per test 2 seconds memory limit per test 256 megabytes input st...
  • mengxiang000000
  • mengxiang000000
  • 2017-07-04 20:06:22
  • 169

Codeforces - 862B Mahmoud and Ehab and the bipartiteness

B. Mahmoud and Ehab and the bipartiteness Mahmoud and Ehab continue their adventures! As ev...
  • lzc504603913
  • lzc504603913
  • 2017-09-20 11:51:07
  • 482

codeforces 862B B. Mahmoud and Ehab and the bipartiteness

codeforces 862B B. Mahmoud and Ehab and the bipartiteness B. Mahmoud and Ehab and the biparti...
  • qq_39482280
  • qq_39482280
  • 2017-09-20 14:47:19
  • 385

binary string codeforces 862D 交互题,二分答案

D. Mahmoud and Ehab and the binary string time limit per test 2 seconds memory limit per ...
  • weixin_37517391
  • weixin_37517391
  • 2017-09-20 23:44:39
  • 374

Codeforces Round #435 (Div. 2) A-C题解+D代码

Codeforces Round #435 (Div. 2) A.Mahmoud and Ehab and the MEX B. Mahmoud and Ehab and the bipartiten...
  • Ever_glow
  • Ever_glow
  • 2017-09-21 17:22:49
  • 458
收藏助手
不良信息举报
您举报文章:Codeforces_959D_E. Mahmoud and Ehab and the xor-MST
举报原因:
原因补充:

(最多只允许输入30个字)