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

析:首先,先遍历一次,如果初态和不一样,那么初态后面的要全部拿出来,然后再遍历一次,然后如果发现不一样,然后要看把末态的父结点是不是孤立的,如果不是,也要全部拿出。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
//#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e15;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 100;
const int mod = 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} int fa[maxn];
int pa[maxn];
int ans; void dfs(int x){
if(pa[x] == 0) return ;
dfs(pa[x]); pa[x] = 0;
++ans;
} int main(){
while(scanf("%d", &n) == 1){
for(int i = 1; i <= n; ++i) scanf("%d", pa+i);
for(int i = 1; i <= n; ++i) scanf("%d", fa+i);
ans = 0;
for(int i = 1; i <= n; ++i) // remove
if(pa[i] != fa[i]) dfs(i);
for(int i = 1; i <= n; ++i){ // unit
if(pa[i] == fa[i]) continue;
++ans; dfs(fa[i]);
}
printf("%d\n", ans);
}
return 0;
}

  

UVALive 7752 Free Figurines (瞎搞)的更多相关文章

  1. UVALive - 7752 Free Figurines

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

  2. URAL 1203. Scientific Conference(瞎搞)

    题目链接 本来觉得这不是经典的贪心吗..果断水一次,wa了,看了看discuss,发现貌似不好水,土土的DP了一下,复杂度很高了,又T了...然后想想单调队列,二分什么的...不好往上加,直接搞了标记 ...

  3. Codeforces Gym 100610 Problem H. Horrible Truth 瞎搞

    Problem H. Horrible Truth Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1006 ...

  4. B. Salty Fish Go! -期望题(瞎搞题)

    链接:https://www.nowcoder.com/acm/contest/104/B来源:牛客网 题意:A few days ago, WRD was playing a small game ...

  5. HDU5532 Almost Sorted Array(最长上升子序列 or 瞎搞个做差的数组)

    题目链接:点我 题意:给定一个序列,询问是否能删除一个数让它成为非递减或者非递增的序列. 比如说 删除后的序列是1 3 3 5 或者5 3 3 1 或者1 3 5 或者5 3 1 都可以.只要满足删掉 ...

  6. TOJ3097: 单词后缀 (字典树 or map瞎搞)

    传送门 (<---可以点击的~) 时间限制(普通/Java):1000MS/3000MS     内存限制:65536KByte 描述 有些英语单词后缀都是一样的,现在我们需要从给定的一堆单词里 ...

  7. 8VC Venture Cup 2016 - Elimination Round B. Cards 瞎搞

    B. Cards 题目连接: http://www.codeforces.com/contest/626/problem/B Description Catherine has a deck of n ...

  8. ubuntu--基础环境瞎搞集合

    安装ubuntu系统后有很多东西需要自己瞎搞一下,这里把一些瞎搞的过程记录在这里,方便以后重新装系统后重新配置. 一.安装. 可以在windows下制作启动盘(软碟通),然后开机u盘启动即可安装,预留 ...

  9. Codeforces631C【栈维护+瞎搞】

    题意: 百度. 思路: 如果该查询的R比前面的所有都大,那么前面所有都失效. 那么我先预处理出这些有效的. 那最坏的情况不就是栈里面元素(R)很多 n,n-1,n-2,n-3,n-4而且都是相反排序的 ...

随机推荐

  1. nginx上传文件大小

    采用nginx作反向代理,出现了一个诡异的问题,小文件可以提交,大文件会报500内部错误.这个是什么原因导致的呢? 查wiki可知,上传文件大小相关的有三个配置 client_body_buffer_ ...

  2. 学习:Dom4j和Xpath

    1.DOM4J简介 DOM4J是 dom4j.org 出品的一个开源 XML 解析包.DOM4J应用于 Java 平台,采用了 Java 集合框架并完全支持 DOM,SAX 和JAXP. DOM4J使 ...

  3. Linux系统启动流程与系统目录

    启动流程 # 1,开机BIOS自检 检查一系列的硬件,最后根据启动顺序启动,是硬盘还是光驱等 # 2,MBR引导 读硬盘0柱面0磁道1扇区的前446字节 1扇区=512字节 MBR=4446字节 四个 ...

  4. sersync之不洗澡

    inotiry图片参考 sersync图片参考 inotify文字教程 该软件对系统有要求,内核2.6以上,并且有如下目录,后面会讲解三个文件用途 [root@jokerpro ~]# uname - ...

  5. java mybatisGenerator with velocity

    mybatisGenerator + velocity 模板生成dao+ mapper,并将mysql命名规范的table name + column -> java命名规范的 Class na ...

  6. 【BZOJ】3524 [POI2014] Couriers(主席树)

    题目 传送门:QWQ 传送到洛谷QWQ 分析 把求区间第k大的改一改就ok了. 代码 #include <bits/stdc++.h> using namespace std; ; ], ...

  7. C和指针 第三章--数据

    简要概述: <C和指针>第三章对数据进行了描述. 其中主要讲解了---变量的三个属性:作用域.链接属性和存储类型. 这三个属性决定了该变量在“什么地方可以使用”以及“该变量的值能够保持多久 ...

  8. canvas下载图片

    canvas下载图片 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  9. php介绍

    PHP 简介 PHP 是服务器端脚本语言. 您应当具备的基础知识 在继续学习之前,您需要对以下知识有基本的了解: HTML CSS 如果您希望首先学习这些项目,请在我们的 首页 访问这些教程. PHP ...

  10. ELK 日志管理系统,再次尝试记录

    简介: 第二次尝试 ELK 记录... 工作流程: 1.客户端的 Logstash 将日志信息采集到之后传输给 Redis 做消息队列 2.然后服务端的 Logstash 将日志从 Redis 中取出 ...