這個題目求有共同愛好的人的集體數,以及從大到小輸出各個集體的人數。
並查集的思想。我的思路是:用num[i]陣列表示根為愛好i的人數個數。最後排序統計不等於的num[i]有多少個,然後排序,後序輸出。
ac**:
#include#include#include#include#include#include#include#include#include#include#include#define ll long long
#define inf 24*60*60
using namespace std;
int num[1005];
int father[1005];
int findroot(int x)
}int main()
}printf("%d\n",ans);
sort(num,num+1001);
printf("%d",num[1000]);
for(int i=999;i>=1;i--)
else
}}
pat甲級1107 並查集
並查集在findfather 函式中進行壓縮路徑,陷阱是這裡只壓縮該結點以上到根的路徑,其以下的路徑不壓縮,這裡不搞清楚會有三個測試點過不去 include include include using namespace std int n vector int hobby 1001 int fat...
PAT 甲級並查集總結
並查集 disjoint set 是一種可以動態維護若干個不重疊的集合,並支援合併與查詢兩種操作的一種資料結構。init 初始化 union int x,int y 合併兩個元素 findfather int x v找元素的所屬集合 我們建立乙個陣列father表示乙個並查集,father i 表示...
浙大pat甲級 1024
可用3個vector來進行處理,先將輸入的數字都壓入到第1第2個vector中,從i 0到k迴圈判斷是否為對稱數,如果不是對稱數則將其與倒轉數相加,並將得到的每一位數加入到第三個vector的頭部,並將新的第三個vector重新賦值給第一第二,直到該數為對稱數或i k結束迴圈。ac include ...