PAT甲级2018冬秋季真题题解

A Werewolf - Simple Version

  简单的枚举题。

#include<iostream>

#define ac cin.sync_with_stdio(0);

using namespace std;
const int MAXN = 110;
int arr[MAXN];
int n;

bool check(int a, int b) {
    int cnt = 0, wcnt = 0;

    for (int i = 1; i <= n; i++) {
        //猜狼人
        if (arr[i] < 0 && -arr[i] != a && -arr[i] != b) {
            ++cnt;
            if (i == a || i == b)
                ++wcnt;
        }
        //猜人
        if (arr[i] > 0 && (arr[i] == a || arr[i] == b)) {
            ++cnt;
            if (i == a || i == b)
                ++wcnt;
        }
    }
    //一个狼人说谎并且两个说谎的人
    return wcnt == 1 && cnt == 2;
}

int main() {
    ac

    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> arr[i];

    for (int i = 1; i <= n; i++) {
        for (int j = i + 1; j <= n; j++)
            if (check(i, j)) {
                cout << i << " " << j;
                return 0;
            }
    }
    cout << "No Solution";
    return 0;
}

B Dangerous Goods Packaging

  简单的集合题。

#include<iostream>
#include<cstring>

#define ac cin.tie(0);cin.sync_with_stdio(0);

using namespace std;
const int MAXN = 100010;
int a[MAXN], b[MAXN], cnt[MAXN];
int n, m;

int main() {
    ac
    int len, tmp;

    cin >> n >> m;
    for (int i = 0; i < n; i++)
        cin >> a[i] >> b[i];
    while (m--) {
        memset(cnt, false, sizeof(cnt));
        bool op = true;

        cin >> len;
        for (int i = 0; i < len; i++) {
            cin >> tmp;
            cnt[tmp] = true;
        }
        //遍历每一个pair
        for (int i = 0; i < n; i++)
        	//不合法
            if (cnt[a[i]] && cnt[b[i]]) {
                op = false;
                break;
            }
        if (op)
            cout << "Yes";
        else
            cout << "No";
        if (m)
            cout << endl;
    }
    return 0;
}

C Travelling Salesman Problem

  稍复杂的图论题,注意simple cycle和ts cycle都可能是tsp的最优解。

#include<cstdio>
#include<unordered_set>

using namespace std;
const int MAXN = 210;
int G[MAXN][MAXN], arr[MAXN];
int n, m, k;

int main() {
    int a, b, c, len;
    int ans1, ans2 = 1e6;

    scanf("%d %d", &n, &m);

    while (m--) {
        scanf("%d %d %d", &a, &b, &c);
        G[a][b] = G[b][a] = c;
    }

    scanf("%d", &k);
    for (int i = 1; i <= k; i++) {
    	//保存通过的城市数
        unordered_set<int> set;
        scanf("%d", &len);
        for (int j = 0; j < len; j++) {
            scanf("%d", &arr[j]);
            set.insert(arr[j]);
        }

        int ans = 0;
        bool op = true;
        for (int j = 1; j < len; j++) {
            if (!G[arr[j - 1]][arr[j]]) {
                op = false;
                break;
            }
            ans += G[arr[j - 1]][arr[j]];
        }
        //不连通,not a ts cycle
        if (!op)
            printf("Path %d: NA (Not a TS cycle)\n", i);
        else {
        	//首尾相连并且访问所有城市,可能是旅行商问题的可行解
            if (set.size() == n && arr[0] == arr[len - 1]) {
            	//除了首尾城市,只访问一次,简单环路
                if (len == n + 1)
                    printf("Path %d: %d (TS simple cycle)\n", i, ans);
                else
                    printf("Path %d: %d (TS cycle)\n", i, ans);
                if (ans < ans2) {
                    ans2 = ans;
                    ans1 = i;
                }
            } else
                printf("Path %d: %d (Not a TS cycle)\n", i, ans);

        }
    }
    printf("Shortest Dist(%d) = %d", ans1, ans2);
    return 0;
}

D LCA in a Binary Tree

  有难度的树题,先dfs建树,然后dfs确定节点的前驱和深度,本题还有一个难点就是需要把int映射到合理范围内。

#include<cstdio>
#include<unordered_map>

using namespace std;
const int MAXN = 10010;
int pre[MAXN], in[MAXN];
//                                  保存深度     保存前驱            
int le[MAXN], ri[MAXN], val[MAXN], dep[MAXN], qian[MAXN];
//原数字映射到范围内   范围内的数字映射到原数字
unordered_map<int, int> map1, map2;
int n, m, cnt = 0;

int buildTree(int a, int b) {
    if (a > b)
        return -1;

    int pos = map1[pre[cnt]];
    int tmp = cnt++;
    val[tmp] = pre[tmp];
    le[tmp] = buildTree(a, pos - 1);
    ri[tmp] = buildTree(pos + 1, b);
    return tmp;
}

void dfs(int root, int lev, int fa) {
    if (root == -1)
        return;

    qian[root] = fa;
    dep[root] = lev;
    dfs(le[root], lev + 1, root);
    dfs(ri[root], lev + 1, root);
}

int main() {
    int a, b, aa, bb;

    scanf("%d %d", &m, &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &in[i]);
        map1[in[i]] = i;
    }
    for (int i = 0; i < n; i++) {
        scanf("%d", &pre[i]);
        //根据前序遍历建树,所以就按照这个顺序映射
        map2[pre[i]] = i;
    }

    int root = buildTree(0, n - 1);

    dfs(root, 0, -1);

    while (m--) {
        bool op1 = true, op2 = true;
        scanf("%d %d", &a, &b);
        if (map1.find(a) == map1.end())
            op1 = false;
        if (map1.find(b) == map1.end())
            op2 = false;
        if (!op1 && !op2)
            printf("ERROR: %d and %d are not found.", a, b);
        else if (!op1)
            printf("ERROR: %d is not found.", a);
        else if (!op2)
            printf("ERROR: %d is not found.", b);
        else {
            a = map2[a];
            b = map2[b];
            aa = a;
            bb = b;
            while (aa != bb) {
                if (dep[aa] > dep[bb])
                    aa = qian[aa];
                else
                    bb = qian[bb];
            }
            if (aa == a) {
                printf("%d is an ancestor of %d.", val[a], val[b]);
            } else if (aa == b) {
                printf("%d is an ancestor of %d.", val[b], val[a]);
            } else
                printf("LCA of %d and %d is %d.", val[a], val[b], val[aa]);
        }

        if (m)
            printf("\n");
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值