栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

abc 236 D

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

abc 236 D

abc 236 D

简单搜索:

#include 
using namespace std;
const int N =20;
int g[N][N];
int n;
int ans=0;

int f[N];
bool st[N];

void dfs(int k)
{
    if(k>2*n)
    {
        int res=0;
        for(int i=1;i<=2*n;i+=2)
        {
            res=res^(g[f[i]][f[i+1]]);
        }
        ans=max(ans,res);
    }
    for(int i=1;i<=2*n;i++)
    {
        if(st[i]) continue;
        f[k]=i;
        st[i]=true;
        dfs(k+1);
        st[i]=false;
    }
    
}
int main()
{
    cin>>n;
    int x;
    for(int i=1;i<=2*n;i++)
    	for(int j=i+1;j<=2*n;j++)
        {
            cin>>x;
            g[i][j]=g[j][i]=x;
        }
    dfs(1);
    cout< 

然而这样会超时,优化之后如下:

#include 
using namespace std;
const int N =20;
typedef long long ll;
ll g[N][N];
int n;
ll ans=0;
ll f[N];
bool st[N];

void dfs(ll res)
{
    int i=1;
    while(i<=2*n&&st[i]) i++;
    if(i>2*n){
        ans=max(res,ans);
        return ;
    }
    for(int j=i+1;j<=2*n;j++)
    if(!st[j])
    {
        st[i]=st[j]=true;
        dfs(res^g[i][j]);
        st[i]=st[j]=false;
    }
}

int main()
{
    cin>>n;
    ll x;
    for(int i=1;i<=2*n;i++)
    	for(int j=i+1;j<=2*n;j++)
        {
            cin>>x;
            g[i][j]=x;
        }
    dfs(0);
    cout<
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/717613.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号