One day n cells of some array decided to play the following game. Initially each cell contains a number which is equal to it’s ordinal number (starting from 1). Also each cell determined it’s favourite number. On it’s move i-th cell can exchange it’s value with the value of some other j-th cell, if |i - j| = di, where di is a favourite number of i-th cell. Cells make moves in any order, the number of moves is unlimited.

The favourite number of each cell will be given to you. You will also be given a permutation of numbers from 1 to n. You are to determine whether the game could move to this state.

Input
The first line contains positive integer n (1 ≤ n ≤ 100) — the number of cells in the array. The second line contains n distinct integers from 1 to n — permutation. The last line contains n integers from 1 to n — favourite numbers of the cells.

Output
If the given state is reachable in the described game, output YES, otherwise NO.

Examples
Input
5
5 4 3 2 1
1 1 1 1 1
Output
YES
Input
7
4 3 5 1 2 7 6
4 6 6 1 6 6 1
Output
NO
Input
7
4 2 5 1 3 7 6
4 6 6 1 6 6 1
Output
YES
一开始以为是个数学题,没想到是个图论。。
对于这种各个元素之间有联系的,一定要往图论上想想,哪怕他不是。。
我们对于一开始可以移动的点,把它们放到一个集合中。处理完之后,判断前后两个位置上的点是否在一个集合中。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;const int maxx=1e2+100;
int f[maxx];
int a[maxx];
int n;inline int getf(int u)
{if(u==f[u]) return u;else return f[u]=getf(f[u]);
}
inline void merge(int u,int v)
{int t1=getf(u);int t2=getf(v);if(t1!=t2){f[t1]=t2;}
}
int main()
{scanf("%d",&n);for(int i=1;i<=n;i++) scanf("%d",&a[i]),f[i]=i;int x;for(int i=1;i<=n;i++){scanf("%d",&x);if(i-x>=1) merge(i,i-x);if(i+x<=n) merge(i,i+x);}int flag=0;for(int i=1;i<=n;i++){if(getf(a[i])!=getf(i)) {flag=1;break;}}if(flag) puts("NO");else puts("YES");
}

努力加油a啊,(o)/~

pSort CodeForces - 28B(并查集)相关推荐

  1. A - Cthulhu CodeForces - 103B (并查集)

    -Once upon a time a man came to the sea. The sea was stormy and dark. The man started to call for th ...

  2. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) C. String Reconstruction 并查集

    C. String Reconstruction 题目连接: http://codeforces.com/contest/828/problem/C Description Ivan had stri ...

  3. codeforces 400D Dima and Bacteria 并查集+floyd

    题目链接:http://codeforces.com/problemset/problem/400/D 题目大意: 给定n个集合,m步操作,k个种类的细菌, 第二行给出k个数表示连续的xi个数属于i集 ...

  4. Codeforces Round #375 (Div. 2) D. Lakes in Berland 并查集

    http://codeforces.com/contest/723/problem/D 这题是只能把小河填了,题目那里有写,其实如果读懂题这题是挺简单的,预处理出每一块的大小,排好序,从小到大填就行了 ...

  5. Secret Passwords CodeForces - 1263D(并查集)

    One unknown hacker wants to get the admin's password of AtForces testing system, to get problems fro ...

  6. Codeforces Global Round 14 F. Phoenix and Earthquake 思维 + 并查集

    传送门 文章目录 题意: 思路: 题意: 给你nnn个点,mmm条边,限制xxx,每个点都有沥青aia_iai​,定义合并两个点即两点之间有边且au+av≥xa_u+a_v\ge xau​+av​≥x ...

  7. Codeforces 699D Fix a Tree 并查集

    原题:http://codeforces.com/contest/699/problem/D 题目中所描述的从属关系,可以看作是一个一个块,可以用并查集来维护这个森林.这些从属关系中会有两种环,第一种 ...

  8. Codeforces Round #548 (Div. 2) C. Edgy Trees(dfs || 并查集)

    题目链接:https://codeforces.com/contest/1139/problem/C 题意:给了一棵树,n个点,m条边.让从中选k个点,使得从a1到a2,a2到a3,ak-1到ak的路 ...

  9. C. Edgy Trees---(思维题+并查集的运用)---Codeforces Round #548 (Div. 2)

    Edgy Trees time limit per test 2 seconds memory limit per test 256 megabytes 题目链接http://codeforces.c ...

最新文章

  1. JAVA 设计模式 模板方法模式
  2. TMG 2010 建立站对站***隧道
  3. 《LeetCode力扣练习》第39题 组合总和 Java
  4. Spring松耦合的个人理解和代码实例
  5. uvalive5843(最大流)
  6. js动态创建两个div 把其中一个添加到另一个的里面_我开发了一个鸿蒙在线教育APP!...
  7. 【例4-4】最小花费
  8. Java作业09-异常
  9. Data Member 的绑定
  10. html中prependid的作用,UIForm与prependId =“false”中断
  11. sqlserver 'sa'密码忘记,windows集成身份验证都登录不了解决办法
  12. Visual C++课程设计选题
  13. 西门子PLC与安川变频器Modbus通信
  14. CCTV十集大型纪录片《华尔街》高速下载地址
  15. linux 家用路由器,饱受折磨的家用路由器 | 在研究的127个家用路由器中,没有一个路由器幸免...
  16. MaNGOS开源魔兽世界服务端
  17. uefi怎么念_uefi模式无法读取u盘启动原因分析及解决方法
  18. 利用天翎知识文档+群晖NAS搭建企业知识库,享用智能检索
  19. Android加载预览PDF文件
  20. 你可以成为测试界的李子柒

热门文章

  1. IOS开发基础之网易新闻JSON转模型数组第2天
  2. 命令执行——系统命令执行(三)
  3. 硬盘温度70度正常吗_读懂固态硬盘:NVMe SSD的SMART健康数据这样看
  4. osg::ComputeBoundsVisitor用法(一)
  5. 向日葵远程使用备忘录
  6. Android开发之自定义ImageView圆角图片的方法
  7. Graphics在java的哪个包_如何在Java中成功扩展Graphics
  8. RxJava 基础扫盲
  9. 最近很火的百度MIP之 zblog改造
  10. 程序设计实习MOOC/自增和自减运算符的重载