├── README.md ├── 第一章_排序 ├── 买书.c ├── 买书2.c ├── 冒泡.c ├── 快排.c └── 桶排序.c ├── 第七章_树 ├── 7-3完整堆排序.c ├── 7-3建堆和堆排序.c └── 7-4并查集.c ├── 第三章_枚举 ├── 奥数算式-枚举.c └── 奥数算式枚举2.c ├── 第二章_栈+队列+链表 ├── 小猫钓鱼.c ├── 扑克牌.c ├── 模拟链表.c ├── 炸弹人.c ├── 解密QQ1.c ├── 解密QQ2.c └── 解密回文.c ├── 第五章_图的遍历 ├── 5-1广度优先.c ├── 5-1深度优先.c ├── 5-2图深度遍历.c ├── 5-3图广度遍历.c ├── 图遍历-bfs.c └── 图遍历-dfs.c ├── 第八章_补充一些精彩算法 ├── 8-1Kruskal算法.c ├── 8-2Prim算法.c ├── 8-2Prim算法堆优化.c ├── 8-3图的割点算法.c ├── 8-4图的割边.c └── 8-5二分图最大分配.c ├── 第六章_最短路径 ├── 6-1Floyd算法.c ├── 6-2-Dijkstra算法.c ├── 6-3-Bellman-Ford算法.c └── 6-4-Bellman-ford的队列优化.c └── 第四章_搜索 ├── dfs.c ├── dfs2.c ├── 奥数算式-搜索.c ├── 宝岛探险-bfs.c ├── 宝岛探险-dfs.c ├── 火柴棍等式.c ├── 解救小哈-bfs.c ├── 解救小哈-dfs.c └── 解救小哈bfs.c /README.md: -------------------------------------------------------------------------------- 1 | ## 《啊哈算法》书上代码 2 | 《啊哈算法》是一本基于C语言写的算法入门书,此书伴随着我走过大二和大三的算法竞赛, 在此将书上代码整理上传。 3 | -------------------------------------------------------------------------------- /第一章_排序/买书.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OctopusLian/AhaAlgorithms/e7d8d5db1818a8e6d6b471ace15e6d3f5426775f/第一章_排序/买书.c -------------------------------------------------------------------------------- /第一章_排序/买书2.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OctopusLian/AhaAlgorithms/e7d8d5db1818a8e6d6b471ace15e6d3f5426775f/第一章_排序/买书2.c -------------------------------------------------------------------------------- /第一章_排序/冒泡.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OctopusLian/AhaAlgorithms/e7d8d5db1818a8e6d6b471ace15e6d3f5426775f/第一章_排序/冒泡.c -------------------------------------------------------------------------------- /第一章_排序/快排.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OctopusLian/AhaAlgorithms/e7d8d5db1818a8e6d6b471ace15e6d3f5426775f/第一章_排序/快排.c -------------------------------------------------------------------------------- /第一章_排序/桶排序.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OctopusLian/AhaAlgorithms/e7d8d5db1818a8e6d6b471ace15e6d3f5426775f/第一章_排序/桶排序.c -------------------------------------------------------------------------------- /第七章_树/7-3完整堆排序.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OctopusLian/AhaAlgorithms/e7d8d5db1818a8e6d6b471ace15e6d3f5426775f/第七章_树/7-3完整堆排序.c -------------------------------------------------------------------------------- /第七章_树/7-3建堆和堆排序.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OctopusLian/AhaAlgorithms/e7d8d5db1818a8e6d6b471ace15e6d3f5426775f/第七章_树/7-3建堆和堆排序.c -------------------------------------------------------------------------------- /第七章_树/7-4并查集.c: -------------------------------------------------------------------------------- 1 | #include 2 | int f[10000]={0},n,m,k,sum=0; 3 | void init() 4 | { 5 | int i; 6 | for(i=1;i<=n;i++) 7 | f[i]=i; 8 | return; 9 | } 10 | 11 | int getf(int v) 12 | { 13 | if(f[v]==v) 14 | return v; 15 | else 16 | { 17 | f[v]=getf(f[v]); 18 | return f[v]; 19 | } 20 | } 21 | 22 | void merge(int v,int u) 23 | { 24 | int t1,t2; 25 | t1=getf(v); 26 | t2=getf(u); 27 | if(t1 != t2) 28 | { 29 | f[t2]=t1; 30 | } 31 | return; 32 | } 33 | 34 | int main() 35 | { 36 | int i,x,y; 37 | scanf("%d %d",&n,&m); 38 | 39 | init(); 40 | for(i=1;i<=m;i++) 41 | { 42 | scanf("%d %d",&x,&y); 43 | merge(x,y); 44 | } 45 | 46 | for(i=1;i<=n;i++) 47 | { 48 | if(f[i]==i) 49 | sum++; 50 | } 51 | printf("%d\n",sum); 52 | getchar();getchar(); 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /第三章_枚举/奥数算式-枚举.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OctopusLian/AhaAlgorithms/e7d8d5db1818a8e6d6b471ace15e6d3f5426775f/第三章_枚举/奥数算式-枚举.c -------------------------------------------------------------------------------- /第三章_枚举/奥数算式枚举2.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OctopusLian/AhaAlgorithms/e7d8d5db1818a8e6d6b471ace15e6d3f5426775f/第三章_枚举/奥数算式枚举2.c -------------------------------------------------------------------------------- /第二章_栈+队列+链表/小猫钓鱼.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OctopusLian/AhaAlgorithms/e7d8d5db1818a8e6d6b471ace15e6d3f5426775f/第二章_栈+队列+链表/小猫钓鱼.c -------------------------------------------------------------------------------- /第二章_栈+队列+链表/扑克牌.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OctopusLian/AhaAlgorithms/e7d8d5db1818a8e6d6b471ace15e6d3f5426775f/第二章_栈+队列+链表/扑克牌.c -------------------------------------------------------------------------------- /第二章_栈+队列+链表/模拟链表.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OctopusLian/AhaAlgorithms/e7d8d5db1818a8e6d6b471ace15e6d3f5426775f/第二章_栈+队列+链表/模拟链表.c -------------------------------------------------------------------------------- /第二章_栈+队列+链表/炸弹人.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OctopusLian/AhaAlgorithms/e7d8d5db1818a8e6d6b471ace15e6d3f5426775f/第二章_栈+队列+链表/炸弹人.c -------------------------------------------------------------------------------- /第二章_栈+队列+链表/解密QQ1.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OctopusLian/AhaAlgorithms/e7d8d5db1818a8e6d6b471ace15e6d3f5426775f/第二章_栈+队列+链表/解密QQ1.c -------------------------------------------------------------------------------- /第二章_栈+队列+链表/解密QQ2.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OctopusLian/AhaAlgorithms/e7d8d5db1818a8e6d6b471ace15e6d3f5426775f/第二章_栈+队列+链表/解密QQ2.c -------------------------------------------------------------------------------- /第二章_栈+队列+链表/解密回文.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OctopusLian/AhaAlgorithms/e7d8d5db1818a8e6d6b471ace15e6d3f5426775f/第二章_栈+队列+链表/解密回文.c -------------------------------------------------------------------------------- /第五章_图的遍历/5-1广度优先.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OctopusLian/AhaAlgorithms/e7d8d5db1818a8e6d6b471ace15e6d3f5426775f/第五章_图的遍历/5-1广度优先.c -------------------------------------------------------------------------------- /第五章_图的遍历/5-1深度优先.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OctopusLian/AhaAlgorithms/e7d8d5db1818a8e6d6b471ace15e6d3f5426775f/第五章_图的遍历/5-1深度优先.c -------------------------------------------------------------------------------- /第五章_图的遍历/5-2图深度遍历.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OctopusLian/AhaAlgorithms/e7d8d5db1818a8e6d6b471ace15e6d3f5426775f/第五章_图的遍历/5-2图深度遍历.c -------------------------------------------------------------------------------- /第五章_图的遍历/5-3图广度遍历.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OctopusLian/AhaAlgorithms/e7d8d5db1818a8e6d6b471ace15e6d3f5426775f/第五章_图的遍历/5-3图广度遍历.c -------------------------------------------------------------------------------- /第五章_图的遍历/图遍历-bfs.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OctopusLian/AhaAlgorithms/e7d8d5db1818a8e6d6b471ace15e6d3f5426775f/第五章_图的遍历/图遍历-bfs.c -------------------------------------------------------------------------------- /第五章_图的遍历/图遍历-dfs.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OctopusLian/AhaAlgorithms/e7d8d5db1818a8e6d6b471ace15e6d3f5426775f/第五章_图的遍历/图遍历-dfs.c -------------------------------------------------------------------------------- /第八章_补充一些精彩算法/8-1Kruskal算法.c: -------------------------------------------------------------------------------- 1 | #include 2 | struct edge{ 3 | int u; 4 | int v; 5 | int w; 6 | }; 7 | struct edge e[10]; 8 | int n,m; 9 | int f[7]={0},sum=0,count=0; 10 | void quicksort(int left,int right) 11 | { 12 | int i,j; 13 | struct edge t; 14 | if(left>right) 15 | return; 16 | 17 | i=left; 18 | j=right; 19 | while(i != j) 20 | { 21 | while(e[j].w >= e[left].w && i 2 | int e[101][101]; 3 | int match[101]; 4 | int book[101]; 5 | int n,m; 6 | int dfs(int u) 7 | { 8 | int i; 9 | for(i=1;i<=n;i++) 10 | { 11 | if(book[i]==0 && e[u][i]==1) 12 | { 13 | book[i]=1; 14 | if(match[i]==0 || dfs(match[i])) 15 | { 16 | match[i]=u; 17 | match[u]=i; 18 | return 1; 19 | } 20 | } 21 | } 22 | return 0; 23 | } 24 | 25 | int main() 26 | { 27 | int i,j,t1,t2,sum=0; 28 | scanf("%d %d",&n,&m); 29 | 30 | for(i=1;i<=m;i++) 31 | { 32 | scanf("%d%d",&t1,&t2); 33 | e[t1][t2]=1; 34 | e[t2][t1]=1; 35 | } 36 | 37 | for(i=1;i<=n;i++) 38 | match[i]=0; 39 | 40 | for(i=1;i<=n;i++) 41 | { 42 | for(j=1;j<=n;j++) 43 | book[j]=0; 44 | if(dfs(i)) 45 | sum++; 46 | } 47 | 48 | printf("%d",sum); 49 | 50 | getchar();getchar(); 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /第六章_最短路径/6-1Floyd算法.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OctopusLian/AhaAlgorithms/e7d8d5db1818a8e6d6b471ace15e6d3f5426775f/第六章_最短路径/6-1Floyd算法.c -------------------------------------------------------------------------------- /第六章_最短路径/6-2-Dijkstra算法.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OctopusLian/AhaAlgorithms/e7d8d5db1818a8e6d6b471ace15e6d3f5426775f/第六章_最短路径/6-2-Dijkstra算法.c -------------------------------------------------------------------------------- /第六章_最短路径/6-3-Bellman-Ford算法.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OctopusLian/AhaAlgorithms/e7d8d5db1818a8e6d6b471ace15e6d3f5426775f/第六章_最短路径/6-3-Bellman-Ford算法.c -------------------------------------------------------------------------------- /第六章_最短路径/6-4-Bellman-ford的队列优化.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OctopusLian/AhaAlgorithms/e7d8d5db1818a8e6d6b471ace15e6d3f5426775f/第六章_最短路径/6-4-Bellman-ford的队列优化.c -------------------------------------------------------------------------------- /第四章_搜索/dfs.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OctopusLian/AhaAlgorithms/e7d8d5db1818a8e6d6b471ace15e6d3f5426775f/第四章_搜索/dfs.c -------------------------------------------------------------------------------- /第四章_搜索/dfs2.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OctopusLian/AhaAlgorithms/e7d8d5db1818a8e6d6b471ace15e6d3f5426775f/第四章_搜索/dfs2.c -------------------------------------------------------------------------------- /第四章_搜索/奥数算式-搜索.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OctopusLian/AhaAlgorithms/e7d8d5db1818a8e6d6b471ace15e6d3f5426775f/第四章_搜索/奥数算式-搜索.c -------------------------------------------------------------------------------- /第四章_搜索/宝岛探险-bfs.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OctopusLian/AhaAlgorithms/e7d8d5db1818a8e6d6b471ace15e6d3f5426775f/第四章_搜索/宝岛探险-bfs.c -------------------------------------------------------------------------------- /第四章_搜索/宝岛探险-dfs.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OctopusLian/AhaAlgorithms/e7d8d5db1818a8e6d6b471ace15e6d3f5426775f/第四章_搜索/宝岛探险-dfs.c -------------------------------------------------------------------------------- /第四章_搜索/火柴棍等式.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OctopusLian/AhaAlgorithms/e7d8d5db1818a8e6d6b471ace15e6d3f5426775f/第四章_搜索/火柴棍等式.c -------------------------------------------------------------------------------- /第四章_搜索/解救小哈-bfs.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OctopusLian/AhaAlgorithms/e7d8d5db1818a8e6d6b471ace15e6d3f5426775f/第四章_搜索/解救小哈-bfs.c -------------------------------------------------------------------------------- /第四章_搜索/解救小哈-dfs.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OctopusLian/AhaAlgorithms/e7d8d5db1818a8e6d6b471ace15e6d3f5426775f/第四章_搜索/解救小哈-dfs.c -------------------------------------------------------------------------------- /第四章_搜索/解救小哈bfs.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OctopusLian/AhaAlgorithms/e7d8d5db1818a8e6d6b471ace15e6d3f5426775f/第四章_搜索/解救小哈bfs.c --------------------------------------------------------------------------------