#include<iostream>#include<cstring>#include<algorithm>#include<cmath>usingnamespace std;constint N =100010;int n;double P, R;int p[N], cnt[N], f[N];intdfs(int u){if(f[u]!=-1)return f[u];if(p[u]==-1)return f[u]=0;return f[u]=dfs(p[u])+1;}intmain(){
cin >> n >> P >> R;memset(p,-1,sizeof p);for(int i =0; i < n; i ++){int k;scanf("%d",&k);for(int j =0; j < k; j ++){int son;scanf("%d",&son);
p[son]= i;}if(!k) cin >> cnt[i];}memset(f,-1,sizeof f);double res =0;for(int i =0; i < n; i ++){if(cnt[i]){
res += cnt[i]* P *pow(1+ R /100,dfs(i));}}printf("%.1lf", res);return0;}
PAT1090:供应链最高价格
#include<iostream>#include<cstring>#include<algorithm>#include<cmath>usingnamespace std;constint N =100010;int n;double P, R;int f[N], p[N];intdfs(int u){if(f[u]!=-1)return f[u];if(p[u]==-1)return f[u]=0;return f[u]=dfs(p[u])+1;}intmain(){
cin >> n >> P >> R;memset(p,-1,sizeof p);memset(f,-1,sizeof f);for(int i =0; i < n; i ++){int x;scanf("%d",&x);
p[i]= x;}int res =0, cnt =0;for(int i =0; i < n; i ++){if(dfs(i)> res){
res =dfs(i);
cnt =1;}elseif(dfs(i)== res) cnt ++;}printf("%.2lf %d", P *pow(1+ R /100, res), cnt);return0;}
PAT1106:供应链最低价格
#include<iostream>#include<cstring>#include<algorithm>#include<cmath>usingnamespace std;constint N =100010;int n;double P, R;int p[N], f[N];bool y[N];intdfs(int u){if(f[u]!=-1)return f[u];if(p[u]==-1)return f[u]=0;return f[u]=dfs(p[u])+1;}intmain(){
cin >> n >> P >> R;memset(p,-1,sizeof p);for(int i =0; i < n; i ++){int k;scanf("%d",&k);for(int j =0; j < k; j ++){int son;scanf("%d",&son);
p[son]= i;}if(!k) y[i]=true;}memset(f,-1,sizeof f);int res =1e9, cnt =0;for(int i =0; i < n; i ++){if(y[i]){if(dfs(i)< res){
res =dfs(i);
cnt =1;}elseif(dfs(i)== res) cnt ++;}}printf("%.4lf %d\n", P *pow(1+ R /100, res), cnt);return0;}
PAT1155:对路径
#include<iostream>#include<cstring>#include<algorithm>#include<vector>usingnamespace std;constint N =1010;int n;int f[N];
vector<int> path;bool gt, lt;voiddfs(int u){
path.push_back(f[u]);if(u *2> n){
cout << path[0];for(int i =1; i < path.size(); i ++){printf(" %d", path[i]);if(path[i]> path[i -1]) gt =true;elseif(path[i]< path[i -1]) lt =true;}puts("");}if(u *2+1<= n)dfs(u *2+1);if(u *2<= n)dfs(u *2);
path.pop_back();}intmain(){
cin >> n;for(int i =1; i <= n; i ++) cin >> f[i];dfs(1);if(gt && lt)puts("Not Heap");elseif(gt)puts("Min Heap");elseputs("Max Heap");return0;}
PAT1130:中缀表达式
#include<iostream>#include<cstring>#include<algorithm>usingnamespace std;constint N =25;int n;int l[N], r[N];
string w[N];bool st[N], is_leaf[N];
string dfs(int u){
string left, right;if(l[u]!=-1){
left =dfs(l[u]);if(!is_leaf[l[u]]) left ="("+ left +")";}if(r[u]!=-1){
right =dfs(r[u]);if(!is_leaf[r[u]]) right ="("+ right +")";}return left + w[u]+ right;}intmain(){
cin >> n;for(int i =1; i <= n; i ++){
cin >> w[i]>> l[i]>> r[i];if(l[i]) st[l[i]]=true;// 判断是否时根节点if(r[i]) st[r[i]]=true;if(l[i]==-1&& r[i]==-1) is_leaf[i]=true;}int root =1;while(st[root]) root ++;
cout <<dfs(root)<< endl;return0;}
PAT1143:最低公共祖先
#include<iostream>#include<cstring>#include<algorithm>#include<unordered_map>usingnamespace std;
unordered_map<int,int> pos;// 作用是为了建立数值和坐标的映射constint N =10010;int m, n;int in[N], pre[N], seq[N];int depth[N], p[N];intbuild(int il,int ir,int pl,int pr,int d){int root = pre[pl];int k = root;
depth[root]= d;if(il < k) p[build(il, k -1, pl +1, pl +1+ k -1- il, d +1)]= root;if(ir > k) p[build(k +1, ir, pl +1+ k -1- il +1, pr, d +1)]= root;return root;}intmain(){
cin >> m >> n;for(int i =0; i < n; i ++){scanf("%d",&seq[i]);
pre[i]= seq[i];}sort(seq, seq + n);for(int i =0; i < n; i ++){
pos[seq[i]]= i;
in[i]= i;}for(int i =0; i < n; i ++) pre[i]= pos[pre[i]];build(0, n -1,0, n -1,0);while(m --){int a, b;scanf("%d %d",&a,&b);if(pos.count(a)&& pos.count(b)){
a = pos[a], b = pos[b];int x = a, y = b;while(a != b){if(depth[a]< depth[b]) b = p[b];else a = p[a];}if(a != x && a != y)printf("LCA of %d and %d is %d.\n", seq[x], seq[y], seq[a]);elseif(a == x)printf("%d is an ancestor of %d.\n", seq[a], seq[y]);elseprintf("%d is an ancestor of %d.\n", seq[a], seq[x]);}elseif(pos.count(a)&& pos.count(b)==0)printf("ERROR: %d is not found.\n", b);elseif(pos.count(a)==0&& pos.count(b))printf("ERROR: %d is not found.\n", a);elseprintf("ERROR: %d and %d are not found.\n", a, b);}return0;}
PAT1151:二叉树中的最低公共祖先
#include<iostream>#include<cstring>#include<algorithm>#include<unordered_map>usingnamespace std;int n, m;constint N =10010;int in[N], pre[N], seq[N];
unordered_map<int,int> pos;int depth[N], p[N];intbuild(int il,int ir,int pl,int pr,int d){int root = pre[pl];int k = root;
depth[root]= d;if(il < k) p[build(il, k -1, pl +1, pl +1+ k -1- il, d +1)]= root;if(ir > k) p[build(k +1, ir, pl +1+ k -1- il +1, ir, d +1)]= root;return root;}intmain(){
cin >> m >> n;for(int i =0; i < n; i ++){scanf("%d",&seq[i]);
pos[seq[i]]= i;
in[i]= i;}for(int i =0; i < n; i ++){scanf("%d",&pre[i]);
pre[i]= pos[pre[i]];}build(0, n -1,0, n -1,0);while(m --){int a, b;scanf("%d %d",&a,&b);if(pos.count(a)&& pos.count(b)){
a = pos[a], b = pos[b];int x = a, y = b;while(a != b){if(depth[a]< depth[b]) b = p[b];else a = p[a];}if(a != x && a != y)printf("LCA of %d and %d is %d.\n", seq[x], seq[y], seq[a]);elseif(a == x)printf("%d is an ancestor of %d.\n", seq[a], seq[y]);elseprintf("%d is an ancestor of %d.\n", seq[a], seq[x]);}elseif(pos.count(a)&& pos.count(b)==0)printf("ERROR: %d is not found.\n", b);elseif(pos.count(a)==0&& pos.count(b))printf("ERROR: %d is not found.\n", a);elseprintf("ERROR: %d and %d are not found.\n", a, b);}return0;}