摘要:算法:就是给你一个无向图,至少添加几条边,使得从fieldsi 到fields j的路径至少有两条以上。转化为问题:添加几条边使得图变成双连通图。。方法:1.用tarjian算法缩点。。2.根据公式计算。。若要使得任意一棵树,在增加若干条边后,变成一个双连通图,那么至少增加的边数 =( 这棵树总度数为1的结点数 + 1 )/ 2来自:http://blog.csdn.net/lyy289065406/article/details/6762370#include<stdio.h>#include<stdlib.h>#include<string.h>#inc
阅读全文
摘要:题意:是求一个无向图的割点,和连通子图个数。根据割点定义:1. u为根, 则u至少有两个儿子。2. u不为根,则至少存在某一儿子节点s, low[s] >= d[u], 即s和s的后代不会追溯到比u更早的祖先点。#include<stdio.h>#include<stdlib.h>#include<string.h>#include<iostream>#include<vector>#include<string>#include<math.h>#include<map>#include<
阅读全文
摘要:1. dp求编辑距离2. bk树找相差d的单词。View Code #include<stdio.h>#include<stdlib.h>#include<string.h>#include<iostream>#include<vector>#include<string>#include<math.h>#include<map>#include<set>#include<stack>#include<queue>#include<algorithm>
阅读全文
摘要:算法:在之前搜索出状态的基础上,再压缩一次状态。View Code //by yefeng#include<iostream>using namespace std;typedef long long LL; const int mod = 9937;int mask,idx, n , m;struct Matrix{ int mat[257][257]; void zero() { memset(mat,0,sizeof(mat)); } void unit(){ memset(mat,0,sizeof(mat)); fo...
阅读全文
摘要:算法:1.状态压缩2.搜索3.矩阵快速幂View Code #include<iostream>using namespace std;typedef long long LL;const int mod = 1000000007;const int mask = 255;struct Matrix{ LL mat[100][100]; void unit(){ memset(mat,0,sizeof(mat)); for(int i = 0; i < 100; i++) mat[i][i]=1; } void zero(){...
阅读全文
摘要:算法:搜索枚举,注意FOR循环初始值。View Code #include<stdio.h>#include<stdlib.h>#include<string.h>#include<iostream>#include<vector>#include<string>#include<math.h>#include<map>#include<set>#include<algorithm>using namespace std;int mp[20][20];int hash[20]
阅读全文
摘要:对一种数据结构有两种操作:1.是插入元素2.输出中位数,并且删除之。数据结构题,splay水过。View Code #include <stdio.h>#include <string.h>#include <stdlib.h>#include <algorithm>using namespace std;//节点定义typedef struct node{ node *child[2], *pf; int v, size;}*Node;Node root = NULL;void update( Node p ){ if( p == NULL)
阅读全文
摘要:题意:求用1 * 2 的矩行拼h * w的矩阵方法数(h,w <=11)算法:1.朴素算法,根据关系,枚举由于在做第i行dp时必须完全覆盖第i-1行,只要抓住这个条件不放就行。1、如果第i行中有0,则第i-1行一定为1;2、如果第i行中为1的x列第i-1行为0,说明第i行肯定是竖着放的;3、如果第i行中为1的x列第i-1行的该列也为1,可能性只有一个,第i行是横放的,所以第i行的x+1列也必须为1,又因为第i行的x+1列为1是因为横着放的,所以第i-1行的x+1列也必须为1。View Code //自己没写枚举暴力的代码了,速度很慢2000ms,代码来自//http://gisyhy.b
阅读全文
摘要:题意:给你n件物品,三个参数描述该物品,编号,数量(<=5),每件价格。 n <= 5然后给你s种方案,第一个数位商品种类对数,然后是该商品型号,数量,最后一个数是该方案价格。求把所有物品买完最低的价格。算法1:商品种类很少,<= 5开个五维数组int dp[5][5][5][5][5],dp[a][b][c][d][e][f],表示第1中物品买a件,第2种物品买b件,。。,第5种物品买f件所需要价格然后DFS枚举每一种方案,记忆化搜索,即可以算出最优解。View Code #include<stdio.h>#include<stdlib.h>#inc
阅读全文
摘要:算法:dp[state][n]表示该状态前N位不是完美排列的个数。步骤:1.初始化第 0 位值2.第i位从第i-1位推出,每一位,枚举(1<<N)种状态。3.答案就是N!- dp[(1<<N)-1][N-1]View Code #include<stdio.h>#include<stdlib.h>#include<string.h>#include<iostream>#include<vector>#include<string>#include<math.h>#include<m
阅读全文
摘要:算法:题意比赛搞错了,只要最后彼此拿到的财务价值一样即可。搜索,2(40)次。加个if条件剪枝就可以了。View Code #include<stdio.h>#include<stdlib.h>#include<string.h>#include<iostream>#include<vector>#include<string>#include<math.h>#include<map>#include<set>#include<algorithm>using namespac
阅读全文
摘要:题意:求1-N的排列数但是有限定条件,AI,BI,该排列要满足第AI位为Bi。算法:1.裸的DFS果断TLE。。时间复杂度为N!View Code #include<stdio.h>#include<stdlib.h>#include<string.h>#include<iostream>#include<vector>#include<string>#include<math.h>#include<map>#include<set>#include<algorithm>us
阅读全文
摘要:题意:给你N个集合,k个元素,元素值小于等于14,问由这些集合可以构造出多少新的不同集合。。。算法:由元素值特别小,可以这样处理。。1,2就是set[i]第1,2位为1然后把所有集合的合并,枚举。。最后枚举1..1<<m之间哪些数存在可以组成。。View Code #include<stdio.h>#include<string.h>#include<stdlib.h>#include<algorithm>#include<queue>using namespace std;int N, M, set[110];int d
阅读全文
摘要:题意:就是有0-15个工作,每个工作有完成日期限制,还有完成这个工作所需要时间,求完成所有工作花时最少,并且输出这些顺序。。。因为工作很小,最多也就1<<15种方案,BFS枚举所有方案即可。View Code /*鐘舵€佸帇缂?+ 鎼滅储*/#include<stdio.h>#include<string.h>#include<stdlib.h>#include<queue>#include<algorithm>using namespace std;struct node{ char name[110]; int end
阅读全文
摘要:ExpressionTime Limit : 6000/2000ms (Java/Other)Memory Limit : 65535/32768K (Java/Other)Total Submission(s) : 10Accepted Submission(s) : 7Font:Times New Roman|Verdana|GeorgiaFont Size:←→Problem DescriptionYou shoule remove valid pairs of parentheses from a valid arithmetic expression,and you will get
阅读全文
摘要:算法:1.只有16种爱好,枚举所有可能,每种爱好要或不要,总共有65536种状态。2.dfs直接枚举每种状态,并保持最优解,刚开始写搓了,dfs超时,原因是搜索没有控制好,搜索的状态变成16!View Code #include<stdio.h>#include<string.h>#include<stdlib.h>#include<iostream>using namespace std;int dt[50][50];int val[50];int hash[100];int visit[100];int sum[100];int tx[20]
阅读全文
摘要:1.题意:给你个一平板由N个矩阵组成,先Y后X,给出的点是矩阵左上角和右下角。每个矩阵可以用一种颜色刷子粉刷,求把这平板按指定颜色全部涂色,并且该平板是竖的,当前粉刷的矩阵要保证其上面没有未粉刷的矩阵,求最少的换刷子次数?算法:1.根据规则建好图。2.根据N比较少,可以用dp状态压缩或直接DFS回溯,我写的是回溯,还可以用记忆化搜索。dp[i][j] = c 表示最后粉刷的是第i个矩阵,已经粉刷的矩阵状态为j, 换粉刷次数为c3. DFS 回溯View Code #include<stdio.h>#include<stdlib.h>#include<string.
阅读全文
摘要:题意:给你一个n和k,要求找出m,满足以下条件1.m的长度和n一样长2.m % k == 03.满足1,2条件,m和n的不同位数要尽量少。4.满足1,2,3条件,m要最小难点:1.n的为位数高达100位,如何处理高精度?如何解决m %k == 0方法:同余取模a + b = a % m + b % ma * b = (a %m) * (b%m)定义:int mod[110][110];mod[i][j] = [(10 ^ i ) * j] % kmod[i][j] = (mod[i-1][j] * 10) % k2.如何满足条件注意搜索的次序性。次数从0到len-1次开始搜索,先搜索小的数,再
阅读全文
摘要:算法:1.先用DFS把员工之间关系系列找出,使之连续,转换为对线段进行操作,用L【】记录左边界,R【】记录右边界。2.线段树,单点更新,区间更新,lazy延时标记。View Code #include<stdio.h>#include<stdlib.h>#include<string.h>#include<iostream>#include<vector>using namespace std;#define MAXN 51000#define LL long longstruct node{ int left, right; LL
阅读全文
摘要:1.线段树 2700ms..View Code /*Source CodeProblem: 3468 User: 314911229Memory: 12928K Time: 2704MSLanguage: G++ Result: AcceptedSource Code*/#include <stdio.h>#include <string.h>#include <stdlib.h>struct node{int l, r;long long lazy, sum;}T[500000];long long ans[101000];long long sum...
阅读全文