题意:有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\n", ans);
}
return 0;
}

  

UVALive - 7752 Free Figurines的更多相关文章

  1. UVALive 7752 Free Figurines (瞎搞)

    题意:给定 n 个盒子,然后告诉你每个盒子在哪个盒子里,数值越大,盒子越大,给定你初态,和末态,问你最少要几步能完成,只有两种操作,一种是把一个盒子连同里面的小盒子放到一个空盒子里,另一种是把一个堆盒 ...

  2. UVALive - 4108 SKYLINE[线段树]

    UVALive - 4108 SKYLINE Time Limit: 3000MS     64bit IO Format: %lld & %llu Submit Status uDebug ...

  3. UVALive - 3942 Remember the Word[树状数组]

    UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...

  4. UVALive - 3942 Remember the Word[Trie DP]

    UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...

  5. 思维 UVALive 3708 Graveyard

    题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...

  6. UVALive 6145 Version Controlled IDE(可持久化treap、rope)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  7. UVALive 6508 Permutation Graphs

    Permutation Graphs Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit ...

  8. UVALive 6500 Boxes

    Boxes Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Pract ...

  9. UVALive 6948 Jokewithpermutation dfs

    题目链接:UVALive 6948  Jokewithpermutation 题意:给一串数字序列,没有空格,拆成从1到N的连续数列. dfs. 可以计算出N的值,也可以直接检验当前数组是否合法. # ...

随机推荐

  1. 在这之后的两天又出现了w3wp进程找不到的情况了

    在这之后的两天又出现了w3wp进程找不到的情况了,我做了什么操作呢?无非就是vs中给一个过程附加删除了了一些dll,然后不停的重新生成解决方案,生成成功后,要调试,发现进程又没了. 实验了上面的方法, ...

  2. 关于Android file.createNewFile() 方法出现的问题总结(转)

    原文:http://blog.csdn.net/wjdarwin/article/details/7108606 今天在编写向SDcard中,创建文件夹并向其中保存文件的过程中出现个一系列的问题 在此 ...

  3. C/C++ - 多线程

    前几天简单对C和C++中的创建多线程的函数进行了测试,这篇随笔就简单介绍一下创建线程的相关函数. C中三个创建线程函数:pthread_create()._beginthread().CreateTh ...

  4. Centos7 使用yum安装MariaDB与MariaDB的简单配置与使用

    一.mariadb的安装 MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可. 开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源的潜 ...

  5. Spring MVC中的ResponseEntity和ResponseBody的区别

    1.ResponseEntity的优先级高于@ResponseBody. 在不是ResponseEntity的情况下才去检查有没有@ResponseBody注解. 如果响应类型是ResponseEnt ...

  6. eclispe+maven+ssm+sql_server/mysql配置

    链接: https://pan.baidu.com/s/1_BFI8XfS8l89-3-1IjlVZg 密码: x9in

  7. 吴裕雄--天生自然HADOOP学习笔记:Shell工具使用

    实验目的 学习使用xshell工具连接Linux服务器 在连上的服务器中进入用户目录 熟悉简单的文件操作命令 实验原理 熟悉shell命令是熟悉使用linux环境进行开发的第一步,我们在linux的交 ...

  8. primecoin服务常用命令和参数说明

    Primecoin命令: 启动服务:./primecoind -daemon -rpcuser=user -rpcpassword=password -txindex=1 -addrindex=1 - ...

  9. 利用Python进行多项式拟合

    多项式拟合的简单代码: import matplotlib.pyplot as plt import numpy as np x=[,,,,,,,] y=[,,,,,,,] a=np.polyfit( ...

  10. ROS学习笔记11-写一个简单的服务和客户端(C++版本)

    本文主要来源于:http://wiki.ros.org/ROS/Tutorials/WritingServiceClient%28c%2B%2B%29 写一个服务节点.在创建消息和服务中,我们创建了一 ...