题目大意:有不超过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. go语言笔记——指针,和C用法以及本质一样,但不支持指针的+-运算!

    4.4.2 值类型和引用类型 所有像 int.float.bool 和 string 这些基本类型都属于值类型,使用这些类型的变量直接指向存在内存中的值. Go 语言的取地址符是 &,放到一个 ...

  2. P1491 集合位置 次短路

    这个题是一个次短路的裸题,就是把最短路路径求出来之后依次删边,然后跑最短路,在这些情况里取最小值就行了. 题干: 每次有大的活动,大家都要在一起“聚一聚”,不管是去好乐迪,还是避风塘,或者汤姆熊,大家 ...

  3. Tyvj 1068 巧用扩展KMP

    Tyvj1068 给定两个长度为2*10^5的字符串为A和B 求B在A中匹配后,任意匹配长度的位置个数. KMP算法大家应该烂熟于心才好,这样碰到这样的题才能灵活运用.有时做题真的需要一点灵感.首先, ...

  4. Linux VPS上安装KDE, Gnome和VNC

  5. Kafka详解与总结(二)

    Kafka Stream Kafka Streams是一个客户端库,用于构建任务关键型实时应用程序和微服务,其中输入和输出数据存储在Kafka集群中.Kafka Streams结合了在客户端编写和部署 ...

  6. [转]Linux下paste命令详解

    转自:http://blog.csdn.net/andy572633/article/details/7214126 paste单词意思是粘贴.该命令主要用来将多个文件的内容合并,与cut命令完成的功 ...

  7. Linux查找目录下的按时间过滤的文件

    在维护项目中,有时会指定都一些条件进行过滤文件,并对该批文件进行操作:这时我们将使用shell命令进行操作:直接上代码 #!/bin/sh #BEGIN #`find ./ ! -name " ...

  8. Android项目实战_手机安全卫士进程管理

    ###1.设备进程信息获取获取设备运行进程 ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVI ...

  9. html5——背景

    背景大小 background-size: % %;//宽 高 background-size: 100px 100px;//宽 高 background-size: cover;//覆盖,图片会完全 ...

  10. JS——缓动框架的问题

    1.opacity问题:IE678支持filter: alpha(opacity=50)取值1-100:小数位容易精度丢失,所i有统一json字符串设置为百进制,赋值时除以100 2.zIndex问题 ...