摘要: 题目链接题目大意:给定一棵树,每个结点有一个权值,一棵树的权值为所有结点的权值和,现将这棵树分为两棵子树,要使得两子树的权值差最小。我的做法是先将无根树化为有根树,然后求每棵子树的权值,最后用一次扫描求结果。需要注意的是结果要用long long型。View Code 1 #include <stdio.h> 2 #include <string.h> 3 #include <vector> 4 #define N 100000 5 #define MAX(a,b) ((a)>(b)?(a):(b)) 6 #define MIN(a,b) ((a)&l 阅读全文
posted @ 2012-04-27 22:18 BeatLJ 阅读(245) 评论(0) 推荐(0)
摘要: 题目链接这题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 阅读(217) 评论(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 阅读(292) 评论(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 阅读(240) 评论(0) 推荐(0)