본문 바로가기
알고리즘/백준

[JAVA]백준_4195_친구네트워크

by 박 현 황 2021. 3. 26.

문제링크

https://www.acmicpc.net/problem/4195

 

4195번: 친구 네트워크

첫째 줄에 테스트 케이스의 개수가 주어진다. 각 테스트 케이스의 첫째 줄에는 친구 관계의 수 F가 주어지며, 이 값은 100,000을 넘지 않는다. 다음 F개의 줄에는 친구 관계가 생긴 순서대로 주어진

www.acmicpc.net

 

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.StringTokenizer;

public class Main {

	static int F;
	static int parents[];
	static int cnt[];
	static HashMap<String, Integer> map;

	static void make() {
		parents = new int[F*2];
		cnt = new int[F*2];
		
		for(int i=0;i<F*2;i++)
			parents[i] = i;
		Arrays.fill(cnt, 1);
	}

	static int findSet(int a) {
		if (parents[a] == a)
			return a;

		return parents[a] = findSet(parents[a]);
	}

	static int union(int a, int b) {
		int aRoot = findSet(a);
		int bRoot = findSet(b);

		if (aRoot == bRoot)
			return cnt[aRoot];

		if (aRoot > bRoot) {
			parents[bRoot] = aRoot;
			cnt[aRoot] += cnt[bRoot];
			return cnt[aRoot];
		} else {
			parents[aRoot] = bRoot;
			cnt[bRoot] += cnt[aRoot];
			return cnt[bRoot];
		}
	}

	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;

		int T = Integer.parseInt(br.readLine());

		for (int t = 0; t < T; t++) { // 테스트 케이스

			F = Integer.parseInt(br.readLine()); // 친구관계
			map = new HashMap<>();
			make();
			
			int idx = 0;
			
			for (int f = 0; f < F; f++) {
				st = new StringTokenizer(br.readLine());

				String name1 = st.nextToken();
				String name2 = st.nextToken();
				
				// 키가 맵에 존재하면 pass//존재하지 않으면 넣어주기
				if (!map.containsKey(name1)) {
					map.put(name1, idx++);
				}
				if (!map.containsKey(name2)) {
					map.put(name2, idx++);
				}

				System.out.println(union(map.get(name1), map.get(name2)));
				// cnt배열은
			}
		}
	}

	static class Node {
		String name;
		int cnt;

		public Node(String name, int cnt) {
			super();
			this.name = name;
			this.cnt = cnt;
		}
	}
}

 

문제링크

https://www.acmicpc.net/problem/419

 

4195번: 친구 네트

 

첫째 줄에 테스트 케이스의 개수가 주어진다. 각 테스트 케이스의 첫째 줄에는 친구 관계의 수 F가 주어지며, 이 값은 100,000을 넘지 않는다. 다음 F개의 줄에는 친구 관계가 생긴 순서대로 주

 

www.acmicpc.n

 

 

 

 

import java.io.BufferedRe

import java.io.IOException

import java.io.InputStreamReader

import java.util.Arrays

import java.util.HashMap

import java.util.StringTokenizer

 

public class Main

 

 static int 

 static int parents[]

 static int cnt[]

 static HashMap<String, Integer> map

 

 static void make()

  parents = new int[F*2]

  cnt = new int[F*2]

 

  for(int i=0;i<F*2;i++

   parents[i] = i

  Arrays.fill(cnt, 1)

 

 

 static int findSet(int a)

  if (parents[a] == a

   return a

 

  return parents[a] = findSet(parents[a]

 

 

 static int union(int a, int b)

  int aRoot = findSet(a)

  int bRoot = findSet(b)

 

  if (aRoot == bRoo

   return cnt[aRoot]

 

  if (aRoot > bRoot)

   parents[bRoot] = aRoot

   cnt[aRoot] += cnt[bRoot]

   return cnt[aRoot]

  } else 

   parents[aRoot] = bRoot

   cnt[bRoot] += cnt[aRoot]

   return cnt[bRoot]

  

 

 

 public static void main(String[] args) throws NumberFormatException, IOException

  BufferedReader br = new BufferedReader(new InputStreamReader(System.in))

  StringTokenizer st

 

  int T = Integer.parseInt(br.readLine()

 

  for (int t = 0; t < T; t++) { // 테스트 케

 

   F = Integer.parseInt(br.readLine()); // 친구

   map = new HashMap<>()

   make()

  

   int idx = 0

  

   for (int f = 0; f < F; f++) 

    st = new StringTokenizer(br.readLine())

 

    String name1 = st.nextToken(

    String name2 = st.nextToken()

   

    // 키가 맵에 존재하면 pass//존재하지 않으면 넣어주

    if (!map.containsKey(name1)) 

     map.put(name1, idx++)

    

    if (!map.containsKey(name2)) 

     map.put(name2, idx++)

    

 

    System.out.println(union(map.get(name1), map.get(name2))

    // cnt배열

   

  

 

 

 static class Node

  String name

  int cnt

 

  public Node(String name, int cnt)

   super()

   this.name = name

   this.cnt = cnt

  

 

 

 

 

 

https://www.acmicpc.net/problem/4

 

 

 

4195번: 친

 

 

 

첫째 줄에 테스트 케이스의 개수가 주어진다. 각 테스트 케이스의 첫째 줄에는 친구 관계의 수 F가 주어지며, 이 값은 100,000을 넘지 않는다. 다음 F개의 줄에는 친구 관계가 생긴 순서

 

 

 

www.acmic

 

 

 

 

 

 

 

 

 

import java.io.

 

import java.io.IOExcepti

 

import java.io.InputStreamRead

 

import java.util.Arra

 

import java.util.HashM

 

import java.util.StringTokeniz

 

 

 

public class 

 

 

 

 static 

 

 static int parents

 

 static int cnt

 

 static HashMap<String, Integer> m

 

 

 

 static void ma

 

  parents = new int[F*

 

  cnt = new int[F*

 

 

  for(int i=0;i<F*2;i+

 

   parents[i] =

 

  Arrays.fill(cnt, 

 

 

 

 

 static int findSet(int

 

  if (parents[a] ==

 

   return

 

 

 

  return parents[a] = findSet(parent

 

 

 

 

 static int union(int a, int

 

  int aRoot = findSet(

 

  int bRoot = findSet(

 

 

 

  if (aRoot == 

 

   return cnt[aRoo

 

 

 

  if (aRoot > bR

 

   parents[bRoot] = aRo

 

   cnt[aRoot] += cnt[bRoo

 

   return cnt[aRoo

 

  } els

 

   parents[aRoot] = bRo

 

   cnt[bRoot] += cnt[aRoo

 

   return cnt[bRoo

 

 

 

 

 

 

 public static void main(String[] args) throws NumberFormatException, IOExcept

 

  BufferedReader br = new BufferedReader(new InputStreamReader(System.in

 

  StringTokenizer 

 

 

 

  int T = Integer.parseInt(br.readLi

 

 

 

  for (int t = 0; t < T; t++) { // 테

 

 

 

   F = Integer.parseInt(br.readLine()); /

 

   map = new HashMap<>

 

   make

 

 

 

   int idx =

 

 

 

   for (int f = 0; f < F; f++

 

    st = new StringTokenizer(br.readLine(

 

 

 

    String name1 = st.nextTo

 

    String name2 = st.nextToken

 

 

 

    // 키가 맵에 존재하면 pass//존재하지 않으면 넣

 

    if (!map.containsKey(name1)

 

     map.put(name1, idx+

 

  

 

    if (!map.containsKey(name2)

 

     map.put(name2, idx+

 

  

 

 

 

    System.out.println(union(map.get(name1), map.get(nam

 

    // cnt

 

 

 

 

 

 

 

 

 static class N

 

  String na

 

  int c

 

 

 

  public Node(String name, int 

 

   super

 

   this.name = na

 

   this.cnt = c

 

 

 

 

 

 

 

 

 

 

 초반에}}};;; {;; {}}}은);};{};{기 ;);;{ ; ;;관계이스);;; {}};;;{;;; {;t);; {});;) {};;) ;; {;;;F; {;;;;;ader;et어진워크5

   ntme()cnt)ntmeode

     배열e2))  +))   +)) 어주  ()ken()))    0  ()()/ 친구스트 케ne()st))ion

   t]t]ote t]t]otoot)t]bRoob)a) b)

 s[a] a a a)

 1) i+

 2]2]ke()ap[][]int MainerapyseronBufferedRepc.n대로 주구 네트19제링크}}};;; {;; {}}}은);};{};{기 ;);;{ ; ;;관계이스);;; {}};;;{;;; {;t);; {});;) {};;) ;; {;;;F; {;;;;;ader;et어진워크5