题意:有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. js 判断时间大小

    //判断结束时间一定要大于开始时间 function comparativeTime(){ var isok=true; //早餐配送时间 var breakfastScanTimeMin = $(& ...

  2. C-当把数组传递给函数

    #include <stdio.h> /** * C语言中,数组的名称就是 一连串连续内存的起始地址, * 因此给数组传递给函数,传递的就是数组元素类型的指针 */ ]); void he ...

  3. Indexed (materialized) views in SQL Server,different with Oracle (materialized) views

    Thanks to MS sql could have materialized views ,similar with oracle MVs, using indexed views. what i ...

  4. Maven项目-Tomcat - 方法无法为jsp编译类ClassFormatException的解决

    解决方法:

  5. 微信小程序中,如何实现显示,隐藏密码的功能

    最近在搞小程序的开发,遇到隐藏,显示密码的功能的时候,电脑上调试没问题,但是手机上面点击却没有效果,必须要跳转到其他页面再跳回来,才能正常显示. 一时间搞得我很头疼,查找资料后,终于知道了是什么原因. ...

  6. routes 学习

    对于routes的学习,感觉还是看官方文档理解的比较快,主要说明connect和resource Setting up routes¶ It is assumed that you are using ...

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

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

  8. Vue 集成easyUI

    原 Vue 集成easyUI https://blog.csdn.net/m0_37948170/article/details/84960320   参考vue官网用cli创建了Vue项目之后: n ...

  9. 如何更改placeholder属性中文字颜色

    如何更改placeholder属性中文字颜色 placeholder这个属性是HTML5中新增的属性,该属性的作用是规定可描述输入字段预期值的简短的提示信息,该提示会在用户输入之前显示在输入字段中,会 ...

  10. php 键值数组搜索查询

    php  键值数组查询 ,需要先将其转换为纯数组,然后才能用in_array 进行搜索. $arr_combos = [ ['id' => '1001', 'value' => 'zs' ...