Skip to content

Latest commit

ย 

History

History
331 lines (236 loc) ยท 10.6 KB

0725-tree.md

File metadata and controls

331 lines (236 loc) ยท 10.6 KB

2020.07.25

๐Ÿ“š๊ณต๋ถ€ํ•œ ๊ฑฐ List๐Ÿ“š

  • ํŠธ๋ฆฌ - 23. ์šฐ์„ ์ˆœ์œ„ ํ์™€ ํž™
  • ํŠธ๋ฆฌ - 25. ์ƒํ˜ธ ๋ฐฐํƒ€์  ์ง‘ํ•ฉ(disjoint set)

[์ข…๋งŒ๋ถ] ์šฐ์„ ์ˆœ์œ„ ํ์™€ ํž™

์šฐ์„ ์ˆœ์œ„๊ฐ€ ๊ฐ€์žฅ ๋†’์€ ์ž๋ฃŒ๊ฐ€ ๊ฐ€์žฅ ๋จผ์ € ๊บผ๋‚ด์ง€๋Š” ํQueue

๊ท ํ˜• ์žกํžŒ ์ด์ง„ ๊ฒ€์ƒ‰ ํŠธ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์›์†Œ๋“ค์„ ์šฐ์„ ์ˆœ์œ„๋กœ ์ •๋ ฌํ•ด๋‘๋ฉด ์ตœ๋Œ€ ์›์†Œ๋ฅผ ์ฐพ์•„ ์‚ญ์ œํ•˜๋Š” ์ผ๊ณผ ์›์†Œ ์‚ฝ์ž… ์—ฐ์‚ฐ์„ **O(NlgN)**๋™์•ˆ ํ•  ์ˆ˜ ์žˆ๋‹ค.

ํ•˜์ง€๋งŒ, ๊ท ํ˜• ์žกํžŒ ์ด์ง„ ๊ฒ€์ƒ‰ ํŠธ๋ฆฌ๋กœ ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์€ ๋„ˆ๋ฌด๋‚˜ ๋ฌด๋ฆฌ...(์ฃผ๊ฐ์ „๋„)

๊ทธ๋ž˜์„œ ๋ณด๋‹ค ๊ฐ„๋‹จํ•œ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง„ **ํž™(heap)**์„ ์‚ฌ์šฉํ•˜์—ฌ ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค.

how? ์šฐ์„ ์ˆœ์œ„์™€ ์‹ค์ œ ์ž๋ฃŒ์˜ ์Œ์„ ๋‹ด๋Š” heap์„ ๋งŒ๋“ค๊ณ , ๋Œ€์†Œ์†ฌ๊ณ„๋ฅผ ๋น„๊ตํ•  ๋•Œ๋Š” ์šฐ์„ ์ˆœ์œ„๋ฅผ ๋น„๊ตํ•˜๋ฉด ๋œ๋‹ค.

heap์˜ ๊ตฌํ˜„ ๋ฐฉ์‹

โ—พ๏ธ (์ตœ๋Œ€)ํž™์˜ ์ค‘์š”ํ•œ ํŠน์ง•

  1. ๋ถ€๋ชจ ๋…ธ๋“œ๊ฐ€ ๊ฐ€์ง„ ์›์†Œ๋Š” ํ•ญ์ƒ ์ž์‹ ๋…ธ๋“œ๊ฐ€ ๊ฐ€์ง„ ์›์†Œ ์ด์ƒ์ด๋‹ค. (=ํž™์˜ ๋Œ€์†Œ ๊ด€๊ณ„ ๊ทœ์น™)
  2. ํž™์˜ ๋Œ€์†Œ ๊ด€๊ณ„ ๊ทœ์น™์— ๋”ฐ๋ผ ํŠธ๋ฆฌ์—์„œ ๊ฐ€์žฅ ํฐ ์›์†Œ๋Š” ํ•ญ์ƒ ํŠธ๋ฆฌ์˜ ๋ฃจํŠธ์— ๋“ค์–ด๊ฐ„๋‹ค.
  3. (๋ชจ์–‘๊ทœ์น™) ๋งˆ์ง€๋ง‰ ๋ ˆ๋ฒจ์„ ์ œ์™ธํ•œ ๋ชจ๋“  ๋ ˆ๋ฒจ์— ๋…ธ๋“œ๊ฐ€ ๊ฝ‰ ์ฐจ ์žˆ๋‹ค.
  4. (๋ชจ์–‘๊ทœ์น™) ๋งˆ์ง€๋ง‰ ๋ ˆ๋ฒจ์— ๋…ธ๋“œ๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ, ํ•ญ์ƒ ๊ฐ€์žฅ ์™ผ์ชฝ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ์ฑ„์›Œ์ ธ ์žˆ๋‹ค.
  5. (ํŠน์ง• 3 + ํŠน์ง• 4 ๋งŒ์กฑ ์‹œ) ํž™์˜ ๋†’์ด๋Š” **O(lgN)**์ด๋‹ค.

*ํŠธ๋ฆฌ์˜ ๋ ˆ๋ฒจ = ๊นŠ์ด๊ฐ€ ๊ฐ™์€ ๋…ธ๋“œ๋“ค์˜ ์ง‘ํ•ฉ

|

โœ๏ธ ๋ฐฐ์—ด์„ ์ด์šฉํ•ด heap๊ตฌํ˜„ํ•˜๊ธฐ

์ผ์ฐจ์› ๋ฐฐ์—ด A[]์˜ ๊ฐ ์›์†Œ์™€ ํž™์˜ ๋…ธ๋“œ๋“ค์„ ์ผ๋Œ€์ผ ๋Œ€์‘ํ•˜๋ฉด ๋‹ค์Œ์˜ ๊ทœ์น™์„ ์–ป๋Š”๋‹ค.

  1. A[i]์— ๋Œ€์‘๋˜๋Š” ๋…ธ๋“œ์˜ ์™ผ์ชฝ ์ž์†์€ A[2*i + 1]
  2. A[i]์— ๋Œ€์‘๋˜๋Š” ๋…ธ๋“œ์˜ ์˜ค๋ฅธ์ชฝ ์ž์†์€ A[2*i + 2]
  3. A[i]์— ๋Œ€์‘๋˜๋Š” ๋…ธ๋“œ์˜ ๋ถ€๋ชจ๋Š” A[(i-1)/2]์— ๋Œ€์‘๋œ๋‹ค. (๋‚˜๋ˆ—์…ˆ ๊ฒฐ๊ณผ๋Š” ๋‚ด๋ฆผ)

โŒจ๏ธ ์ •์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” ํž™

vector<int> heap;

ํž™์˜ ์›์†Œ์˜ ๊ฐœ์ˆ˜๋Š” ๋™์  ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๊ฐ€ ๋œ๋‹ค.

|

โœ๏ธ ์›์†Œ ์‚ฝ์ž…

๋ชจ์–‘ ๊ทœ์น™์„ ๋จผ์ € ๋งŒ์กฑ์‹œํ‚จ ํ›„, ๋Œ€์†Œ ๊ด€๊ณ„ ๊ทœ์น™์„ ๋งŒ์กฑ์‹œํ‚จ๋‹ค.

์›์†Œ ์‚ฝ์ž… ๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  1. ์ƒˆ ๋…ธ๋“œ๋ฅผ ํ•ญ์ƒ heap[] ๋งˆ์ง€๋ง‰์— ์ถ”๊ฐ€ํ•œ๋‹ค.
  2. ์ƒˆ ์›์†Œ๋ฅผ ์ž์‹ ์˜ ๋ถ€๋ชจ ๋…ธ๋“œ๊ฐ€ ๊ฐ€์ง„ ์›์†Œ์™€ ๋น„๊ตํ•˜๊ณ , ๋ถ€๋ชจ ๋…ธ๋“œ๊ฐ€ ๊ฐ€์ง„ ์›์†Œ๊ฐ€ ๋” ์ž‘์œผ๋ฉด ๋‘ ์›์†Œ์˜ ์œ„์น˜๋ฅผ ๋ฐ”๊ฟ”์ค€๋‹ค.
  3. ์ƒˆ ์›์†Œ๊ฐ€ ๋” ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€ ์›์†Œ๋ฅผ ๊ฐ€์ง„ ๋ถ€๋ชจ ๋…ธ๋“œ๋ฅผ ๋งŒ๋‚˜๊ฑฐ๋‚˜, ๋ฃจํŠธ์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€ <๊ณผ์ • 2>๋ฅผ ๋ฐ˜๋ณตํ•œ๋‹ค.

โŒจ๏ธ ์ •์ˆ˜ ์›์†Œ๋ฅผ ๊ฐ–๋Š” ์ตœ๋Œ€ ํž™์— ์ƒˆ ์›์†Œ ์‚ฝ์ž…ํ•˜๊ธฐ

/*
 * ์ •์ˆ˜ ์›์†Œ๋ฅผ ๋‹ด๋Š” ์ตœ๋Œ€ ํž™ heap์— ์ƒˆ ์›์†Œ newValue๋ฅผ ์‚ฝ์ž…ํ•˜๋Š” ํ•จ์ˆ˜
 * @param heap [์ •์ˆ˜ ์›์†Œ๋ฅผ ๋‹ด๋Š” ์ตœ๋Œ€ ํž™]
 * @param newValue [์ƒˆ ์›์†Œ]
 */
void push_heap(vector<int>& heap, int newValue) {
  heap.push_back(newValue); //์ผ๋‹จ ๋ชจ์–‘ ๊ทœ์น™์„ ๋งŒ์กฑ์‹œํ‚ค๊ธฐ ์œ„ํ•ด heap ๋งจ ๋์— ์›์†Œ ์‚ฝ์ž…
  int idx = heap.size() - 1; //์ดˆ๊ธฐ ์ƒˆ ์›์†Œ์˜ ์œ„์น˜ 
  //idx>0 : ๋ฃจํŠธ์— ๋„๋‹ฌํ•˜๊ฑฐ๋‚˜
  //heap[(idx-1)/2]<heap[idx] : ๋ถ€๋ชจ๋…ธ๋“œ๊ฐ’์ด newValue๋ณด๋‹ค ์ž‘์œผ๋ฉด 
  while(idx>0 && heap[(idx-1)/2]<heap[idx]) {
    swap(heap[idx], heap[(idx-1)/2]); //๋‘ ๋…ธ๋“œ๋ฅผ ๋ฐ”๊พผ๋‹ค
    idx = (idx-1)/2; //newValue์˜ ์œ„์น˜๋ฅผ ์ˆ˜์ •
  }
}

โฑTime-Complexityโฑ

O(lgN)

|

โœ๏ธ ์ตœ๋Œ€ ์›์†Œ ๊บผ๋‚ด๊ธฐ

โ—พ๏ธ ๋ชจ์–‘ ๊ทœ์น™ ๋งŒ์กฑ ๊ณผ์ •

  1. ํž™์˜ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋ฅผ ์ง€์šด๋‹ค.
  2. ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ์— ์žˆ๋˜ ์›์†Œ๋ฅผ ๋ฃจํŠธ์— ๋ฎ์–ด์”Œ์šด๋‹ค.

โ—พ๏ธ ๋Œ€์†Œ ๊ด€๊ณ„ ๊ทœ์น™ ๋งŒ์กฑ ๊ณผ์ •

  1. ํŠธ๋ฆฌ์˜ ๋ฐ”๋‹ฅ์— ๋„๋‹ฌํ•˜๊ฑฐ๋‚˜, ๋‘ ์ž์†์ด ๋ชจ๋‘ ๋ถ€๋ชจ๋…ธ๋“œ์ดํ•˜์˜ ์›์†Œ๋ฅผ ๊ฐ–๊ณ  ์žˆ์„ ๋•Œ๊นŒ์ง€ <๊ณผ์ • 2> ๋ฐ˜๋ณต
  2. ๋‘ ์ž์‹์ด ๊ฐ€์ง€๊ณ  ์žˆ๋˜ ์›์†Œ ์ค‘ ํฐ ๋…ธ๋“œ๋ฅผ ๋ถ€๋ชจ๋…ธ๋“œ์™€ ๋ฐ”๊ฟ”์ค€๋‹ค.

โŒจ๏ธ ์ •์ˆ˜ ์›์†Œ๋ฅผ ๊ฐ–๋Š” ์ตœ๋Œ€ ํž™์—์„œ ์ตœ๋Œ€ ์›์†Œ ์‚ญ์ œํ•˜๊ธฐ

/*
 * ์ •์ˆ˜๋ฅผ ๋‹ด๋Š” ์ตœ๋Œ€ ํž™ heap์—์„œ heap[0]์„ ์ œ๊ฑฐํ•œ๋‹ค.
 * @param heap [์ •์ˆ˜๋ฅผ ๋‹ด๋Š” ์ตœ๋Œ€ ํž™]
 */
void pop_heap(vector<int>& heap) {
  heap[0] = heap.back(); //ํž™์˜ ๋ฃจํŠธ์— ๋งˆ์ง€๋ง‰ ๊ฐ’์„ ๋„ฃ๋Š”๋‹ค.
  heap.pop_back(); //๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋Š” ์‚ญ์ œ
  int here = 0; //๋งˆ์ง€๋ง‰ ๊ฐ’์˜ ํ˜„์žฌ ์œ„์น˜
  while(true) {
    int left = here*2 + 1, right = here*2 + 2;//left:์™ผ์ชฝ ์ž์‹๋…ธ๋“œ, right:์˜ค๋ฅธ์ชฝ ์ž์‹๋…ธ๋“œ
    if(left >= heap.size()) break; //๋ฆฌํ”„์— ๋„๋‹ฌํ•œ ๊ฒฝ์šฐ ์ค‘๋‹จ
    int next = here; //ํ˜„์žฌ ์œ„์น˜๋ฅผ next์— ์ €์žฅํ•ด๋†“๊ณ 
    if(heap[next] < heap[left]) //ํ˜„์žฌ๋…ธ๋“œ(๋ถ€๋ชจ)๋ณด๋‹ค ์™ผ์ชฝ์ž์‹๋…ธ๋“œ ๊ฐ’์ด ๋” ํฌ๋ฉด
      next = left; //ํ˜„์žฌ๋…ธ๋“œ์œ„์น˜๋ฅผ ์™ผ์ชฝ์ž์‹๋…ธ๋“œ ์œ„์น˜๋กœ ์„ค์ •
    if(right < heap.size() && heap[next] < heap[right]) //ํ˜„์žฌ๋…ธ๋“œ(๋ถ€๋ชจ)๋ณด๋‹ค ์˜ค๋ฅธ์ชฝ์ž์‹๋…ธ๋“œ ๊ฐ’์ด ๋” ํฌ๋ฉด
      next = right; //ํ˜„์žฌ๋…ธ๋“œ์œ„์น˜๋ฅผ ์˜ค๋ฅธ์ชฝ์ž์‹๋…ธ๋“œ ์œ„์น˜๋กœ ์„ค์ •
    if(next == here) break; //๋ถ€๋ชจ๋…ธ๋“œ์˜ ๊ฐ’์ด ์ž์‹๋…ธ๋“œ ๊ฐ’๋ณด๋‹ค ํฌ๋‹ค๋ฉด(=๋Œ€์†Œ๊ด€๊ณ„๊ทœ์น™์„ ๋งŒ์กฑํ•œ๋‹ค๋ฉด) while๋ฌธ ์ค‘๋‹จ
    swap(heap[here], heap[next]); //์•„๋‹ˆ๋ผ๋ฉด ๋…ธ๋“œ๋ฅผ ์„œ๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค
    here = next; //ํ˜„์žฌ ์œ„์น˜ ๊ฐฑ์‹ 
  }
}

โฑTime-Complexityโฑ

O(lgN)

|

๐Ÿ“์˜ˆ์ œ::๋ณ€ํ™”ํ•˜๋Š” ์ค‘๊ฐ„ ๊ฐ’

๋ฌธ์ œ : ํ…… ๋นˆ ์ˆ˜์—ด์—์„œ ์‹œ์ž‘ํ•ด์„œ ๊ฐ ์ˆ˜๊ฐ€ ์ถ”๊ฐ€๋  ๋•Œ๋งˆ๋‹ค ์ค‘๊ฐ„ ๊ฐ’์„ ๊ณ„์‚ฐํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑ.

์ˆ˜์—ด์˜ ๊ธธ์ด๊ฐ€ ์ง์ˆ˜์ผ ๋•Œ๋Š” ๊ฐ€์šด๋ฐ ์žˆ๋Š” ๋‘ ๊ฐ’ ์ค‘ ๋ณด๋‹ค ์ž‘์€ ๊ฒƒ์„ ์ˆ˜์—ด์˜ ์ค‘๊ฐ„ ๊ฐ’์ด๋ผ๊ณ  ์ •์˜ํ•œ๋‹ค.

โšก๏ธkeypointโšก๏ธ

  1. ํŠธ๋ฆฝ์„ ์ด์šฉํ•ด ํ•ด๊ฒฐํ•˜๊ธฐ

  2. ์ฃผ์–ด์ง„ ์ˆ˜์—ด์˜ ์ˆซ์ž๋“ค์„ ์ •๋ ฌํ•œ ๋’ค, ์•ž์˜ ์ ˆ๋ฐ˜์„ ์ตœ๋Œ€ํž™์—, ๋’ค์˜ ์ ˆ๋ฐ˜์„ ์ตœ์†Œํž™์— ๋„ฃ์–ด ํ‘ผ๋‹ค. ์ด ๊ฒฝ์šฐ ๋‹ค์Œ์˜ ๋ถˆ๋ณ€์‹์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

    1. ์ตœ๋Œ€ ํž™์˜ ํฌ๊ธฐ๋Š” ์ตœ์†Œ ํž™์˜ ํฌ๊ธฐ์™€ ๊ฐ™๊ฑฐ๋‚˜, ํ•˜๋‚˜ ๋” ํฌ๋‹ค.
    2. ์ตœ๋Œ€ ํž™์˜ ์ตœ๋Œ€ ์›์†Œ๋Š” ์ตœ์†Œ ํž™์˜ ์ตœ์†Œ ์›์†Œ๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค.

    ๊ทธ ๊ฒฐ๊ณผ, ์ด ์ˆ˜์—ด์˜ ์ค‘๊ฐ„ ๊ฐ’์€ ํ•ญ์ƒ ์ตœ๋Œ€ ํž™์˜ ๋ฃจํŠธ์— ์žˆ๊ฒŒ ๋œ๋‹ค.

๐Ÿ”‘**ํ•ด๋‹ต(์ฝ”๋“œ)**๐Ÿ”‘

/*
 * ๋ณ€ํ™”ํ•˜๋Š” ์ค‘๊ฐ„ ๊ฐ’ ๋ฌธ์ œ์˜ ์ž…๋ ฅ ์ƒ์„ฑํ•˜๊ธฐ
 */
struct RNG {
  int seed, a, b;
  RNG(int _a, int _b) : a(_a), b(_b), seed(1983) {}
  int next() {
    int ret = seed;
    seed = ((seed * (long long)a) + b) % 20090711;
    return ret;
  }
};
/*
 * <iostream>, <queue>, <vector>
 * ํž™์„ ์ด์šฉํ•ด ๋ณ€ํ™”ํ•˜๋Š” ์ค‘๊ฐ„ ๊ฐ’ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ํ•จ์ˆ˜์˜ ๊ตฌํ˜„
 * @param n []
 * @param rng []
 */
int runningMedian(int n, RNG rng) {
  priority_queue<int, vector<int>, less<int>> maxHeap;
  priority_queue<int, vector<int>, greater<int>> minHeap;
  int ret = 0;
  for(int cnt=1; cnt<=n; ++cnt) {
    if(maxHeap.size() == minHeap.size()) maxHeap.push(rng.next());
    else minHeap.push(rng.next());
    if(!minHeap.empty() && !maxHeap.empty() && minHeap.top()<maxHeap.top()) {
      int a = maxHeap.top();
      int b = minHeap.top();
      maxHeap.pop(); minHeap.pop();
      maxHeap.push(b); 
      minHeap.push(a);
    }
    ret = (ret + maxHeap.top()) % 20090711;
  }
  return ret;
}

โฑTime-Complexityโฑ

O(lgN)


[์ข…๋งŒ๋ถ] ์ƒํ˜ธ ๋ฐฐํƒ€์  ์ง‘ํ•ฉ(disjoint set)

์œ ๋‹ˆ์˜จ-ํŒŒ์ธํŠธ(Union-Find)

์ƒํ˜ธ ๋ฐฐํƒ€์  ์ง‘ํ•ฉ(disjoint set)

Union-Find ์ž๋ฃŒ๊ตฌ์กฐ๋ž€?

๐Ÿ‘‰ ๊ณตํ†ต ์›์†Œ๊ฐ€ ์—†๋Š”, ๋‹ค์‹œ ๋งํ•ด ์ƒํ˜ธ ๋ฐฐํƒ€์ ์ธ ๋ถ€๋ถ„ ์ง‘ํ•ฉ๋“ค๋กœ ๋‚˜๋ˆ ์ง„ ์›์†Œ๋“ค์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๊ณ  ์กฐ์ž‘ํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ

n๊ฐœ์˜ ์›์†Œ๋“ค๋กœ ์ด๋ค„์ง„ ์ง‘ํ•ฉ์„ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ, ์›์†Œ๋ฅผ 0๋ถ€ํ„ฐ n-1๊นŒ์ง€๋กœ ํ‘œํ˜„ํ•˜๊ณ  ๊ฐ 1๊ฐœ์˜ ์›์†Œ๋ฅผ ํฌํ•จํ•˜๋Š” n๊ฐœ์˜ ์ง‘ํ•ฉ์„ ๋งŒ๋“ ๋‹ค.

Union-Find์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์„ธ ๊ฐ€์ง€ ์—ฐ์‚ฐ

  1. ์ดˆ๊ธฐํ™” : n๊ฐœ์˜ ์›์†Œ๊ฐ€ ๊ฐ๊ฐ์˜ ์ง‘ํ•ฉ์— ํฌํ•จ๋˜์–ด ์žˆ๋„๋ก ์ดˆ๊ธฐํ™”
  2. union : ๋‘ ์›์†Œ a,b๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ ์ด๋“ค์ด ์†ํ•œ ๋‘ ์ง‘ํ•ฉ์„ ํ•˜๋‚˜๋กœ ํ•ฉ์นœ๋‹ค.
  3. find : ์–ด๋–ค ์›์†Œ a๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, ์ด ์›์†Œ๊ฐ€ ์†ํ•œ ์ง‘ํ•ฉ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๋ฐฐ์—ด๋กœ ์ƒํ˜ธ ๋ฐฐํƒ€์  ์ง‘ํ•ฉ ํ‘œํ˜„ํ•˜๊ธฐ

belongsTo[i] : i๋ฒˆ ์›์†Œ๊ฐ€ ์†ํ•˜๋Š” ์ง‘ํ•ฉ์˜ ๋ฒˆํ˜ธ

์ดˆ๊ธฐํ™” : belongsTo[i] = i

๐Ÿ˜ต**๋ฐฐ์—ด๋กœ ์ƒํ˜ธ ๋ฐฐํƒ€์  ์ง‘ํ•ฉ ํ‘œํ˜„ํ•˜๊ธฐ์˜ ๋ฌธ์ œ์  **

Union์—ฐ์‚ฐ์„ ํ•  ๋•Œ, belongTo์˜ ์›์†Œ์ˆœํ™˜์— O(n)์‹œ๊ฐ„์ด ์†Œ์š”๋œ๋‹ค.

ํŠธ๋ฆฌ๋ฅผ ์ด์šฉํ•œ ์ƒํ˜ธ ๋ฐฐํƒ€์  ์ง‘ํ•ฉ์˜ ํ‘œํ˜„

ํ•œ ์ง‘ํ•ฉ์— ์†ํ•˜๋Š” ์›์†Œ๋“ค์„ ํ•˜๋‚˜์˜ ํŠธ๋ฆฌ๋กœ ๋ฌถ์–ด์ฃผ์–ด, ์ƒํ˜ธ ๋ฐฐํƒ€์  ์ง‘ํ•ฉ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ํŠธ๋ฆฌ๋“ค์˜ ์ง‘ํ•ฉ์œผ๋กœ ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธ.

๋‘ ์›์†Œ๊ฐ€ ๊ฐ™์€ ์ง‘ํ•ฉ์— ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ• : ์†ํ•œ ํŠธ๋ฆฌ์˜ ๋ฃจํŠธ๊ฐ€ ๊ฐ™์€์ง€ ํ™•์ธํ•˜๋ฉด ๋œ๋‹ค.

์ „์ฒด ์ฝ”๋“œ

struct NaiveDisjointSet {
  vector<int> parent;
  NaiveDisjointSet(int n) : parent(n) { //์•„๋ž˜ ๋ณต์‚ฌ ์ƒ์„ฑ์ž ์„ค๋ช… ์ฐธ๊ณ 
    for(int i=0; i<n; ++i) parent[i] = i;
  }
  //์ฐพ๊ธฐ ์—ฐ์‚ฐ:find
  int find(int u) const {
    if(u == parent[u]) return u;
    return find(parent[u]);
  }
  //ํ•ฉ์น˜๊ธฐ ์—ฐ์‚ฐ:union
  void merge(int u, int v) {
    u.find(u);
    v.find(v);
    if(u==v) return;
    parent[u] = v;
  }
};

๐Ÿšฉ๐Ÿ”ŽC++::default copy constructor_๋””ํดํŠธ ๋ณต์‚ฌ ์ƒ์„ฑ์ž

#include <iostream>
#include <vector>
using namespace std;

class Sample {
    vector<int> num;
    int n = num.size();
    public : 
        Sample(int param) : num(param) {
            for(int i=0; i<n; ++i) num[i] = i;
        }
        void printNum() {
            for(int i=0; i<n; ++i) cout<<num[i]<<endl;
        }
};
int main()
{
    vector<int> a = {1,2,3};
    Sample first(3);
    Sample num = first;
    num.printNum();
    return 0;
}

์‹คํ–‰๊ฒฐ๊ณผ : 0 1 2

  • find ์—ฐ์‚ฐ

    • ์›์†Œ๊ฐ€ ์†ํ•œ ํŠธ๋ฆฌ์˜ ๋ฃจํŠธ๋ฅผ ์ฐพ๋Š”๋‹ค.

    • ๋ชจ๋“  ์ž์‹ ๋…ธ๋“œ๊ฐ€ ๋ถ€๋ชจ ๋…ธ๋“œ์— ๋Œ€ํ•œ ํฌ์ธํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค.

    • โฑTime-Complexityโฑ : ํ•ด๋‹น ํŠธ๋ฆฌ์˜ ๋†’์ด์— ๋น„๋ก€ํ•˜๋Š” ์‹œ๊ฐ„์ด ์†Œ์š”๋œ๋‹ค.

    • int find(int u) const {
        if(u == parent[u]) return u;
        return find(parent[u]);
      }
  • union ์—ฐ์‚ฐ

    • ๊ฐ ํŠธ๋ฆฌ์˜ ๋ฃจํŠธ๋ฅผ ์ฐพ๊ณ , ํ•˜๋‚˜๋ฅผ ๋‹ค๋ฅธ ํ•œ์ชฝ์˜ ์ž์†์œผ๋กœ ํฌํ•จ์‹œํ‚ค๋ฉด ๋œ๋‹ค.

    • โฑTime-Complexityโฑ : ํ•ด๋‹น ํŠธ๋ฆฌ์˜ ๋†’์ด์— ๋น„๋ก€ํ•˜๋Š” ์‹œ๊ฐ„์ด ์†Œ์š”๋œ๋‹ค.

    • void merge(int u, int v) {
          u.find(u);
          v.find(v);
          if(u==v) return;
          parent[u] = v;
        }
      

์ƒํ˜ธ ๋ฐฐํƒ€์  ์ง‘ํ•ฉ์˜ ์ตœ์ ํ™”

ํŠธ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ƒํ˜ธ ๋ฐฐํƒ€์  ์ง‘ํ•ฉ์„ ํ‘œํ˜„ํ•  ๊ฒฝ์šฐ ์ƒ๊ธฐ๋Š” ๋ฌธ์ œ์ 

๐Ÿ‘‰ ์—ฐ์‚ฐ์˜ ์ˆœ์„œ์— ๋”ฐ๋ผ ์ž์นซ ํŠธ๋ฆฌ๊ฐ€ ํ•œ์ชฝ์œผ๋กœ ๊ธฐ์šธ์–ด์งˆ ์ˆ˜ ์žˆ๋‹ค.

ํ•ด๊ฒฐ๋ฐฉ์•ˆ

๐Ÿ‘‰ ๋žญํฌ(rank)์— ์˜ํ•œ ํ•ฉ์น˜๊ธฐ(union-by-rank) ์ตœ์ ํ™” : ๋‘ ํŠธ๋ฆฌ๋ฅผ ํ•ฉ์น  ๋•Œ, ํ•ญ์ƒ ๋†’์ด๊ฐ€ ๋” ๋‚ฎ์€ ํŠธ๋ฆฌ๋ฅผ ๋” ๋†’์€ ํŠธ๋ฆฌ ๋ฐ‘์— ์ง‘์–ด๋„ฃ์Œ์œผ๋กœ์จ ํŠธ๋ฆฌ์˜ ๋†’์ด๊ฐ€ ๋†’์•„์ง€๋Š” ์ƒํ™ฉ์„ ๋ฐฉ์ง€ํ•œ๋‹ค.