上一页 1 ··· 12 13 14 15 16 17 18 19 20 ··· 23 下一页
摘要: 题目链接这题Balance Act那题差不多,不过这题的数据量更大,时间有点卡,我的O(N)的算法都跑了1100多MS(时限2S)。View Code 1 #include <stdio.h> 2 #include <string.h> 3 #include <vector> 4 #define N 50005 5 #define MAX(a,b) ((a)>(b)?(a):(b)) 6 using namespace std; 7 vector<int> dep[N]; 8 int u[2*N],v[2*N],first[N],next[ 阅读全文
posted @ 2012-04-27 20:04 BeatLJ 阅读(219) 评论(0) 推荐(0)
摘要: 题目链接题目大意:给定一棵树,树的每个结点有一个权值,每个结点的权值=去掉该结点后剩余的分支中结点最多的那个分支的结点数。求树中权值最小的结点。这题CE了两次,第一次因为使用了memset没包含头文件,第二次是因为GNU C++没有头文件<memory.h>,使用<string.h>就AC了。View Code 1 #include <stdio.h> 2 #include <string.h> 3 #include <vector> 4 #define N 20000 5 #define MAX(a,b) ((a)>(b)?( 阅读全文
posted @ 2012-04-27 15:08 BeatLJ 阅读(295) 评论(0) 推荐(0)
摘要: 题目链接这题要用左二子,右兄弟的存储结构来存树(附加一个结点0,将森林连成树),然后就是在二叉树上DP。View Code 1 #include <stdio.h> 2 #include <memory.h> 3 #define MAX(a,b) ((a)>(b)?(a):(b)) 4 #define N 205 5 int son[N],bro[N],w[N],n,m; 6 int c[N][N]; 7 void Insert(int u,int fa,int x) 8 { 9 w[u]=x;10 if(son[fa]==-1) son[fa]=u;11 els 阅读全文
posted @ 2012-04-27 09:50 BeatLJ 阅读(249) 评论(0) 推荐(0)
摘要: 题目链接没有上司的晚会,经典的树形DP题。View Code 1 #include <stdio.h> 2 #include <memory.h> 3 #define MAX(a,b) ((a)>(b)?(a):(b)) 4 #define N 6000 5 int a[N],p[N],d[N],c[N],vis[N],sums[N],sumgs[N],dmax,n; 6 int fd(int k) 7 { 8 if(!vis[k]) return d[k]=0; 9 if(d[k]) return d[k];10 return d[k]=fd(p[k])+1;1 阅读全文
posted @ 2012-04-26 16:38 BeatLJ 阅读(227) 评论(0) 推荐(0)
摘要: 1015 Jury Compromise1029 False coin1036 Gangsters1037 A decorative fence1038 Bugs Integrated, Inc.1042 Gone Fishing1050 To the Max1062 昂贵的聘礼1074 Parallel Expectations1080 Human Gene Functions1088 滑雪1093 Formatting Text1112 Team Them Up!1141 Brackets Sequence1143 Number Game1157 LITTLE SHOP OF FLOWER 阅读全文
posted @ 2012-04-26 13:14 BeatLJ 阅读(220) 评论(0) 推荐(0)
摘要: 题目链接题目大意:给定一棵树,求最大点独立集中点的数目。这题昨晚一开始想到可以转化为求二部图的最大匹配来做,没估计复杂度,最后超时了。后来就考虑把树分成两部分,取结点较多的那部分作为结果,提交后WA。再后来,我考虑给树定一个根结点,然后分层,统计每层的结点数目,然后再转化为DP问题去做,相当于是分层DP,结果还是WA。昨天晚上躺在床上时,突然想到曾经听说过树形DP,难道这题正是?今天翻了下刘汝佳的白书,学习了下树形DP,然后果断AC。AC的代码 1 #include <stdio.h> 2 #include <vector> 3 #define MAX(a,b) ((a 阅读全文
posted @ 2012-04-26 12:45 BeatLJ 阅读(216) 评论(0) 推荐(0)
摘要: 题目链接题目大意:给定一个只含1,2,3的数列,求排序的最小交换次数。这题说不出需要用什么算法,如果有的话,应该是贪心的思想。我的做法是,先统计1,2,3的个数,然后就知道了1,2,3应该排在哪些区间,首先将错位的两两交换(例如1在2的区间,2在1的区间),然后三个之间交换(例如1在2的区间,2在3的区间,3在1的区间)。View Code 1 #include <stdio.h> 2 #include <memory.h> 3 #define MIN(a,b) ((a)<(b)?(a):(b)) 4 #define N 1000 5 int a[N]; 6 in 阅读全文
posted @ 2012-04-25 20:39 BeatLJ 阅读(249) 评论(0) 推荐(0)
摘要: 题目链接二分查找树的模拟。不能用数组模拟,那样内存会爆掉。View Code 1 #include <stdio.h> 2 #define N 1000 3 struct node 4 { 5 int x; 6 struct node *left,*right; 7 }node[N]; 8 char f; 9 void Insert(struct node *r,struct node *p)10 {11 if(!r) return;12 if(r->x>p->x)13 {14 if(r->left) Insert(r->left,p);15 else 阅读全文
posted @ 2012-04-25 19:59 BeatLJ 阅读(257) 评论(0) 推荐(0)
摘要: 题目链接典型的DFS题,骑士周游问题。这题的关键在于字典序输出路径,要处理好搜索的顺序,另外需要注意的是,字母表示行,数字表示列。View Code 1 #include <stdio.h> 2 #include <memory.h> 3 #define N 26 4 int dx[8]={-2,-2,-1,-1,1,1,2,2}; 5 int dy[8]={-1,1,-2,2,-2,2,-1,1}; 6 char vis[N][N],ok; 7 int n,m; 8 int ans[N]; 9 void dfs(int i,int j,int cnt)10 {11 i 阅读全文
posted @ 2012-04-25 17:15 BeatLJ 阅读(305) 评论(0) 推荐(0)
摘要: 题目链接宽度优先搜索。View Code 1 #include <stdio.h> 2 #include <memory.h> 3 #include <queue> 4 #define N 300 5 using namespace std; 6 typedef pair<int,int> node; 7 queue<node> Q; 8 int dx[8]={1,1,-1,-1,2,2,-2,-2}; 9 int dy[8]={2,-2,2,-2,1,-1,1,-1};10 int n;11 int step[N][N];12 in 阅读全文
posted @ 2012-04-25 14:27 BeatLJ 阅读(212) 评论(0) 推荐(0)
上一页 1 ··· 12 13 14 15 16 17 18 19 20 ··· 23 下一页