题意:有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. jquery对象和dom原生获取的对象是不同的。

    写了一个点击无缝滚动的demo,但是点击的时候如果上一个不运动完成,在快速点击就会快闪. 可是开始也清除定时器了,后来发现是传入的jq对象,jqobj.timer=定时器,这里jqobj没法添加.ti ...

  2. prototype入门----自定义创建元素

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. Tensorflow机器学习入门——网络可视化TensorBoard

    一.在代码中标记要显示的各种量 tensorboard各函数的作用和用法请参考:https://www.cnblogs.com/lyc-seu/p/8647792.html import tensor ...

  4. springboot 时间戳和 数据库时间相差14个小时

    在 springboot 开发过程中遇到一个奇怪的问题,就是已经设置系统时间GMT+8, 但是时间到数据库后会减少14个小时.后来发现是 jvm 时区和数据库时区设置不一致的问题. jvm 设置的是 ...

  5. Hive的存储和MapReduce处理——数据清洗

    日期:2019.11.13 博客期:115 星期三 Result文件数据说明: Ip:106.39.41.166,(城市) Date:10/Nov/2016:00:01:02 +0800,(日期) D ...

  6. 洛谷 P2891 [USACO07OPEN]吃饭Dining

    裸的最大流. #include <cstdio> #include <cstring> #include <queue> const int MAXN = 4e3 ...

  7. Python学习笔记之正则表达式

    本篇在写的时候大量参考了https://deerchao.cn/tutorials/regex/regex.htm的内容 一.什么是正则表达式 在编写处理字符串的程序或网页时,经常会有查找符合某些复杂 ...

  8. 学习进度-11 RDD 编程初级实践

    一. 请到本教程官网的“下载专区”的“数据集”中下载 chapter5-data1.txt,该数据集包含 了某大学计算机系的成绩,数据格式如下所示: Tom,DataBase,80 Tom,Algor ...

  9. PromQL操作符

    PromQL操作符 使用PromQL除了能够方便的按照查询和过滤时间序列以外,PromQL还支持丰富的操作符,用户可以使用这些操作符对进一步的对事件序列进行二次加工.这些操作符包括:数学运算符,逻辑运 ...

  10. 检测皮肤PH值、感知你的便意,健康是可穿戴设备的新风口?

    在经历最初的喧嚣与疯狂后,可穿戴设备近年来有些低调和沉寂.换句话说,虽然可穿戴设备销量在持续走高,但从形态和功能上,呈现出高度一致性.这似乎也在证明着,可穿戴设备已成为寻常可见的普通产品而已.不过在迈 ...