並查集 題1

2021-08-09 07:36:27 字數 989 閱讀 5106

問題描述

今天是ignatius的生日。他邀請很多朋友。現在是晚餐時間。ignatius想知道他至少需要多少張桌子。你必須注意到,並不是所有的朋友都認識對方,所有的朋友都不想留在陌生人身上。

這個問題的乙個重要規則是,如果我告訴你a知道b,b知道c,這意味著a,b,c彼此認識,所以他們可以留在乙個表中。

例如:如果我告訴你a知道b,b知道c,d知道e,所以a,b,c可以留在乙個表中,d,e必須留在另乙個表中。所以ignatius至少需要2張桌子。

輸入 輸入開始於表示測試用例數的整數t(1 <= t <= 25)。然後t測試用例如下。每個測試用例從兩個整數n和m開始(1 <= n,m <= 1000)。n表示朋友的數量,朋友的標記從1到n.然後m行跟隨。每行由兩個整數a和b(a!= b)組成,這意味著朋友a和朋友b彼此認識。兩種情況之間會有一條空白線。

產量 樣品輸入

2 5 3

1 2

2 3

4 55 1

2 5

樣品輸出 2 4

並查集解析:

寫的非常好!!

轉;**:

#include

#include

int pre[1005];

int t[1005];

int find(int x)

return r;

}void mix(int x,int y)

int main()

memset(t,0,sizeof(t));

for(int i=1;i<=x;i++)

t[find(i)]=1;

int ans=0;

for(int i=1;i<=x;i++)

if(t[i]==1)

ans++;

getchar();

printf("%d\n",ans);

}

return

0; }

初學並查集 並查集1

現在,我是用乙個初學者的眼光來寫並查集,此文最初寫於我學並查集的那天,後經過多次修改。1 並查集是什麼 並查集是乙個具有多個連通分支的圖,他擁有合併兩個連通分支,和查詢兩個元素是否位於同乙個連通分支的功能。2 並查集的簡單應用 並查集解決什麼問題 假如有一些點,你知道哪些點是直接相連的,但實際上間接...

並查集 帶權並查集 種類並查集 入門基礎題

include include include include include include include includetypedef long long ll using namespace std const int inf 0x3f3f3f3f const int maxn 2e5 10...

並查集簡單題

題目傳送 poj 1611 the suspects ac include include include include include include include include include include include include include include define l...