• 牛客算法周周练17A


    在这里插入图片描述
    在这里插入图片描述在这里插入图片描述
    在这里插入图片描述

    题目大意:

    给出两个生成树,每一条恰好n - 1 条边,现在定义一种操作,可以删除一条边并增加一条边,问最少几次操作才能使两棵树完全相同。

    解题思路:

    基础STL,用map映射一下 u v,第二次输入时,如果这条边是没出现过的那就说明一定要改,ans++即可。

    Code:

    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cmath>
    #include <vector>
    #include <map>
    #include <set>
    #include <cstring>
    #include <queue>
    using namespace std;
    typedef long long ll;
    typedef pair<int, int> pii;
    map<pii, int> mp;
    int main()
    {
        int n;
        cin >> n;
        for (int i = 1; i < n; i ++)
        {
            int x, y;
            cin >> x >> y;
            if (x < y)
              swap(x, y);
            mp[make_pair(x, y)]++;
        }
        int ans = 0;
        for (int i = 1; i < n; i ++)
        {
            int x, y;
            cin >> x >> y;
            if (x < y)
              swap(x, y);
            if (!mp[make_pair(x, y)])  ans ++;
        }
        cout << ans << endl;
        return 0;
    }
    
  • 相关阅读:
    第三次冲刺
    第二次冲刺
    第一次冲刺
    团队学习
    git and github
    还不够格的程序员
    CF1602F. Difficult Mountain
    线性基
    欧拉回路学习笔记
    莫比乌斯反演-学习笔记
  • 原文地址:https://www.cnblogs.com/Hayasaka/p/14294176.html
Copyright © 2020-2023  润新知