题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4665

  本题的2-SAT建图颇为复杂,有时间再来填坑(自己写的一直挂着,标程建图太复杂了)。。。然后用暴力搜索,用一个栈保存第一个序列就可以了,因为题目是SPG,并且重复的最多只有四个,所以搜索很好过。。

 //STATUS:C++_AC_62MS_276KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD= ,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e30;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End int num[N],sta[N],vis[N];
int T,n,top1,top2,mid; bool dfs(int u)
{
if(top2==mid)return true;
if(top1<mid){
vis[u]=;
sta[top1++]=num[u];
if(dfs(u+))return true;
top1--;
}
if(num[u]==sta[top2]){
vis[u]=;
top2++;
if(dfs(u+))return true;
vis[u]=;
top2--;
}
return false;
} int main(){
// freopen("in.txt","r",stdin);
int i,j;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
mid=n>>;
for(i=;i<n;i++)
scanf("%d",&num[i]); top1=top2=;
dfs(); for(i=;i<n;i++)
printf("%d",vis[i]);
putchar('\n');
}
return ;
}

2-SAT(WA):

 /*   WA   */
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD= ,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e30;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End struct Edge{
int u,v;
}e[N<<]; int first[N<<],next[N<<],vis[N<<],S[N<<];
int num[N];
int T,n,mt,cnt; void adde(int a,int b)
{
e[mt].u=a,e[mt].v=b;
next[mt]=first[a];first[a]=mt++;
} int dfs(int u)
{
if(vis[u^])return ;
if(vis[u])return ;
int i;
vis[u]=;
S[cnt++]=u;
for(i=first[u];i!=-;i=next[i]){
if(!dfs(e[i].v))return ;
}
return ;
} int Twosat()
{
int i,j;
for(i=;i<n;i+=){
if(vis[i] || vis[i^])continue;
cnt=;
if(!dfs(i)){
while(cnt)vis[S[--cnt]]=;
if(!dfs(i^))return ;
}
}
return ;
} int cnt2[N],tot[N],w[N][];
void Init()
{
int i,j,t;
mt=;mem(vis,);
mem(first,-);
mem(tot,);
for(i=;i<n;i++)w[num[i]][tot[num[i]]++]=i;
mem(cnt2,);
for(i=;i<n;i++){
t=num[i];
cnt2[t]++;
if(cnt2[t]==){
adde((i<<)^,i<<);
adde(i<<,(w[t][tot[t]-]<<)^);
adde(w[t][tot[t]-]<<,(i<<)^);
if(tot[t]==){
for(j=w[t][]+;j<n;j++){
if(w[num[j]][]==j && w[num[j]][]<j && w[num[j]][]>i){
adde(i<<,(w[t][]<<)^);
break;
}
}
}
}
else if(cnt2[t]==){
adde(w[t][]<<,(w[t][]<<)^);
adde((w[t][]<<)^,w[t][]<<);
adde(w[t][]<<,(w[t][]<<)^);
adde((w[t][]<<)^,w[t][]<<);
}
}
} int main(){
freopen("in.txt","r",stdin);
int i,j;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=;i<n;i++)
scanf("%d",&num[i]); Init();
n<<=;
int t=Twosat();
// printf("%d\n",t); for(i=;i<n;i+=)
printf("%d",vis[i]);
putchar('\n');
}
return ;
}

HDU-4665 Unshuffle 搜索 | 2-SAT的更多相关文章

  1. HDU 4665 Unshuffle (2013多校6 1011 )

    Unshuffle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  2. HDU 4665 Unshuffle DFS找一个可行解

    每层找一对相等的整数,分别放在两个不同的串中. 参考了学弟的解法,果断觉得自己老了…… #include <cstdio> #include <cstring> #includ ...

  3. hdu 4665 搜索

    思路:直接搜索 #include<iostream> #include<cstdio> #include<algorithm> #include<cstrin ...

  4. hdu 5468(莫比乌斯+搜索)

    hdu 5468 Puzzled Elena   /*快速通道*/ Sample Input 5 1 2 1 3 2 4 2 5 6 2 3 4 5   Sample Output Case #1: ...

  5. HDU 4499.Cannon 搜索

    Cannon Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Subm ...

  6. HDU 1045 (DFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1045 题目大意:在不是X的地方放O,所有O在没有隔板情况下不能对视(横行和数列),问最多可以放多少个 ...

  7. HDU 1180 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1180 题目大意:迷宫中有一堆楼梯,楼梯横竖变化.这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达 ...

  8. HDU 2531 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2531 题目大意: 你的身体占据多个点.每次移动全部的点,不能撞到障碍点,问撞到目标点块(多个点)的最 ...

  9. HDU 1026 (BFS搜索+优先队列+记录方案)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 题目大意:最短时间内出迷宫.迷宫里要杀怪,每个怪有一定HP,也就是说要耗一定时.输出方案. 解 ...

随机推荐

  1. php站点

    thinkphp wordpress 记事狗 phpcms http://jingyan.baidu.com/article/4b07be3c61e93e48b380f3fd.html

  2. Unity3D NGUI自适应屏幕分辨率(2014/4/17更新)

    原地址:http://blog.csdn.net/asd237241291/article/details/8126619 原创文章如需转载请注明:转载自 脱莫柔Unity3D学习之旅 本文链接地址: ...

  3. POJ1002487-3279(map)

    http://poj.org/problem?id=1002 题意:是说很多公司用了容易记住的电话号码,例如有英文字母的或者是用了很多连字符或没有连字符的.每个电话号码都有标准模式,而为了统计有没有重 ...

  4. [数论]ZOJ3593 One Person Game

    题意:一个人要从A走到B  只能走a布.b步.(a+b)步,可以往左或右走   问 最少要走几步,不能走到的输出-1 可以列出方程 $ax+by=A-B$ 或者 $ax+(a+b)y=A-B$ 或者 ...

  5. HDMI介绍与流程

    HDMI接口 http://baike.c114.net/view.asp?id=17671-21565442 DDC(Display Data Channel)通道用于HDMI发送和接收端之间交换一 ...

  6. 好看的游戏soul calibur

    http://soulcalibur.fr/index.php?wiki/sophitia/ http://astuce-soluce.playfrance.com/index.php/Soul_Ca ...

  7. php Late Static Bindings延迟静态绑定

    官网说道: As of PHP 5.3.0, PHP implements a feature called late static bindings which can be used to ref ...

  8. C#.Net 如何动态加载与卸载程序集(.dll或者.exe)3---- 动态加载Assembly应用程序

    下载 supergraphfiles.exe 示例文件. 应用程序体系结构 在我专攻代码之前,我想谈谈我尝试做的事.您可能记得,SuperGraph 让您从函数列表中进行选择.我希望能够在具体的目录中 ...

  9. poj 2531 Network Saboteur( dfs )

    题目:http://poj.org/problem?id=2531 题意:一个矩阵,分成两个集合,求最大的 阻碍量 改的 一位大神的代码,比较简洁 #include<stdio.h> #i ...

  10. POJ 3281 Dining (网络流构图)

    [题意]有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料.现在有N头牛,每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享用到自己喜欢的食物和 ...