2011年8月24日

以数组方式建立二叉树

摘要: sicily 1156//结点以0到1000标识,存储在数组tree[1001]中,这样就可以不用按遍历或按层遍历的方法来建立二叉树了,随时都可以插入一结点#include<iostream>#include<stack>#include<memory.h>using namespace std;int isroot[1001];bool had[1001];struct node{ char c; int l;int r;}tree[1001];void init(){ for (int i=0;i<=1000;i++) tree[i].l=tree 阅读全文

posted @ 2011-08-24 21:15 sysu_mjc 阅读(295) 评论(0) 推荐(0) 编辑

图的关键路径

摘要: #include <iostream>using namespace std; //边表示活动的网AOE网,是基于假设:活动可以并行地进行,即从某顶点出发的几条边可以同时进行const int MaxVertex=10;const int MaxEdge=50;struct Edge { int index; //邻接顶点的下标,表示第index个顶点 int weight; Edge *next;};struct item //表头结点结构{ int data; //顶点数据 ... 阅读全文

posted @ 2011-08-24 21:12 sysu_mjc 阅读(382) 评论(0) 推荐(0) 编辑

森林

摘要: //sicily 1034. Forest// 1.如果有任一节点的入度>1 或者没有根节点,则不是森林,否则:// 2.先找根节点(入度为0的节点),压入栈.// 3.对栈中的根结点(当前)删除掉,把所有子节点压入栈,重复这过程.最后若所有结点都曾压入栈中,则能构成森林,反之则说明有环存在#include<iostream> //BFS求森林深度和宽度 #include<stdio.h>#include<vector>#include<stack>#include<cstring>using namespace std;str 阅读全文

posted @ 2011-08-24 16:32 sysu_mjc 阅读(229) 评论(0) 推荐(0) 编辑

邻接链表

摘要: //sicily 1024. Magic Island#include<iostream>#include<cstring>using namespace std;struct Edge{ int v,w; int next;}edge[20010];int head[10010],curr,vis[10010],res;void add_edge(int x,int y,int d) //读入的边插入到图的邻接链表{ edge[curr].v=y;edge[curr].w=d; edge[curr].next=head[x]; head[x]=curr; ... 阅读全文

posted @ 2011-08-24 16:31 sysu_mjc 阅读(680) 评论(0) 推荐(0) 编辑

哈夫曼树

摘要: #include <iostream>#include <iomanip>#include <string>using namespace std;typedef int ElementType;struct HuffNode { ElementType data; //data表示权值数据域 int par; //par=0表明结点是独立的,par>0为它的双亲下标 int lch,rch; //lch,rch左右孩子结点在数组中的下标,0,0则表示它是独立结点};const int MAXSIZE... 阅读全文

posted @ 2011-08-24 16:28 sysu_mjc 阅读(286) 评论(0) 推荐(0) 编辑

二叉树之三

摘要: #include<iostream> #include<stack>#include <queue>using namespace std;struct BiNode{ char data; BiNode *LChild; BiNode *RChild; int ltag;int rtag;};void Creat(BiNode *root) //先序建立{ char ch; cin>>ch; if(ch=='#'){root->data=ch;root->ltag=root->rtag=0;} //#代表... 阅读全文

posted @ 2011-08-24 16:27 sysu_mjc 阅读(172) 评论(0) 推荐(0) 编辑

二叉树之二

摘要: //sicily 1156. Binary tree#include<iostream> //给出一棵二叉树,输出前序遍历序列#include<stack>using namespace std;struct node{ char c; int l,r;}tree[1010];int vis[1010],rt[1010];void add(int p,char c,int l,int r) //满足vis[p]==1且rt[p]==1 是根结点{ tree[p].c=c; vis[p]=1; if(l>0) { tr... 阅读全文

posted @ 2011-08-24 16:26 sysu_mjc 阅读(168) 评论(0) 推荐(0) 编辑

二叉树之一

摘要: //sicily 1935. 二叉树重建#include <iostream> //根据先序遍历序列和中序遍历序列建立二叉树,并按层遍历#include <string>using namespace std;string str1,str2;int n;struct Node { char ch; Node *left,*right;}node[5000];Node* built(int s1,int t1,int s2,int t2) //根据先序遍历序列和中序遍历序列建二叉树{ int m=n++; //声明为局部变量 n... 阅读全文

posted @ 2011-08-24 16:25 sysu_mjc 阅读(130) 评论(0) 推荐(0) 编辑

二叉排序树

摘要: #include <iostream>using namespace std;typedef int ElementType;struct BstNode { ElementType data; BstNode *lch; BstNode *rch;};class BstTree{public: BstNode *root;public: BstTree(){root=NULL;} void creat(); void NorecCreat(); //非递归创建二叉排序树 void inorder(){inorder(root);} v... 阅读全文

posted @ 2011-08-24 16:24 sysu_mjc 阅读(173) 评论(0) 推荐(0) 编辑

差分约束系统

摘要: //差分约束系统//定理:给定一差分约束系统Ax≤b,设G=(V,E)为其相应的约束图。//如果G不包含负权回路,即权值之和小于0 的环路。//那么x=( d(v0,v1) , d(v0,v2) , … , d(v0,vn) )是此系统的一可行解,//其中d(v0,vi)是约束图中v0到vi的最短路径(i=1,2,…,n)。//如果G包含负权回路,那么此系统不存在可行解。//所以可以用Bellman-Ford算法解决差分约束系统。//求解差分约束系统,可以转化成图论的单源最短路径问题。//观察xj-xi<=bk,会发现它类似最短路中的三角不等式d[v]<=d[u]+w[u,v]// 阅读全文

posted @ 2011-08-24 16:22 sysu_mjc 阅读(211) 评论(0) 推荐(0) 编辑

trie树

摘要: // poj4 3630 Phone List// 题意: 给一组互不相同的号码,问其中是否有号码是另一号码的前缀#include <iostream> // trie树using namespace std ;struct Node { Node *next[10]; //数字0-9 int vis; //记录该节点所代表的号码是否有被访问过}table[100000];Node *root;int cur;void init(){ //trie树的根结点不包含字符信息 root=&table[0]; f... 阅读全文

posted @ 2011-08-24 16:20 sysu_mjc 阅读(152) 评论(0) 推荐(0) 编辑

STL堆之四

摘要: #include<iostream> //自定义比较函数#include<algorithm>#include<cstring>#include<stdio.h>using namespace std;#define maxn 100010struct node{ char name[11]; int mark;}heap[2][maxn]; //heap[0],heap[1]分别表示大顶堆和小顶堆bool myless(const node& a,const node& b) //大顶堆比较函数{ r... 阅读全文

posted @ 2011-08-24 16:15 sysu_mjc 阅读(164) 评论(0) 推荐(0) 编辑

STL堆之五

摘要: //有一些随时更新的数据,求排序后在中间的那个数据//可以把数据分成两半,使用两个堆,最大堆和最小堆,一半比中间值小,一半比中间值大,//插入数据时,只需要和前一半(最大堆)的最大值和后一半(最小堆)的最小值比较即可//必须保证第1个堆(大顶堆)的堆顶结点要小于第2个堆(小顶堆)的堆顶结点,//这样小顶堆的结点就全部大于大顶堆的结点,且小顶堆的结点数只能与大顶堆的结点数相等或者比它大 1 //这样任何时候,如果两堆大小相等,则说明是偶数,不存在中间结点//如果小顶堆的结点个数大于大顶堆(>1),则中间结点就是小顶堆的堆顶结点#include<iostream> //sicil 阅读全文

posted @ 2011-08-24 16:15 sysu_mjc 阅读(237) 评论(0) 推荐(0) 编辑

STL堆之二

摘要: #include <iostream>#include <algorithm>#include <vector>#include<functional>using namespace std;int main () { int myints[] = {10,20,30,5,15}; vector<int> v(myints,myints+sizeof(myints)/sizeof(myints[0])); vector<int>::iterator ite; //heap各种操作的效果是对参数指向的vector容器里储存的 阅读全文

posted @ 2011-08-24 16:14 sysu_mjc 阅读(335) 评论(0) 推荐(0) 编辑

STL堆之三

摘要: //如果一开始用greater,则之后调用的push_heap,pop_heap,sort_heap都要带greater,否则出现异常:invalid heap#include<iostream>#include<algorithm>#include<vector>#include<functional>using namespace std;void print_ivec(vector<int>::iterator begin, vector<int>::iterator end){ for(;begin != end; 阅读全文

posted @ 2011-08-24 16:14 sysu_mjc 阅读(142) 评论(0) 推荐(0) 编辑

SPFA算法之四

摘要: //poj 3013 Big Christmas Tree#include<iostream> //邻接链表实现单源最短路SPFA #include<deque>using namespace std;#define maxn 50002const __int64 inf=(__int64)1<<63-1; //inf=4611686018427387904 //(1)#define inf (1<<63)-1 (2)const __int64 inf=(__int64)(1<<63)-1; (3)const __int64 inf= 阅读全文

posted @ 2011-08-24 16:11 sysu_mjc 阅读(162) 评论(0) 推荐(0) 编辑

SPFA算法之三

摘要: //poj 3013 Big Christmas Tree#include<iostream> #include<deque>using namespace std;#define maxn 50002const __int64 inf=(__int64)1<<63-1; struct Edge { int v; int weight; int next;}Vertex[4*maxn]; int head[maxn],curr;int cases,v,e,i,j,edge[maxn][3... 阅读全文

posted @ 2011-08-24 16:10 sysu_mjc 阅读(183) 评论(0) 推荐(0) 编辑

SPFA算法之二

摘要: //当题目中并没有明确的唯一源点,就需要添加超级源点,它到各顶点都可达,//因为如果图是非连通的,那么有些顶点将一直得不到检查,//解决方法就是在初始队列时,把所有的顶点都压入队列里#include <iostream> //poj 2983 差分约束系统,使用并查集#include <queue>using namespace std;const int MAX = 500000;const int INF = 1000000000;const int N = 2000;struct Node{ int v; int cost; int next;};Node... 阅读全文

posted @ 2011-08-24 16:08 sysu_mjc 阅读(161) 评论(0) 推荐(0) 编辑

SPFA算法之一

摘要: //poj 1511 Invitation Cardstle//有p个点和e条边,从第一个点出发,求到每个点再回来所需要的最短路之和。边是有向边。//1 <= p,e<= 1000000 显然邻接矩阵是不可能存下来的,所以用邻接表//先正向建边求一次最短路径(从顶点1到其他顶点),再反向建边再求一次最短路径,两次求和#include<iostream> using namespace std;struct Edge { int index; int weight; Edge *next;}Vertex[1000002]; ... 阅读全文

posted @ 2011-08-24 16:07 sysu_mjc 阅读(236) 评论(0) 推荐(0) 编辑

RMQ区间最值

摘要: //poj 3264 #include <iostream> //线段树,求区间的最值问题using namespace std;struct segment{ int max,min;}table[200000]; //4*50000int arr[50005],MAX,MIN;void built_tree(int s,int t,int n){ if(s==t) table[n].max=table[n].min=arr[s]; else { int mid=(s+t)/2; built... 阅读全文

posted @ 2011-08-24 16:03 sysu_mjc 阅读(194) 评论(0) 推荐(0) 编辑

导航