UVALIVE 6958 Indoorienteering
题目大意:有不超过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的更多相关文章
- UVALive - 4108 SKYLINE[线段树]
UVALive - 4108 SKYLINE Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- 思维 UVALive 3708 Graveyard
题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...
- UVALive 6145 Version Controlled IDE(可持久化treap、rope)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- UVALive 6508 Permutation Graphs
Permutation Graphs Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit ...
- UVALive 6500 Boxes
Boxes Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Pract ...
- UVALive 6948 Jokewithpermutation dfs
题目链接:UVALive 6948 Jokewithpermutation 题意:给一串数字序列,没有空格,拆成从1到N的连续数列. dfs. 可以计算出N的值,也可以直接检验当前数组是否合法. # ...
- 【暑假】[实用数据结构]UVAlive 3135 Argus
UVAlive 3135 Argus Argus Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %l ...
随机推荐
- bzoj 3895 取石子 —— 博弈论
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3895 看了博客:https://blog.csdn.net/popoqqq/article/ ...
- E20170630-ts
displacement n. 取代,替代; 免职,停职; [船] 排水量; [化] 置换;
- 使用js模拟ecshop元素挪移
<!DOCTYPE html><html><head> <title>移动</title> <script src="jqu ...
- [Swift通天遁地]七、数据与安全-(5)高效操作SQLite数据库
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- 03-vue实例生命周期和vue-resource
vue实例的生命周期 什么是生命周期:从Vue实例创建.运行.到销毁期间,总是伴随着各种各样的事件,这些事件,统称为生命周期! 生命周期钩子:就是生命周期事件的别名而已: 生命周期钩子 = 生命周期函 ...
- 349 Intersection of Two Arrays 两个数组的交集
给定两个数组,写一个函数来计算它们的交集.例子: 给定 num1= [1, 2, 2, 1], nums2 = [2, 2], 返回 [2].提示: 每个在结果中的元素必定是唯一的. 我们 ...
- HeadFirst HTML&CSS
CH1 认识HTML HTML和CSS是我们用来创建网页的语言:HTML是超文本标记语言(HyperText Markup Language)的缩写,用来建立网页的结构:CSS是层叠样式表(Casca ...
- 增加删除div
<!doctype html><html><head><meta charset="utf-8"><title>无标题文 ...
- win32绘图基础
获取设备环境句柄: (1)WM_PAINT消息中: PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd,&ps); EndPaint(hwnd,&ps ...
- linux 安装 mongo
整个安装过程:下载安装包--> 解压,添加系统路径(是滴,不用安装解压即可) --> 创建数据目录 --> 启动mongod服务 --> 运行mongo 1.下载安装包 Mon ...