傳送門
這個資料範圍好大啊,我想省點空間於是就想到了vector,思路很簡單,就是先根據建造時間排個序,然後從小到大的順序合併,合併一次檢查一次,如果最後祖先都一樣那麼就是通了。
#include
#include
#include
using
namespace std;
int fa[
100010];
int n,m;
struct node
;vector v;
vector
::iterator it;
bool
cmp(
const node &a,
const node &b)
intfind
(int pos)
bool
search()
}return1;
}int
main()
);}sort
(v.begin()
,v.end()
,cmp)
;int ans =0;
for(it=v.
begin()
;it!=v.
end(
);it++)if
(search()
)//printf("%d\n",cc);
//printf("%d %d %d\n",*(it));
}printf
("-1");
return0;
}
結果:
這組資料其實在第四次的時候就已經通了,但是我的fa記錄有問題,我以為讓村子號小的放在前面就可以解決問題。但又發現
用個雞兒的stl,尋常並查集即可,每次聯通一條路的時候就n–
#include
#include
#include
using
namespace std;
struct hh
a[200000];
int f[
200000
],n,m;
intcmp
(const hh &a,
const hh &b)
intfind
(int x)
//極致壓縮
intmain()
}printf
("-1");
return0;
}
並查集 洛谷P1111修復公路
這道題的演算法很直觀,對於兩個村莊之間的連線性,直接用並查集進行處理就可以。處理時要先將修路時間進行排序,然後按時間順序將相連的村莊並在同一集合裡面。include include include include include include include using namespace std...
並查集 並查集
本文參考了 挑戰程式設計競賽 和jennica的github題解 陣列版 int parent max n int rank max n void init int n int find int x else void union int x,int y else 結構體版 struct node ...
並查集入門(普通並查集 帶刪除並查集 關係並查集)
什麼是並查集?通俗易懂的並查集詳解 普通並查集 基礎並查集 例題 題解 how many tables problem description lh boy無聊的時候很喜歡數螞蟻,而且,還給每乙隻小螞蟻編號,通過他長期的觀察和記錄,發現編號為i的螞蟻會和編號為j的螞蟻在一起。現在問題來了,他現在只有...