摘要: 题目大意:告诉你一堆虫子的关系,就是两只相互喜欢,让你找出有没有同性恋的虫子。题解:加权并查集,感觉比食物链简单很多,难得一次AC,我们直接定义喜欢为异性,记关系d为1,同性为2,r数组记录与祖先的关系,同性或是异性,在线做,当发现新关系与就关系矛盾时,就存在同性恋的虫子。#include usi... 阅读全文
posted @ 2014-02-10 19:05 forever97 阅读(259) 评论(0) 推荐(0) 编辑
摘要: 题目大意:在给定的序列中找到固定个数的递增的子序列,如果子序列的总个数少于要求的个数,那么就把所有的子序列输出即可。题解:本来题目也不太看得懂,在别人的博客看了许久才懂,剪枝和判重也不大会,于是暂时先把它给看懂。一个有效的剪枝,一个子串如果不成立,那么比其大的子串显然不成立,所以剪枝。#include #include #include #include using namespace std;int n,p,len,count_num;int num[1001];bool flag;typedef struct{ int n,pos;}Tem;Tem tem[1001];bool ch... 阅读全文
posted @ 2014-02-10 18:06 forever97 阅读(220) 评论(0) 推荐(0) 编辑
摘要: 题目大意:给定一个只含有MAX和+操作的式子,求加法运行了多少次,其中MAX使用宏定义。题解:注意一个规律,对于MAX(A,B)其中A中加a次,B中加b次若A>B,则加a*2+b次,否则a+b*2次。然后用递归处理这个字符串就可以了。~心力憔悴地copy了代码,果然DFS太差了……#include #include #include using namespace std; struct state{ state(int a,int b){s=a,k=b;} int s,k;//和,次数 }; state find(string str){ int ... 阅读全文
posted @ 2014-02-10 16:28 forever97 阅读(230) 评论(0) 推荐(0) 编辑
摘要: 题解:http://www.matrix67.com/blog/archives/266#include int n,ans,upperlim;void test(int row,int ld,int rd){ int pos,p; if (row!=upperlim) { pos=upperlim&(~(row|ld|rd)); while(pos) { p=pos&(~pos+1); pos=pos-p; test(row|p,(ld|p)>1); } } ... 阅读全文
posted @ 2014-02-10 16:08 forever97 阅读(142) 评论(0) 推荐(0) 编辑
摘要: 非常经典的搜索剪枝,剪枝说明见程序:#include #include using namespace std;int l,total,n,sum;struct node{ int l; bool flag; friend bool operator b.l; }}stick[100];bool dfs(int s,int nl,int pos){ if(s==total-1)return true; //最后一根不必搜,因为加起来一定为l for(int i=pos+1;i<=n;i++){ //记录上一次搜到的地方,不要从第一个重新开始搜 ... 阅读全文
posted @ 2014-02-10 15:38 forever97 阅读(198) 评论(0) 推荐(0) 编辑
摘要: 就是把数字全填上,然后检验是否可以……#include #include #include #include #define rep(i,n) for(int i=1;iq[Hash[j]])swap(Hash[i],Hash[j]); while(q[Hash[k]]==0)k++;... 阅读全文
posted @ 2014-02-10 14:26 forever97 阅读(175) 评论(0) 推荐(0) 编辑
摘要: 题解:纸牌只能移到比其大一的纸牌上,所以移动方向是定的,那么,就只有选择移动先后的问题了,对于决定要移的纸牌,比如1,如果2,3,4都是visited的状态,那么1一定是要移动到5的,因为2,3,4一定是全在5上了,清楚这一点,这道题就变得很简单的:#include #include using ... 阅读全文
posted @ 2014-02-10 14:16 forever97 阅读(175) 评论(0) 推荐(0) 编辑
摘要: 简单DFS#include bool used[21];bool prim[41];int now[21];int i,j,n;void out(){ printf("1"); for(int i=2;i<=n;i++) printf(" %d",now[i]); printf("\n");}void dfs(int step){ if (step==n&&prim[now[n]+1]) out();//注意是==,=导致WA for(int i=2;i<=n;i++) if ((!used[i])& 阅读全文
posted @ 2014-02-10 10:32 forever97 阅读(158) 评论(0) 推荐(0) 编辑
摘要: 题目大意:给定你起点S,和终点D,问你是否能在 T 时刻恰好到达终点D。单词积累:doggie 小狗 befascinatedby 被……吸引题解:首先很容易将题目误解为T时刻之前到达,那么广搜无疑,但是要在T时刻刚好到达,就只能DFS了,以下是两个剪枝:1.地图方格数减去障碍数再减1小于T,则直接无解:因为无法在T时刻到达;2.奇偶剪枝:每一步走后,曼哈顿距离与所走步数之和与T的差值必定是偶数,否则剪枝。#include#include#includeusing namespace std;int n,m,t,ex,ey;bool V[10][10],flag,ans;char map[10 阅读全文
posted @ 2014-02-10 09:06 forever97 阅读(297) 评论(0) 推荐(0) 编辑