Hsu
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 問題解決力を鍛える!アルゴリズムとデータ構造 ## 第11章~ - 任意参加 - ファシリテーター: hsu ### 話す・書く順番決めルーレット in Ruby ```ruby ['hsu', 'chen', 'teruya', 'kanazawa', 'okura', 'tsujita'].shuffle.each { |v| puts ">[name=#{v}]\n\n" } ``` in JavaScript ```javascript const l = ['hsu', 'chen', 'teruya', 'kanazawa', 'okura', 'tsujita'] l[Math.floor(Math.random()*l.length)] ``` ### ファシリテーター決め方 - `%w(hsu chen teruya kanazawa okura kanno)`の順番でやっていく ### 参加者がやること - 事前に決められた章を読む - 学んだことやわからなかったことを書き込む(任意) 当日 - 最初に感想や質問を書く時間を設ける(10分) - 時間枠 60分 - 延長枠 30分 ### 勉強会時にやること 各自学びや気付き、疑問を発表していく ファシリテーターがよしなに議論を進める 終了前に勉強会を振り返る?(KPTなどで)← 60分のときに仮締め ## 各自が書き込む場所 >[name=hsu] https://www.runoob.com/data-structures/union-find-size.html 文字を読んでも理解できずJavaの例を参考しにJSに 効率は木の高さに依存するなので時間複雑度はO(h)です ```javascript= class UnionFind { constructor(n) { // parent[i] 表示第 i 個元素所指向的父節點 this.parent = new Array(n); // 數據個數 this.count = n; // 初始化, 每一個 parent[i] 指向自己, 表示每一個元素自己自成一個集合 for (let i = 0; i < n; i++) { this.parent[i] = i; } } find(p) { if (p < 0 || p >= this.count) { throw new Error("Index out of range"); } // Search self-parent node until achieving root node // root node: parent[p] === p while (p !== this.parent[p]) { p = this.parent[p]; } return p; } // 要素pとqが同じ集合に属するかどうかを判断し、それらのルートノードが同じかどうかを確認します isConnected(p, q) { return this.find(p) === this.find(q); } // 要素pとqが属する2つの集合をマージする unionElements(p, q) { const pRoot = this.find(p); const qRoot = this.find(q); if (pRoot === qRoot) { return; } this.parent[pRoot] = qRoot; } } ``` size化にすると効率が上がる ```javascript= class UnionFindWithSizeOptimization { constructor(n) { // parent[i] 表示第 i 個元素所指向的父節點 this.parent = new Array(n); // sz[i] 表示以 i 為根的集合中元素個数 this.sz = new Array(n); // 數據個數 this.count = n; // 初始化, 每一個 parent[i] 指向自己, 表示每一個元素自己自成一個集合 for (let i = 0; i < n; i++) { this.parent[i] = i; this.sz[i] = 1; } } find(p) { if (p < 0 || p >= this.count) { throw new Error("Index out of range"); } // Search self-parent node until achieving root node // root node: parent[p] === p while (p !== this.parent[p]) { p = this.parent[p]; } return p; } // 要素pとqが同じ集合に属するかどうかを判断し、それらのルートノードが同じかどうかを確認します isConnected(p, q) { return this.find(p) === this.find(q); } // 要素pとqが属する2つの集合をマージする unionElements(p, q) { const pRoot = this.find(p); const qRoot = this.find(q); if (pRoot === qRoot) { return; } // 少ない集合は多いの方にマージする if (this.sz[pRoot] < this.sz[qRoot]) { this.parent[pRoot] = qRoot; this.sz[qRoot] += this.sz[pRoot]; } else { this.parent[qRoot] = pRoot; this.sz[pRoot] += this.sz[qRoot]; } } } ``` テストしたBeforeとAfter ```javascript! function testUF(uf, m) { const startTime = new Date().getTime(); const n = uf.count; for (let i = 0; i < m; i++) { const a = Math.floor(Math.random() * n); const b = Math.floor(Math.random() * n); uf.unionElements(a, b); } for (let i = 0; i < m; i++) { const a = Math.floor(Math.random() * n); const b = Math.floor(Math.random() * n); uf.isConnected(a, b); } const endTime = new Date().getTime(); return (endTime - startTime) / 1000; } const n = 100000; const m = 100000; const uf1 = new UnionFind(n); console.log(`Without optimization: ${testUF(uf1, m)} s`); const uf2 = new UnionFindWithSizeOptimization(n); console.log(`With optimization: ${testUF(uf2, m)} s`); ``` ```bash Without optimization: 0.380 s With optimization: 0.064 s ``` > [name=tsujita] - 特段語れることがないのですが・・・・ - こちらのサイト内のスライドがわかりやすかったです https://atcoder.jp/contests/atc001/tasks/unionfind_a - まとめて→同じグループにいるか判定するプロセス の中で計算をより効率的にできる手法と理解しました > [name=teruya] https://atcoder.jp/ranking https://atcoder.jp/contests/arc032/tasks/arc032_2 ```ruby= # 上位ノードほど @rank が大きい数字となる class Node attr_accessor :parent, :rank # 親は自分自身、ランクは 0 として初期化する def initialize(n) @parent = n @rank = 0 end end class UnionFind # 0 から n までのノード(つまりn + 1個)を初期化する def initialize(n) @nodes = (0..n).map { |i| Node.new(i) } end # 再帰で最も上位の親を探して更新し、その値を返す def parent(x) return x if @nodes[x].parent == x return @nodes[x].parent = parent(@nodes[x].parent) end # a と b を仲間にする def unite(a, b) a_parent = parent(a) b_parent = parent(b) # a 及び b の親が同じ場合は何もしない return if a_parent == b_parent # a親ランク < b親ランク の場合は、b親をa親の親とする # なお、ランクをいじる必要はなし if @nodes[a_parent].rank < @nodes[b_parent].rank @nodes[a_parent].parent = b_parent # その他の場合は、a親をb親の親とする else @nodes[b_parent].parent = a_parent # a親、及びb親のランクが同じ場合は、a親のランクを +1 する @nodes[a_parent].rank += 1 if @nodes[a_parent].rank == @nodes[b_parent].rank end end # a と b の親が同じかどうかを返す def same?(a, b) parent(a) == parent(b) end # 各ノードの親を全て出力する確認用 def parents @nodes.map(&:parent) end end ``` ## 振り返り > [name=tsujita] たまーにatcoderで照屋さんの過去の解答例を見つけてます > [name=chen] 都市の例は面白い... それで説明すれば理解しやすいですねw > [name=hsu] atcoder見てみます > [name=teruya] たまにアルゴリズムで殴って問題解決してます https://github.com/Catal/Nozomi/pull/8263 など ## 終了後ファシリテーターがやること - 次回のファシリテーターを決める - chenさん - 読む範囲を決める - 第12.1~12.4章

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully