zoukankan      html  css  js  c++  java
  • UVALive

    题意:有n个娃娃,如果大娃娃j直接套小娃娃i,则fa[i] = j。若fa[i] = 0,则该娃娃自由。给出每个娃娃初始的父亲,和改变后的父亲,在满足以下合法操作的条件下,问最少需要多少次变换。

    1、一个自由的娃娃可以被套在一个比自己大且为空的自由地娃娃里。

    2、打开一个非空的娃娃,取出里面的娃娃。

    分析:

    1、若一个娃娃的父亲改变了,则必须打开套在他外面的所有娃娃。

    2、若一个娃娃a成为另一个娃娃新的父亲,则这个娃娃a必须是自由的,即必须打开套在娃娃a外面的所有娃娃。

    3、所有牵扯到改变的娃娃都变成了自由,满足变换条件,因此依次嵌套即可。

    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<cmath>
    #include<iostream>
    #include<sstream>
    #include<iterator>
    #include<algorithm>
    #include<string>
    #include<vector>
    #include<set>
    #include<map>
    #include<stack>
    #include<deque>
    #include<queue>
    #include<list>
    #define lowbit(x) (x & (-x))
    const double eps = 1e-8;
    inline int dcmp(double a, double b){
        if(fabs(a - b) < eps) return 0;
        return a > b ? 1 : -1;
    }
    typedef long long LL;
    typedef unsigned long long ULL;
    const int INT_INF = 0x3f3f3f3f;
    const int INT_M_INF = 0x7f7f7f7f;
    const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
    const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
    const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
    const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
    const int MOD = 1e9 + 7;
    const double pi = acos(-1.0);
    const int MAXN = 100000 + 10;
    const int MAXT = 10000 + 10;
    using namespace std;
    int fa1[MAXN];
    int fa2[MAXN];
    bool change[MAXN];
    int main(){
        int n;
        while(scanf("%d", &n) == 1){
            memset(change, false, sizeof change);
            for(int i = 1; i <= n; ++i){
                scanf("%d", &fa1[i]);
            }
            for(int i = 1; i <= n; ++i){
                scanf("%d", &fa2[i]);
                if(fa1[i] != fa2[i]){
                    change[i] = true;
                    change[fa2[i]] = true;
                }
            }
            int ans = 0;
            for(int i = 1; i <= n; ++i){
                if(change[i] && fa1[i] != 0){
                    int cnt = 0;
                    for(int j = fa1[i]; j != 0;){
                        int tmpj = j;
                        j = fa1[j];
                        fa1[tmpj] = 0;
                        ++cnt;
                    }
                    fa1[i] = 0;
                    ans += cnt;
                }
            }
            for(int i = 1; i <= n; ++i){
                if(fa1[i] != fa2[i]){
                    ++ans;
                }
            }
            printf("%d
    ", ans);
        }
        return 0;
    }
    

      

  • 相关阅读:
    Mac OS系统安装pymssql 报错
    odoo源生打印【web report】
    《动手学深度学习》task05课后习题
    《动手学深度学习》task05笔记
    《动手学深度学习》task03课后习题
    《动手学深度学习》task03笔记
    《动手学深度学习》task04笔记
    《动手学深度学习》task04课后习题
    《动手学深度学习》task01-02疑难杂症
    《动手学深度学习》task01-02笔记
  • 原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/7454898.html
Copyright © 2011-2022 走看看