
PAT
andream7
这个作者很懒,什么都没留下…
展开
-
【树】1020 Tree Traversals (25分) 树的遍历
原题链接原创 2020-06-12 10:17:03 · 159 阅读 · 0 评论 -
【树】1004 Counting Leaves (30分)数叶子节点
原题链接DFS + 邻接表(数组模拟)邻接表:对每一个点开一个单链表#include <iostream>#include <cstring>#include <algorithm>using namespace std;const int N = 110;int n, m; //节点总数,内部节点数量int h[N], e[N], ne[N], idx;int cnt[N]; //存每一层的叶子节点数int max_depth; //最大层数原创 2020-06-11 19:06:12 · 132 阅读 · 0 评论 -
并查集
在近乎O(1)的时间复杂度下支持两个操作:将两个集合合并询问两个元素是否在一个集合中基本原理:每个集合用一棵树维护,根节点的编号是整个集合的编号,对于每一个点都存储父节点,p[x]表示父节点,若想确定某个点属于哪一个集合,就先确定这个点的父节点,知道找到树根位置,根节点编号即数组编号。如何判断树根: p[x] = x如何求x的集合编号while(p[x] != x) x = p[x];如何合并两个集合:若px是x的集合编号,py是y的集合编号,p[x] = py;相当于把x原创 2020-06-11 18:07:05 · 123 阅读 · 0 评论 -
数组模拟单链表&双链表(静态链表)
单链表:邻接表(邻接表主要用于存储图和树,如最短路,最小生成树,最大流)双链表:优化用数组模拟单链表,首先需要定义表示val的数组e[N],表示某个点的next指针值的数组ne[N],e和ne之间用相同的下标关联,idx存储当前用到了哪个地址(相当于开始指向第一个结点的指针,每次把idx指向的结点分配出去之后,idx向后移动一位),head指向头结点。初始化void init(){ head = -1; idx = 0;}把新结点x插到头结点的位置把x的指针指向当前的头结点.原创 2020-06-11 17:22:03 · 218 阅读 · 0 评论 -
1154 Vertex Coloring (25分)
分析:图的存储:结构体对每个询问,枚举所有边,判断每个边两个端点的颜色是否一样若每个边的两个端点的颜色都不一样,则求一共用了几种颜色求用了几种颜色用set通过代码:#include <iostream>#include <cstring>#include <unordered_set>using namespace std;const...原创 2020-05-02 18:22:30 · 126 阅读 · 2 评论 -
1146 Topological Order (25分)
思路:拓扑排序:在有向图中,若存在一条从顶点 A 到顶点 B 的路径,那么在序列中顶点 A 出现在顶点 B 的前面。对于每个询问序列,先存储每个节点在序列中的下标,建立一个下标数组再遍历每条边,判断起点下标是否比终点下标小(即判断起点是否在终点前面)只用枚举边而不用遍历图,故用最简便的结构体来存储每条边通过代码:#include <iostream>#include ...原创 2020-05-02 17:09:34 · 197 阅读 · 1 评论 -
1142 Maximal Clique (25分)
分析:判断是否是团 clique,即如果任意两个点之间都有边相连则是团,否则就不是团再判断是否是最大团maximal,即遍历所有不在集合中的剩余的点,看是否存在集合外的点满足和集合中所有的结点相连,如果存在一个这样的点就不是最大团Maximal clique题目中的数据范围较小,可以直接使用邻接表的方式存储图通过代码:#include <iostream>#includ...原创 2020-05-02 16:45:51 · 188 阅读 · 0 评论 -
1139 First Contact (30分)
思路:每一个人名映射到一个整数,用邻接矩阵存储图可能会有-0000的情况产生,故用字符串而不是int的方式读入补充知识:来源:C++STL中的unique函数解析unique函数通常和erase函数一起使用,来达到删除重复元素的目的。(注:此处的删除是真正的删除,即从容器中去除重复的元素,容器的长度也发生了变换;而单纯的使用unique函数的话,容器的长度并没有发生变化,只是元素的位...原创 2020-05-02 16:16:18 · 197 阅读 · 1 评论 -
1134 Vertex Cover (25分)
思路:把给定集合的每一个点标记,再判断每条边是否至少有一个点在给定的集合里面通过代码:#include <iostream>#include <cstring>using namespace std;const int N = 10010;int n, m;struct Edge{ int a, b;}e[N];bool st[N];...原创 2020-05-01 23:47:52 · 93 阅读 · 0 评论 -
1126 Eulerian Path (25分)
分析:每个点度数:每个点所连通的边数Eulerian:连通,所有点度数均为偶数Semi-Eulerian:连通,有两个点度数为奇数,其他点度数为偶数Non-Eulerian:不符合以上两种情况步骤:判断连通性:深搜或者宽搜遍历整个图,从某个点出发判断能否把图中的每一个点都遍历判断每个点的奇偶性通过代码:#include <cstring>#include <...原创 2020-05-01 21:30:03 · 153 阅读 · 0 评论 -
1122 Hamiltonian Cycle (25分)
分析:哈密顿回路:从起点出发,每个点走一遍,恰好回到起点哈密顿回路需要满足的条件:判断起点终点是否相同路径连通(过程中每两个点之间都有边)所有点都会访问一遍所有点数和为n+1通过代码:#include <iostream>#include <cstring>using namespace std;const int N = 210;int ...原创 2020-05-01 20:58:32 · 116 阅读 · 0 评论 -
1111 Online Map (30分)
#include <cstring>#include <iostream>#include <vector>using namespace std;const int N = 510;int n, m, S, T;int d1[N][N], d2[N][N];//d1存放length ,d2存放timeint dist1[N], dist2...原创 2020-05-01 10:25:22 · 147 阅读 · 0 评论 -
1087 All Roads Lead to Rome (30分)
#include <iostream>#include <cstring>#include <unordered_map>#include <vector>using namespace std;const int N = 210;int n, m;int w[N];int d[N][N];int dist[N], cnt[N...原创 2020-04-30 18:35:14 · 83 阅读 · 0 评论 -
1034 Head of a Gang (30分)
#include <cstring>#include <iostream>#include <vector>#include <algorithm>#include <unordered_map>using namespace std;int n, k;unordered_map<string, vector<...原创 2020-04-29 18:45:38 · 148 阅读 · 0 评论 -
1030 Travel Plan (30分)
思路:改进版Dijkstra算法#include <iostream>#include <cstring>#include <vector>using namespace std;const int N = 510;int n, m, S, T;int d[N][N], c[N][N];int dist[N], cost[N], pre[N...原创 2020-04-29 13:22:21 · 89 阅读 · 0 评论 -
1003 Emergency (25分) && Dijkstra算法
Dijkstra算法求单源最短路径:从一个点出发,到其他所有点的最短路径朴素版双重for循环时间复杂度:N^2堆优化时间复杂度:N^2logN#include <cstring>#include <iostream>using namespace std;const int N = 510;int n, m, S, T;int w[N], d[...原创 2020-04-29 13:13:23 · 148 阅读 · 0 评论 -
1094 The Largest Generation (25分)
思路:找出节点个数最多的一层层序遍历、DFS通过代码:不用queue用vector实现DFS#include <iostream>#include <cstring>#include <vector>using namespace std;const int N = 110;int n, m;bool g[N][N]; //邻接矩...原创 2020-04-27 21:42:12 · 97 阅读 · 0 评论 -
1053 Path of Equal Weight (30分) ♡
#include <iostream>#include <cstring>#include <vector>#include <algorithm>using namespace std;const int N = 110;int n, m, S;int w[N];bool g[N][N];vector<vector&l...原创 2020-04-27 21:11:27 · 94 阅读 · 0 评论 -
1061 Dating (20分)
思路:前两对字符串找两个相同的大写英文字母——字母本身的字典序——星期、钟头后面两对字符串找一个相同的英文字母——出现位置——分钟一对字符相同:字符、位置均相同前两个字符串中的第一对——要能正确代表星期(A-G)前两个字符串中的第二对——位于第一对后面,正确代表小时(0-9 A-N)输入格式:4 行中分别给出 4 个非空、不包含空格、且长度不超过 60 的字符串。输出格式:在...原创 2020-04-22 23:44:28 · 134 阅读 · 0 评论 -
1071 Speech Patterns (25分)
通过代码:#include <iostream>#include <unordered_map>using namespace std;bool check(char c){ if (c >= '0' && c <= '9') return true; if (c >= 'A' && c <...原创 2020-04-22 23:24:48 · 94 阅读 · 0 评论 -
1050 String Subtraction (20分)
#include <iostream>#include <unordered_set>using namespace std;string s1, s2;int main(){ getline(cin, s1); getline(cin, s2); unordered_set<char> hash; // 定义哈希表...原创 2020-04-22 23:08:21 · 113 阅读 · 0 评论 -
1036 Boys vs Girls (25分)
#include <iostream>using namespace std;int main(){ int n; string girl_name, girl_id; // 女生第一名的信息 int girl_score; string boy_name, boy_id; // 男生第一名的信息 int boy_score;...原创 2020-04-22 20:34:42 · 569 阅读 · 0 评论 -
1035 Password (20分)
通过代码:#include <iostream>using namespace std;const int N = 1010;string name[N], pwd[N];string change(string str){ string res; for (auto c : str) //范围遍历 if (c == '1') r...原创 2020-04-22 20:02:11 · 111 阅读 · 0 评论 -
1006 Sign In and Sign Out (25分)
通过代码:#include <iostream>using namespace std;int main(){ string open_id, open_time; string close_id, close_time; int n; cin >> n; for(int i = 0; i < n; i++){ ...原创 2020-04-22 18:03:48 · 108 阅读 · 0 评论 -
PAT乙级 1019 数字黑洞 (20分)
#include <iostream>#include <algorithm>using namespace std;bool cmp(char a, char b) {return a > b;}int main() { string s; cin >> s; s.insert(0, 4 - s.length(), '...原创 2020-03-23 21:29:53 · 124 阅读 · 0 评论 -
PAT乙级 1048 数字加密 (20分)
#include <iostream>#include <algorithm>using namespace std;int main() { string a, b, c; cin >> a >> b; int lena = a.length(), lenb = b.length(); //首先将a和b倒置,将...原创 2020-03-23 21:13:20 · 254 阅读 · 0 评论 -
PAT乙级 1033 旧键盘打字 (20分)
#include <iostream>#include <cctype>using namespace std;int main() { string bad, should; getline(cin, bad); getline(cin, should); for (int i = 0, len = should.length(); i < l...原创 2020-03-23 20:54:48 · 140 阅读 · 0 评论 -
PAT乙级 1034 有理数四则运算 (20分)
#include <iostream>#include <cmath>using namespace std;long long a, b, c, d;//计算t1和t2的最大公约数long long gcd(long long t1, long long t2) { return t2 == 0 ? t1 : gcd(t2, t1 % t2);}//...原创 2020-03-23 20:47:05 · 154 阅读 · 0 评论 -
PAT乙级 1015 德才论 (25分)
1.用结构体存储。vector v[4]中v[0]保存第一类考生,v[1]保存第二类考生……以此类推。2.写好cmp函数很重要,cmp函数中,排序先按照总分排序,然后按照德分排序,最后按照才分排序#include <iostream>#include <algorithm>#include <vector>using namespace std;//...原创 2020-03-23 20:27:02 · 87 阅读 · 0 评论 -
PAT乙级 1025 反转链表 (25分)
给定一个常数 K 以及一个单链表 L,请编写程序将 L 中每 K 个结点反转。例如:给定 L 为 1→2→3→4→5→6,K 为 3,则输出应该为 3→2→1→6→5→4;如果 K 为 4,则输出应该为 4→3→2→1→5→6,即最后不到 K 个元素不反转。输入格式:每个输入包含 1 个测试用例。每个测试用例第 1 行给出第 1 个结点的地址、结点总个数正整数 N (≤10^5)、以及正整数 ...原创 2020-03-23 19:35:44 · 135 阅读 · 0 评论