【图论】C_hdu_1213 How Many Tables(dfs / uf)

一、Problem

Today is Ignatius’ birthday. He invites a lot of friends. Now it’s dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want to stay with strangers.

One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table.

For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least.

Input

The input starts with an integer T(1<=T<=25) which indicate the number of test cases. Then T test cases follow. Each test case starts with two integers N and M(1<=N,M<=1000). N indicates the number of friends, the friends are marked from 1 to N. Then M lines follow. Each line consists of two integers A and B(A!=B), that means friend A and friend B know each other. There will be a blank line between two cases.

Output

For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.

Sample Input
2
5 3
1 2
2 3
4 5

5 1
2 5

Sample Output
2
4

二、Solution

方法一:UF

import java.util.*;
import java.math.*;
import java.io.*;
public class Main{
	static class Solution {
		void init() {
			Scanner sc = new Scanner(new BufferedInputStream(System.in));
			int T = sc.nextInt();
			while (T-- > 0) {
				int N = sc.nextInt(), M = sc.nextInt();
				UF uf = new UF(N);
				for (int i = 0; i < M; i++) {
					int a = sc.nextInt(), b = sc.nextInt();
					uf.union(a, b);
				}
				System.out.println(uf.count);
			}
			
		}
		class UF {
			int[] id;
			int count;
			UF(int N) {
				count = N;
				id = new int[N+1];
				for (int i = 0; i < N; i++) {
					id[i] = i;
				}
			}
			int find(int p) {
				while (p != id[p]) {
					id[p] = id[id[p]];
					p = id[p];
				}
				return p;
			}
			void union(int p, int q) {
				int pID = find(p), qID = find(q);
				if (pID == qID)
					return;
				id[pID] = qID;
				count--;
			}
		}
	}
    public static void main(String[] args) throws IOException {  
        Solution s = new Solution();
		s.init();
    }
}

复杂度分析

  • 时间复杂度: O ( ) O()
  • 空间复杂度: O ( ) O()

方法二:dfs

import java.util.*;
import java.math.*;
import java.io.*;
public class Main{
	static class Solution {
		int[][] g;
		boolean[] vis;
		int N;
		void dfs(int x) {
			for (int i = 1; i <= N; i++) {
				if (g[x][i] > 0 && !vis[i]) {
					vis[i] = true;
					dfs(i);
				}
			}
		}
		void init() {
			Scanner sc = new Scanner(new BufferedInputStream(System.in));
			int T = sc.nextInt();
			while (T-- > 0) {
				N = sc.nextInt(); int M = sc.nextInt();
				g = new int[N+1][N+1];
				for (int i = 0; i < M; i++) {
					int a = sc.nextInt(), b = sc.nextInt();
					g[a][b] = g[b][a] = 1;
				}
				vis = new boolean[N+1];
				int tot = 0;
				for (int i = 1; i <= N; i++) {
					if (!vis[i]) {
						vis[i] = true;
						dfs(i);
						tot++;
					}
				}
				System.out.println(tot);
			}
		}
	}
    public static void main(String[] args) throws IOException {  
        Solution s = new Solution();
		s.init();
    }
}

复杂度分析

  • 时间复杂度: O ( ) O()
  • 空间复杂度: O ( ) O()
原创文章 787 获赞 314 访问量 5万+

猜你喜欢

转载自blog.csdn.net/qq_43539599/article/details/105857027