Created
July 4, 2014 22:20
-
-
Save Tsutomu-KKE/dae823ed0549a296aba9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "", | |
"signature": "sha256:c630ad3b34817b7892df399acfcc5a3df82a10364b93c4e2a1bfd6994f441e53" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#UnionFind" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"class unionfind(object):\n", | |
" def __init__(self, n):\n", | |
" self.parent = range(n)\n", | |
" def find(self, i):\n", | |
" if self.parent[i] != i: self.parent[i] = self.find(self.parent[i])\n", | |
" return self.parent[i]\n", | |
" def unite(self, i, j):\n", | |
" i = self.find(i)\n", | |
" j = self.find(j)\n", | |
" if i != j: self.parent[i] = j\n", | |
" def issame(self, i, j):\n", | |
" return self.find(i) == self.find(j)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://nbviewer.ipython.org/gist/Tsutomu-KKE/dae823ed0549a296aba9