UVALive - 7752 Free Figurines
2024-08-31 06:10:33
题意:有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;
}
最新文章
- [原] VS新添加WebApplication项目,无法运行,请求帮助,问题如何解决
- java ExecutorService
- css知多少(4)——解读浏览器默认样式
- 《Linux私房菜》笔记和问题记录
- Struts2 面试题分析
- Django1.8教程——从零开始搭建一个完整django博客(一)
- ExtJS入门教程03,form中怎能没有validation
- oracle_dblink配置
- 小知识点 LINQ中延时求值和主动求值的区别
- Eclipse 环境下安装PhoneGap开发插件
- WCF与Web API 区别(应用场景)
- 自己动手用Javascript写一个无刷新分页控件
- CSS 概览(CSS2.1)更新时间2014-0406
- (转)HTTPS到底是个啥玩意儿?
- jQuery与js例子
- 蝉知CMS本地迁移到服务器具体步骤
- Scrapy学习笔记(5)-CrawlSpider+sqlalchemy实战
- C#重点内容之:事件(Event)
- [转]用 jQuery 实现页面滚动(Scroll)效果的完美方法
- 动态加载jar包(一)