题目链接: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. VC中不同类型DLL及区别

    1. DLL的概念可以向程序提供一些函数.变量或类. 静态链接库与动态链接库的区别:(1)静态链接库与动态链接库都是共享代码的方式.静态链接库把最后的指令都包含在最终生成的EXE文件中了:动态链接库不 ...

  2. stl map高效遍历删除的方法

    for(:iter!=mapStudent.end():) {      if((iter->second)>=aa)      {          //满足删除条件,删除当前结点,并指 ...

  3. CI框架大纲总结

    一.大纲 1.codeigniter框架的授课内容安排 2.codeigniter框架的简介 |-----关于框架的概念 |-----使用CI框架的好处 |-----为什么选择CI框架 3.codei ...

  4. hdu 3927 Math Geek

    纯数论题,不解释!!!! 代码如下: #include<stdio.h> int main(){ ,m; scanf("%d",&t); while(t--){ ...

  5. DP的简单应用

    Problem A:简单的图形覆盖 Time Limit:1000MS  Memory Limit:65536KTotal Submit:201 Accepted:104 Description 有一 ...

  6. Hibernate逍遥游记-第2章-使用hibernate.properties

    1. package mypack; import org.hibernate.*; import org.hibernate.cfg.Configuration; import java.util. ...

  7. Hoax or what

    Hoax or what 题意是询问一个动态序列的最小值和最大值. 可以用multiset来实现. #include <stdio.h> #include <set> usin ...

  8. WinCE设置多国语言支持

    最近项目中需要支持中(简繁)日韩英多种语言,在网上找了很多解决办法,最后发现还是MSDN最好. [c-sharp] view plaincopy [HKEY_LOCAL_MACHINE/SYSTEM/ ...

  9. git svn rebase出现了checksum mismatch的错误

    http://stackoverflow.com/questions/3156744/git-svn-rebase-checksum-mismatch This solution was the on ...

  10. [2015编程之美] 第一场C

    题目3 : 质数相关 时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 两个数a和 b (a<b)被称为质数相关,是指a × p = b,这里p是一个质数.一个集合S被 ...