日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

pSort CodeForces - 28B(并查集)

發布時間:2023/12/15 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pSort CodeForces - 28B(并查集) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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(并查集)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。