2012年3月26日星期一

UGG SaleThe problem with the two most commonly used methods

The problem with the two most commonly used methods, one is the use of union-find sets,Christian Louboutin Sale, using a graph depth-first traversal / * HDOJ 1232 expedite project using union-find sets do * / # include < iostream>
using namespace STD; define INF 999int N #,Dre Beats Headphones, M int p [1005];; int rank [1005]; int Find (int x) {int, y, W, root; y = x; while (P [y],Dr Dre Headphones! = y) {y = P [y];} root = y; y = x; while (P [y]! = y) {w = P [y]; P
[y] = root; y = w;} return root;} void Union (int x, int y) {if (rank [x] < = rank [y]) {P [x] = y; if (rank = = rank [[x] rank y]) [y] + +;} elsep [y] = x;} void Make_set (int n) {for (int i = 0; i<
n; I + +) {P [i] = I; rank [i] = 0;}} int main () {int i, J, a, B, x, y, f; while (scanf ("%d" & &, N); & N) {Make_set (N); cin> > M; if (M = = 0) {printf ("%d, n",
N-1; continue;}); for (I = 1; i< M; I + +) {cin> > a> > B; X = Find (a); y = Find (b); if (x! = y) Union (x, y);} f = 0; for (I = 1; i< N; I + +) if (P [i] = = I) F + +; printf ("%d,
n" return, F-1);} 0;} / * HDOJ 1232 project smooth map DFS do, have a look the number of connected components * / # include < iostream> using namespace STD; int graph [1005] [1005] [1005]; bool
visited; int N, M,UGG Sale, f;Void Create_Graph () {int i, J, a, B; for (I = 1; i< I = N; for + +) (J = 1; j< N; j + +) {graph [i] [J] = 0;} for (I = 0; i< M; I + +) {cin> > a> > B; graph [a]
[b] = 1; graph [b] [a] = 1;}} void DFS (int V) {visited [v] = 1; for (int i = 1; i< = N;; I + +) if (graph [v] [i] & & visited DFS,Moncler boots! [i]) (I);} void DFS_Traverse () {for (int i = 1; i< N; I
+ +) visited [i] = 0; F = 0; for (I = 1; i< N; I + +) {if (! Visited [i]) {F + +; DFS (I);}}} int main () {while (cin> > N) {if (N = = 0) break; cin> > M; if (M = = 0) cout< < N-1<
Related articles:

没有评论:

发表评论