哈密顿回路

Time Limit: 15 Sec  Memory Limit: 256 MB

Description

  

Input

  

Output

  

Sample Input

  4 10
  0 3 2 1
  3 0 1 3
  2 1 0 2
  1 3 2 0

Sample Output

  possible
  

HINT

  

Main idea

  判断能否找到一条长度为L的哈密顿回路。

Solution

  我们直接使用Meet in middle,记录M[t][opt]表示以 t 结尾,到的点为 opt 的长度集合。然后暴力合并即可。

Code

 #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
using namespace std;
typedef long long s64; const int ONE = ;
const int Base = ;
const int INF = ; int n;
int lenA,lenB;
s64 E[][];
int Num[ONE],vis[ONE],a[ONE];
s64 Ans,L; vector <s64> M[][<<]; int get()
{
int res=,Q=; char c;
while( (c=getchar())< || c>)
if(c=='-')Q=-;
if(Q) res=c-;
while((c=getchar())>= && c<=)
res=res*+c-;
return res*Q;
} void Dfs(int u,int opt,int T,s64 Val)
{
if( Val > L) return;
if( T==lenA || T==lenB ) M[u][opt].push_back(Val);
if( T==max(lenA,lenB) ) return;
for(int v=; v<=n; v++)
{
int now = opt | (<<v-);
if(now != opt) Dfs(v,now,T+,Val+E[u][v]);
}
} int main()
{
n=get(); cin>>L;
lenA = (n+)/; lenB = (n+)-lenA;
for(int i=; i<=n; i++)
for(int j=; j<=n; j++)
{
scanf("%lld",&E[i][j]);
} Dfs(,,,); int All = (<<n)-; for(int u=;u<=All;u++)
for(int t=;t<=n;t++)
sort(M[t][u].begin(),M[t][u].end()); for(int u=;u<=All;u++)
for(int t=;t<=n;t++)
{
if(! (u&(<<t-)) ) continue;
int v = All^u | | (<<t-);
int A_size = M[t][u].size(), B_size = M[t][v].size()-;
for(int i=;i<A_size;i++)
{
s64 A = M[t][u][i]; while(B_size >= && M[t][v][B_size] + A > L) B_size--;
if(B_size < ) break;
if(M[t][v][B_size] + A == L)
{
printf("possible");
exit();
}
}
} printf("impossible");
}

【Foreign】哈密顿回路 [MIM]的更多相关文章

  1. Constraint6:更新外键约束(Foreign Key Constraint)的引用列

    在SQL Server中,表之间存在引用关系,引用关系通过创建外键约束(Foreign Key Constraint)实现.如果一个Table中的column被其他Table引用,那么该表是参考表,或 ...

  2. MySQL主从复制中断,报“Error on master: message (format)='Cannot delete or update a parent row: a foreign key constraint fails' error code=1217” 错误

    前几天,发现从库挂了,具体报错信息如下: 分析思路 1. 因为我采用的是选择性复制,只针对以下几个库进行复制: card,upay,deal,monitor,collect.所以,不太可能出现对于sa ...

  3. ORA-02266: unique/primary keys in table referenced by enabled foreign keys

    在数据库里面使用TRUNCATE命令截断一个表的数据时,遇到如下错误 SQL >TRUNCATE TABLE ESCMOWNER.SUBX_ITEM ORA-02266: unique/prim ...

  4. SQL Server 2008 R2——TRUNCATE TABLE 无法截断表 该表正由 FOREIGN KEY 约束引用

    =================================版权声明================================= 版权声明:原创文章 禁止转载  请通过右侧公告中的“联系邮 ...

  5. 【MySQL】Create table 以及 foreign key 删表顺序考究。

    1.以下是直接从数据库导出的建表语句. 1 -- ---------------------------- 2 -- Table structure for files 3 -- ---------- ...

  6. MYSQL外键(Foreign Key)的使用

    在MySQL 3.23.44版本后,InnoDB引擎类型的表支持了外键约束.外键的使用条件:1.两个表必须是InnoDB表,MyISAM表暂时不支持外键(据说以后的版本有可能支持,但至少目前不支持): ...

  7. SQL PRIMARY KEY 约束\SQL FOREIGN KEY 约束\SQL CHECK 约束

    SQL PRIMARY KEY 约束 PRIMARY KEY 约束唯一标识数据库表中的每条记录. 主键必须包含唯一的值. 主键列不能包含 NULL 值. 每个表都应该有一个主键,并且每个表只能有一个主 ...

  8. Netsuite > Foreign Currency Revaluation 外币评估

    MENU: Transactions > Financial > Revalue Open Currency Balances 使用频率: - 每个月月底,结账前, 手工操作. - 或者在 ...

  9. 无法删除对象 '产品',因为该对象正由一个 FOREIGN KEY 约束引用。

    在删除northwindcs表时,发生报错,消息 3726,级别 16,状态 1,第 2 行,无法删除对象 '产品',因为该对象正由一个 FOREIGN KEY 约束引用.此时判断是因为有其他表的外键 ...

随机推荐

  1. Hbase读写流程和寻址机制

    写操作流程 (1) Client通过Zookeeper的调度,向RegionServer发出写数据请求,在Region中写数据. (2) 数据被写入Region的MemStore,直到MemStore ...

  2. Java泛型的基本介绍与使用

    为什么要使用泛型? 在Java中增加泛型之前,泛型程序设计是用继承来实现的,例如ArrayList,只维护Object引用的数组: public class ArrayList{ private Ob ...

  3. Vue学习(四):条件渲染

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. selenide UI自动化进阶二 pageObject实现页面管理

    首先定义登录页面,上代码吧 LoginPage.java package com.test.selenium.page; import org.openqa.selenium.By; import s ...

  5. 8.0 TochAction各种用法

    1.滑动---TouchAction  支持相对坐标.绝对坐标.Element 注意看顶部的导入TouchAction这个库.. #实例化 action = TouchAction(driver) # ...

  6. CSS : @media 示例

    .example { padding: 20px; color: white; } /* Extra small devices (phones, 600px and down) */ @media ...

  7. 腾讯云,搭建 FTP 文件服务

    腾讯云,搭建 FTP 文件服务 腾讯云,搭建 FTP 文件服务 安装并启动 FTP 服务 任务时间:5min ~ 10min 安装 VSFTPD 使用 yum 安装 vsftpd: yum insta ...

  8. [leetcode-609-Find Duplicate File in System]

    https://discuss.leetcode.com/topic/91430/c-clean-solution-answers-to-follow-upGiven a list of direct ...

  9. 【历史】- Unix英雄传:图文细数十五位计算机先驱

    Unix,一款多任务多用户操作系统,最早由AT&T公司员工及合作伙伴在贝尔实验室于1969年开发完成.Unix的衍生及克隆版本包括Berkeley Unix.Minix.Linux.AIX.A ...

  10. Linux中安装apache

    一.前言 安装的背景就是公司项目要用,自己在linux安装软件不多,过程比较曲折,需要记录一下(最近几篇博客好像都会有这种高大上的背景)~先说下笔者安装环境以及相关软件版本: 操作系统:RedHat ...