Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7bc60cb

Browse files
estudos
1 parent 942f04d commit 7bc60cb

File tree

14 files changed

+228
-198
lines changed

14 files changed

+228
-198
lines changed

‎UnionFind/.idea/workspace.xml

Lines changed: 104 additions & 167 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
561 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
435 Bytes
Binary file not shown.
1.08 KB
Binary file not shown.
425 Bytes
Binary file not shown.
384 Bytes
Binary file not shown.

‎UnionFind/raw/links

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,9 @@ http://codeforces.com/blog/entry/45645
3030
http://codeforces.com/blog/entry/23262
3131

3232
Problems
33-
https://www.hackerearth.com/challenge/competitive/code-monk-disjoint-set-union/problems/
33+
https://www.hackerearth.com/challenge/competitive/code-monk-disjoint-set-union/problems/
34+
35+
36+
Kruskal
37+
https://www.geeksforgeeks.org/greedy-algorithms-set-2-kruskals-minimum-spanning-tree-mst/
38+
https://www.cs.cmu.edu/~ckingsf/class/02713-s13/lectures/lec03-othermst.pdf

‎UnionFind/src/problems/codeforces/RestructuringCompany.java

Lines changed: 68 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,26 @@
1010
/**
1111
* http://codeforces.com/contest/566/problem/D
1212
* http://codeforces.com/contest/566
13+
*
14+
*
15+
* Tutorial
16+
* http://codeforces.com/blog/entry/19518
17+
* Resolver os problemas desse tutorial futuramente
18+
*
19+
* Solucao interessante
20+
* https://github.com/yancouto/maratona-sua-mae/blob/master/Yan/codeforces/566D.cpp
21+
*
1322
* */
1423
public class RestructuringCompany {
1524

25+
private static final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
26+
private static final PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(System.out), true);
1627

17-
public static int [] teams;
18-
public static int [] sz;
19-
public static int [] next;
28+
private static int [] teams;
29+
private static int [] sz;
30+
private static int [] next;
2031

21-
public static void init(int size) {
32+
private static void init(int size) {
2233
teams = new int[size+1];
2334
sz = new int[size+1];
2435
next = new int[size+1];
@@ -29,13 +40,11 @@ public static void init(int size) {
2940
}
3041
}
3142

32-
public static void union(int p, int q) {
33-
int rootP = find(p);
34-
int rootQ = find(q);
43+
private static void unionByRank(int p, int q) {
44+
int rootP = findWithPathCompression(p);
45+
int rootQ = findWithPathCompression(q);
3546
if(rootP == rootQ)
3647
return;;
37-
teams[rootP] = rootQ;
38-
/*
3948
// weight compare
4049
if(sz[rootP] < sz[rootQ]) {
4150
teams[rootP] = rootQ;
@@ -45,24 +54,58 @@ public static void union(int p, int q) {
4554
teams[rootQ] = rootP;
4655
sz[rootP] += sz[rootQ];
4756
}
48-
*/
4957
}
5058

51-
public static boolean hasSameRoot(int p, int q) {
59+
private static void union(int p, int q) {
60+
int rootP = find(p);
61+
int rootQ = find(q);
62+
if(rootP == rootQ)
63+
return;;
64+
teams[rootP] = rootQ;
65+
}
66+
67+
private static boolean hasSameRoot(int p, int q) {
5268
return find(p) == find(q);
5369
}
5470

55-
public static int find(int p) {
71+
private static int findWithPathCompression(int p) {
72+
if(p == teams[p])
73+
return p;
74+
teams[p] = teams[teams[p]];
75+
return findWithPathCompression(teams[p]);
76+
}
77+
78+
private static int find(int p) {
5679
if(p == teams[p])
5780
return p;
58-
teams[p] = teams[teams[p]]; // path compression
5981
return find(teams[p]);
6082
}
6183

84+
// TLE
85+
private static void solver1(int type, int x, int y) {
86+
if(type == 1) {
87+
union(x, y);
88+
}
89+
else if(type == 2) {
90+
for (int j = x+1; j <= y;) {
91+
union(j-1, j);
92+
int tempRoot = next[j];
93+
next[j] = next[y];
94+
j = tempRoot;
95+
}
96+
}
97+
else {
98+
printWriter.printf("%s\n", hasSameRoot(x, y) ? "YES" : "NO");
99+
}
100+
}
101+
102+
private static void solver2(int type, int x, int y) {
103+
104+
}
105+
62106

63107
public static void main(String[] args) {
64-
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
65-
PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(System.out), true);
108+
66109
try {
67110
StringTokenizer tk = new StringTokenizer(bufferedReader.readLine(), " ");
68111
int n = Integer.parseInt(tk.nextToken());
@@ -73,21 +116,16 @@ public static void main(String[] args) {
73116
int type = Integer.parseInt(tk.nextToken());
74117
int x = Integer.parseInt(tk.nextToken());
75118
int y = Integer.parseInt(tk.nextToken());
76-
if(type == 1) {
77-
union(x, y);
78-
}
79-
else if(type == 2) {
80-
int i=x+1;
81-
while(i<=y) {
82-
union(i, x);
83-
int tempRoot = next[i];
84-
next[i] = next[y];
85-
i = tempRoot;
86-
}
87-
}
88-
else {
89-
printWriter.printf("%s\n", hasSameRoot(x, y) ? "YES" : "NO");
90-
}
119+
120+
/**
121+
* type == tipo de operacao na estrutura union-findWithPathCompression
122+
* Tipo 1 e 2 sao unioes, tipo 3 verifica se o NÓ x possui o mesmo NÓ-PAI de y
123+
*
124+
* Union tipo 1 - muda o no pai de teams[y] para o no pai de teams[x
125+
* Union tipo 2 - muda o no pai dos teams[x+1, x+2, x+3 ... y] para o no pai de teams[x]
126+
*
127+
* */
128+
solver1(type, x, y);
91129
}
92130
} catch (Exception e) {}
93131
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /