题目大意:有不超过14个点组成的完全图,给出邻接矩阵,问是否存在长度为W的欧拉回路?

     数据范围:n<=14, w<=1e15;

     standard input/output 7 s, 256 MB

分析:直接暴力是14!的复杂度,显然不能通过;

   考虑折半搜索,我们取0号点为起点,然后把所有的点分成两半;

   两边分别暴力跑,最后检查一边,看另一边是否有可行的路径即可;

   复杂度为C(N,N/2)*(n/2)!;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <cassert>
#include <ctime>
#define rep(i,m,n) for(i=m;i<=(int)n;i++)
#define inf 0x3f3f3f3f
#define mod 1000000007
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
#define ls (rt<<1)
#define rs (rt<<1|1)
#define all(x) x.begin(),x.end()
const int maxn=1e5+;
const int N=4e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qmul(ll p,ll q,ll mo){ll f=;while(q){if(q&)f=(f+p)%mo;p=(p+p)%mo;q>>=;}return f;}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,k,t,c[];
ll w,d[][];
set<ll>dp[];
bool flag;
void dfs(int pos,int sta,ll now,int lim,int tp)
{
if(flag)return;
int i;
sta|=(<<pos);
if(lim==)
{
if(tp==)
{
rep(i,,n-)
{
if(c[tp]>>i&)continue;
if(dp[i].find(w-now-d[i][pos])!=dp[i].end())
{
flag=true;
return;
}
}
}
else dp[pos].insert(now);
return;
}
rep(i,,n-)
{
if((sta>>i&)||(c[tp]>>i&)==)continue;
dfs(i,sta,now+d[pos][i],lim-,tp);
}
}
int main(){
int i,j;
scanf("%d%lld",&n,&w);
rep(i,,n-)rep(j,,n-)scanf("%lld",&d[i][j]);
if(n==)
{
if(d[][]+d[][]==w)puts("possible");
else puts("impossible");
}
else
{
int all=((<<n)-);
for(i=all-;i;i=((i-)&all))
{
if(__builtin_popcount(i)!=n/)continue;
c[]=c[]=;
rep(j,,n-)
{
if(i>>j&)c[]|=(<<j);
else c[]|=(<<j);
}
dfs(,,,n/,);
dfs(,,,n--n/,);
rep(j,,n-)dp[j].clear();
if(flag)break;
}
puts(flag?"possible":"impossible");
}
return ;
}

UVALIVE 6958 Indoorienteering的更多相关文章

  1. UVALive - 4108 SKYLINE[线段树]

    UVALive - 4108 SKYLINE Time Limit: 3000MS     64bit IO Format: %lld & %llu Submit Status uDebug ...

  2. UVALive - 3942 Remember the Word[树状数组]

    UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...

  3. UVALive - 3942 Remember the Word[Trie DP]

    UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...

  4. 思维 UVALive 3708 Graveyard

    题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...

  5. UVALive 6145 Version Controlled IDE(可持久化treap、rope)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  6. UVALive 6508 Permutation Graphs

    Permutation Graphs Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit ...

  7. UVALive 6500 Boxes

    Boxes Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Pract ...

  8. UVALive 6948 Jokewithpermutation dfs

    题目链接:UVALive 6948  Jokewithpermutation 题意:给一串数字序列,没有空格,拆成从1到N的连续数列. dfs. 可以计算出N的值,也可以直接检验当前数组是否合法. # ...

  9. 【暑假】[实用数据结构]UVAlive 3135 Argus

    UVAlive 3135 Argus Argus Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %l ...

随机推荐

  1. P2453 [SDOI2006]最短距离 dp

    自己想出来了!这个dp比较简单,而且转移也很简单,很自然,直接上代码就行了. 题干: 一种EDIT字母编辑器,它的功能是可以通过不同的变换操作可以把一个源串X [l..m]变换为新的目标串y[1..n ...

  2. scrapy xpath中提取多个class值

    xpath中没有提供对class的原生查找方法.但是 stackoverflow 看到了一个很有才的回答: This selector should work but will be more eff ...

  3. E20171014-hm

    Sibling   n. 兄弟,姐妹; [生] 同科,同属; [人] 氏族成员;

  4. P3399 丝绸之路(线性二维dp)

    P3399 丝绸之路 题目背景 张骞于公元前138年曾历尽艰险出使过西域.加强了汉朝与西域各国的友好往来.从那以后,一队队骆驼商队在这漫长的商贸大道上行进,他们越过崇山峻岭,将中国的先进技术带向中亚. ...

  5. SpringBoot2.0整合Redission

    Redisson是redis一个很强大的客户端,有兴趣的同学可以看我的下一篇文章,这篇主要讲如何整合,费话不多说,直接上干货(大牛请绕道) 首先创建RedissionConfig文件 import o ...

  6. python 生成器函数.推导式.生成器表达式

    一.生成器 什么是生成器,生成器的实质就是迭代器 在python中有三种方式来获取生成器: 1.通过生成器函数 2.通过各种推导式来实现生成器 3.通过数据的转换也可以获取生成器 1 def func ...

  7. android ListView,GridView 设置某一项显示位置

    在项目中有时会用到,当使用 listview  想让它显示某一项,当它又不在前面的位置,可以 使用 //让某一项显示出来(否则可能不在当前) listview.setSelection(positio ...

  8. [转]Linux之ACL权限

    转自:http://www.2cto.com/os/201110/108736.html 引言 前面的内容中,我们讲到传统的权限仅有三种身份(owner,group,others)搭配三种权限(r,w ...

  9. lua_string_pattern

    两大特点: 1. string库中所有的字符索引从前往后是1,2,...;从后往前是-1,-2,... 2. string库中所有的function都不会直接操作字符串,而是返回一个新的字符串. 库函 ...

  10. linux,apache,mysql,php常用查看版本信息的方法

    1. 查看linux的内核版本,系统信息,常用的有三种办法: uname -a; cat /proc/version; -bash-4.2$ uname -a Linux apphost -.el7. ...