Created
March 1, 2016 18:32
-
-
Save aolo2/6a4d2563293061590be5 to your computer and use it in GitHub Desktop.
test
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
Disko |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="CompilerConfiguration"> | |
<resourceExtensions /> | |
<wildcardResourcePatterns> | |
<entry name="!?*.java" /> | |
<entry name="!?*.form" /> | |
<entry name="!?*.class" /> | |
<entry name="!?*.groovy" /> | |
<entry name="!?*.scala" /> | |
<entry name="!?*.flex" /> | |
<entry name="!?*.kt" /> | |
<entry name="!?*.clj" /> | |
<entry name="!?*.aj" /> | |
</wildcardResourcePatterns> | |
<annotationProcessing> | |
<profile default="true" name="Default" enabled="false"> | |
<processorPath useClasspath="true" /> | |
</profile> | |
</annotationProcessing> | |
</component> | |
</project> |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="Encoding"> | |
<file url="PROJECT" charset="UTF-8" /> | |
</component> | |
</project> |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="EntryPointsManager"> | |
<entry_points version="2.0" /> | |
</component> | |
<component name="MavenImportPreferences"> | |
<option name="generalSettings"> | |
<MavenGeneralSettings> | |
<option name="mavenHome" value="Bundled (Maven 3)" /> | |
</MavenGeneralSettings> | |
</option> | |
</component> | |
<component name="ProjectLevelVcsManager" settingsEditedManually="false"> | |
<OptionsSetting value="true" id="Add" /> | |
<OptionsSetting value="true" id="Remove" /> | |
<OptionsSetting value="true" id="Checkout" /> | |
<OptionsSetting value="true" id="Update" /> | |
<OptionsSetting value="true" id="Status" /> | |
<OptionsSetting value="true" id="Edit" /> | |
<ConfirmationsSetting value="0" id="Add" /> | |
<ConfirmationsSetting value="0" id="Remove" /> | |
</component> | |
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="false" assert-keyword="true" jdk-15="true" project-jdk-name="1.8.0_74" project-jdk-type="JavaSDK"> | |
<output url="file://$PROJECT_DIR$/out" /> | |
</component> | |
</project> |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="ProjectModuleManager"> | |
<modules> | |
<module fileurl="file://$PROJECT_DIR$/Disko.iml" filepath="$PROJECT_DIR$/Disko.iml" /> | |
</modules> | |
</component> | |
</project> |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="Palette2"> | |
<group name="Swing"> | |
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false"> | |
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" /> | |
</item> | |
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false"> | |
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" /> | |
</item> | |
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false"> | |
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" /> | |
</item> | |
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true"> | |
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" /> | |
</item> | |
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" /> | |
<initial-values> | |
<property name="text" value="Button" /> | |
</initial-values> | |
</item> | |
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" /> | |
<initial-values> | |
<property name="text" value="RadioButton" /> | |
</initial-values> | |
</item> | |
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" /> | |
<initial-values> | |
<property name="text" value="CheckBox" /> | |
</initial-values> | |
</item> | |
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false"> | |
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" /> | |
<initial-values> | |
<property name="text" value="Label" /> | |
</initial-values> | |
</item> | |
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true"> | |
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> | |
<preferred-size width="150" height="-1" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true"> | |
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> | |
<preferred-size width="150" height="-1" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true"> | |
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> | |
<preferred-size width="150" height="-1" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true"> | |
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> | |
<preferred-size width="150" height="50" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true"> | |
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> | |
<preferred-size width="150" height="50" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true"> | |
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> | |
<preferred-size width="150" height="50" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true"> | |
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" /> | |
</item> | |
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> | |
<preferred-size width="150" height="50" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3"> | |
<preferred-size width="150" height="50" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> | |
<preferred-size width="150" height="50" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3"> | |
<preferred-size width="200" height="200" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false"> | |
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3"> | |
<preferred-size width="200" height="200" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true"> | |
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" /> | |
</item> | |
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" /> | |
</item> | |
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false"> | |
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" /> | |
</item> | |
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" /> | |
</item> | |
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false"> | |
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1"> | |
<preferred-size width="-1" height="20" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false"> | |
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" /> | |
</item> | |
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" /> | |
</item> | |
</group> | |
</component> | |
</project> |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="VcsDirectoryMappings"> | |
<mapping directory="" vcs="" /> | |
</component> | |
</project> |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<module type="JAVA_MODULE" version="4"> | |
<component name="NewModuleRootManager" inherit-compiler-output="true"> | |
<exclude-output /> | |
<content url="file://$MODULE_DIR$"> | |
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | |
</content> | |
<orderEntry type="inheritedJdk" /> | |
<orderEntry type="sourceFolder" forTests="false" /> | |
</component> | |
</module> |
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
package BridgeNum; | |
import java.util.ArrayList; | |
import java.util.Scanner; | |
class Vertex { | |
boolean visited; | |
ArrayList<Integer> list; | |
public Vertex() { | |
list = new ArrayList<>(); | |
visited = false; | |
} | |
public String toString() { | |
return visited + ", " + list; | |
} | |
} | |
class ListGraph { | |
private int vertexNum, bridges; | |
private Vertex[] data; | |
private boolean[] used; | |
private int timer, tin[], fup[]; | |
private int min(int a, int b) { | |
return (a <= b) ? a : b; | |
} | |
public ListGraph(int n) { | |
this.vertexNum = n; | |
data = new Vertex[n]; | |
for (int i = 0; i < n; i++) | |
data[i] = new Vertex(); | |
tin = new int[n]; | |
fup = new int[n]; | |
used = new boolean[n]; | |
} | |
public void addEdge(int start, int end) { | |
data[start].list.add(end); | |
data[end].list.add(start); | |
} | |
private int dfs(int v, int p) { | |
used[v] = true; | |
tin[v] = fup[v] = timer++; | |
for (int i = 0; i < data[v].list.size(); i++) { | |
int to = data[v].list.get(i); | |
if (to == p) | |
continue; | |
if (used[to]) | |
fup[v] = min(fup[v], tin[to]); | |
else { | |
dfs(to, v); | |
fup[v] = min(fup[v], fup[to]); | |
if (fup[to] > tin[v]) | |
bridges++; | |
} | |
} | |
return bridges; | |
} | |
public int find_bridges() { | |
timer = 0; | |
for (int i = 0; i < vertexNum; i++) | |
if (!used[i]) | |
dfs(i, -1); | |
return bridges; | |
} | |
} | |
public class BridgeNum { | |
public static void main(String[] args) { | |
Scanner in = new Scanner(System.in); | |
int a, b, vertexN, edgeN; | |
vertexN = in.nextInt(); | |
edgeN = in.nextInt(); | |
ListGraph G = new ListGraph(vertexN); | |
for (int i = 0; i < edgeN; i++) { | |
a = in.nextInt(); | |
b = in.nextInt(); | |
G.addEdge(a, b); | |
} | |
System.out.println(G.find_bridges()); | |
} | |
} |
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
package CompSet; | |
import java.util.ArrayList; | |
import java.util.Scanner; | |
class Vertex { | |
boolean visited; | |
ArrayList<Integer> list; | |
public Vertex() { | |
list = new ArrayList<>(); | |
visited = false; | |
} | |
public String toString() { | |
return visited + ", " + list; | |
} | |
} | |
class ListGraph { | |
private int vertexNum; | |
private Vertex[] data; | |
public ListGraph(int n) { | |
this.vertexNum = n; | |
data = new Vertex[n]; | |
for (int i = 0; i < n; i++) | |
data[i] = new Vertex(); | |
} | |
public int getVertexNum() { | |
return vertexNum; | |
} | |
public void addEdge(int start, int end) { | |
data[start].list.add(end); | |
data[end].list.add(start); | |
} | |
public boolean dfs(int start) { | |
boolean stop = true; | |
if (!data[start].visited) { | |
data[start].visited = true; | |
stop = false; | |
} | |
for (int i : data[start].list) { | |
if (!data[i].visited) { | |
data[i].visited = true; | |
stop = false; | |
dfs(i); | |
} | |
} | |
return stop; | |
} | |
public void print() { | |
for (Vertex v : this.data) | |
System.out.println(v); | |
} | |
} | |
public class CompSet { | |
public static void main(String[] args) { | |
Scanner in = new Scanner(System.in); | |
int a, b, vertexN = in.nextInt(), edgeN = in.nextInt(), compset = 0; | |
ListGraph G = new ListGraph(vertexN); | |
for (int i = 0; i < edgeN; i++) { | |
a = in.nextInt(); | |
b = in.nextInt(); | |
G.addEdge(a, b); | |
} | |
for (int i = 0; i < G.getVertexNum(); i++) { | |
if (!G.dfs(i)) | |
compset++; | |
} | |
System.out.println(compset); | |
} | |
} |
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
package Dividers; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.Scanner; | |
public class Dividers { | |
static void findDividers(ArrayList<Long> divs, long n, long d, long g) { | |
divs.add(d); | |
if (d != g) { | |
d++; | |
while (d * d <= n && n % d != 0) | |
d++; | |
if (d * d <= n) | |
findDividers(divs, n, d, n / d); | |
divs.add(g); | |
} | |
} | |
static boolean isPrime(long n) { | |
for (int i = 2; i * i <= n; i++) | |
if (n % i == 0) | |
return false; | |
return true; | |
} | |
public static void main(String[] args) { | |
Scanner in = new Scanner(System.in); | |
long num = in.nextLong(); | |
ArrayList<Long> divs = new ArrayList<>(); | |
findDividers(divs, num, 1, num); | |
Collections.reverse(divs); | |
System.out.println("graph {"); | |
for (long div : divs) { | |
System.out.println("\t" + div); | |
} | |
for (long n : divs) { | |
for (long x : divs) { | |
if ((n != x) && (n % x == 0) && isPrime(n / x)) | |
System.out.println("\t" + n + " -- " + x); | |
} | |
} | |
System.out.println("}"); | |
} | |
} |
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
package EqDist; | |
import java.util.ArrayDeque; | |
import java.util.ArrayList; | |
import java.util.Scanner; | |
class Vertex { | |
boolean visited; | |
ArrayList<Integer> list; | |
public Vertex() { | |
list = new ArrayList<>(); | |
visited = false; | |
} | |
@Override | |
public String toString() { | |
return visited + ", " + list; | |
} | |
} | |
class ListGraph { | |
private int vertexNum; | |
private Vertex[] data; | |
public ListGraph(int n) { | |
this.vertexNum = n; | |
data = new Vertex[n]; | |
for (int i = 0; i < n; i++) | |
data[i] = new Vertex(); | |
} | |
public void addEdge(int start, int end) { | |
data[start].list.add(end); | |
data[end].list.add(start); | |
} | |
public void bfs(ArrayList<Integer> bearingList) { | |
ArrayList<int[]> lengths = new ArrayList<>(bearingList.size()); | |
for (int start : bearingList) { | |
ArrayDeque<Integer> q1, q2; | |
q1 = new ArrayDeque<>(); | |
q2 = new ArrayDeque<>(); | |
ArrayList<Integer> p1, p2; | |
p1 = new ArrayList<>(); | |
p2 = new ArrayList<>(); | |
q1.add(start); | |
q2.add(start); | |
while (!q1.isEmpty()) { | |
int k1 = q1.remove(), k2 = q2.remove(); | |
if (!data[k1].visited) { | |
p1.add(k1); | |
p2.add(k2); | |
data[k1].visited = true; | |
for (int n : data[k1].list) | |
if (!data[n].visited) { | |
q1.add(n); | |
q2.add(k1); | |
} | |
} | |
} | |
int[] len = new int[vertexNum]; | |
for (int i = 0; i < vertexNum; i++) { | |
int lvl = -1, from, index; | |
if (data[i].visited) { | |
lvl = 0; | |
for (from = i; from != start; ) { | |
index = p1.indexOf(from); | |
if (index != -1) { | |
from = p2.get(index); | |
lvl++; | |
} | |
} | |
} | |
len[i] = lvl; | |
} | |
lengths.add(len); | |
reset(); | |
} | |
int count = 0; | |
for (int i = 0; i < vertexNum; i++) { | |
boolean same = true; | |
for (int k = 1; k < lengths.size(); k++) | |
if (lengths.get(k)[i] != lengths.get(k - 1)[i] || | |
lengths.get(k)[i] == -1 || lengths.get(k - 1)[i] == -1) | |
same = false; | |
if (same) { | |
System.out.print(i + " "); | |
count++; | |
} | |
} | |
if (count == 0) | |
System.out.println("-"); | |
} | |
public void reset() { | |
for (Vertex v : data) | |
v.visited = false; | |
} | |
} | |
public class EqDist { | |
public static void main(String[] args) { | |
Scanner in = new Scanner(System.in); | |
int a, b, vertexN, edgeN, bearingN; | |
vertexN = in.nextInt(); | |
edgeN = in.nextInt(); | |
ListGraph G = new ListGraph(vertexN); | |
for (int i = 0; i < edgeN; i++) { | |
a = in.nextInt(); | |
b = in.nextInt(); | |
G.addEdge(a, b); | |
} | |
bearingN = in.nextInt(); | |
ArrayList<Integer> bearingList = new ArrayList<>(); | |
for (int i = 0; i < bearingN; i++) | |
bearingList.add(in.nextInt()); | |
G.bfs(bearingList); | |
} | |
} |
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
package FormulaOrder; | |
public class FormulaOrder { | |
} |
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
package GraphBase; | |
import java.util.ArrayDeque; | |
import java.util.ArrayList; | |
import java.util.Scanner; | |
class MatrixGraph { | |
private int[][] data; | |
private boolean[] visited; | |
public MatrixGraph(int n) { | |
data = new int[n][n]; | |
visited = new boolean[n]; | |
} | |
public void addEdge(int start, int end) { | |
data[start][end] = 1; | |
data[end][start] = -1; | |
} | |
public void reverse() { | |
for (int i = 0; i < data.length; i++) { | |
for (int j = 0; j < data[i].length; j++) { | |
data[i][j] *= -1; | |
} | |
} | |
} | |
public void reset() { | |
for (int i = 0; i < visited.length; i++) { | |
visited[i] = false; | |
} | |
} | |
public String toString() { | |
String tmp = ""; | |
for (int[] col : data) { | |
for (int n : col) { | |
tmp += String.format("%3d", n); | |
} | |
tmp += '\n'; | |
} | |
return tmp; | |
} | |
public boolean isVisited(int v) { | |
return visited[v]; | |
} | |
public boolean allVisited() { | |
for (boolean b : visited) { | |
if (!b) { | |
return false; | |
} | |
} | |
return true; | |
} | |
public ArrayList<Integer> bfs(int start) { | |
ArrayDeque<Integer> q = new ArrayDeque<>(); | |
ArrayList<Integer> path = new ArrayList<>(); | |
q.add(start); | |
while (!q.isEmpty()) { | |
int tmp = q.remove(); | |
if (!visited[tmp]) { | |
path.add(tmp); | |
visited[tmp] = true; | |
for (int i = 0; i < data[tmp].length; i++) { | |
int near = data[tmp][i]; | |
if (near == 1) { | |
q.add(i); | |
} | |
} | |
} | |
} | |
return path; | |
} | |
} | |
public class GraphBase { | |
public static void main(String[] args) { | |
Scanner in = new Scanner(System.in); | |
int N = in.nextInt(), M = in.nextInt(); | |
MatrixGraph G = new MatrixGraph(N); | |
for (int i = 0; i < M; i++) { | |
G.addEdge(in.nextInt(), in.nextInt()); | |
} | |
// System.out.println(G); | |
ArrayList<ArrayList<Integer>> strongComp = new ArrayList<>(); | |
for (int start = 0; start < N; start++) { | |
ArrayList<Integer> union = G.bfs(start); | |
G.reverse(); | |
G.reset(); | |
ArrayList<Integer> tmp = G.bfs(start); | |
union.retainAll(tmp); | |
strongComp.add(union); | |
G.reset(); | |
} | |
for (ArrayList<Integer> a : strongComp) { | |
System.out.println(a); | |
} | |
} | |
} |
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
package Kruskal; | |
import java.util.ArrayList; | |
import java.util.Scanner; | |
class Vertex { | |
Vertex parent = this; | |
int x, y, depth; | |
public Vertex(int x, int y) { | |
this.x = x; | |
this.y = y; | |
} | |
public Vertex Find() { | |
if (this != this.parent) { | |
this.parent = this.parent.Find(); | |
} | |
return this.parent; | |
} | |
public void Union(Vertex v) { | |
Vertex a = this.Find(), b = v.Find(); | |
if (a.depth < b.depth) { | |
a.parent = b; | |
} else { | |
b.parent = a; | |
if (a.parent == b.parent && a != b) { | |
a.depth++; | |
} | |
} | |
} | |
} | |
class Edge { | |
float weight; | |
Vertex start, end; | |
public Edge(Vertex start, Vertex end) { | |
this.start = start; | |
this.end = end; | |
weight = (float) (Math.sqrt((end.x - start.x) * (end.x - start.x) + (end.y - start.y) * (end.y - start.y))); | |
} | |
} | |
public class Kruskal { | |
public static void main(String[] args) { | |
Scanner in = new Scanner(System.in); | |
int pNum = in.nextInt(); | |
double min_length = 0; | |
ArrayList<Vertex> points = new ArrayList<>(pNum); | |
ArrayList<Edge> ws = new ArrayList<>((pNum * (pNum - 1)) / 2); | |
for (int i = 0; i < pNum; i++) { | |
points.add(new Vertex(in.nextInt(), in.nextInt())); | |
} | |
for (int i = 0; i < pNum; i++) { | |
for (int j = i + 1; j < pNum; j++) { | |
ws.add(new Edge(points.get(i), points.get(j))); | |
} | |
} | |
for (Edge e : ws) { | |
if (e.start.Find() != e.end.Find()) { | |
min_length += e.weight; | |
e.start.Union(e.end); | |
} | |
} | |
System.out.format("%.2f\n", min_length); | |
} | |
} |
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
package Mars; | |
// I'm so bad | |
import java.util.ArrayDeque; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.Scanner; | |
class Vertex { | |
boolean visited; | |
int val; | |
ArrayList<Integer> list; | |
public Vertex(int val) { | |
list = new ArrayList<>(); | |
this.val = val; | |
visited = false; | |
} | |
} | |
class Node { | |
int here, from; | |
public Node(int here, int from) { | |
this.here = here; | |
this.from = from; | |
} | |
} | |
class ListGraph { | |
private Vertex[] data; | |
public ListGraph(int n) { | |
data = new Vertex[n]; | |
for (int i = 0; i < n; i++) | |
data[i] = new Vertex(i); | |
} | |
public boolean isEdge(int to, int from) { | |
return (data[from].list.indexOf(to) != -1); | |
} | |
public void addEdge(int start, int end) { | |
if (data[start].list.indexOf(end) == -1) { | |
data[start].list.add(end); | |
} | |
if (data[end].list.indexOf(start) == -1) { | |
data[end].list.add(start); | |
} | |
} | |
public ArrayList<Node> bfs(int start) { | |
ArrayDeque<Node> q = new ArrayDeque<>(); | |
ArrayList<Node> path = new ArrayList<>(); | |
q.add(new Node(start, start)); | |
while (!q.isEmpty()) { | |
Node k = q.remove(); | |
if (!data[k.here].visited) { | |
path.add(k); | |
data[k.here].visited = true; | |
for (int n : data[k.here].list) { | |
if (!data[n].visited) { | |
q.add(new Node(n, k.here)); | |
} | |
} | |
} | |
} | |
return path; | |
} | |
boolean allVisited() { | |
for (Vertex v : data) { | |
if (!v.visited) { | |
return false; | |
} | |
} | |
return true; | |
} | |
} | |
class Rule { | |
ArrayList<Integer> team1, team2; | |
public Rule(ArrayList<Node> src) { | |
team1 = new ArrayList<>(); | |
team2 = new ArrayList<>(); | |
int len, start; | |
team1.add(start = src.get(0).here); | |
for (int i = 1; i < src.size(); i++) { | |
len = 0; | |
int tmp = src.get(i).from; | |
while (tmp != start) { | |
for (int j = i - 1; j > 0; j--) { | |
if (src.get(j).here == tmp) { | |
tmp = src.get(j).from; | |
len++; | |
break; | |
} | |
} | |
} | |
if (len % 2 != 0) { | |
team1.add(src.get(i).here); | |
} else { | |
team2.add(src.get(i).here); | |
} | |
} | |
} | |
public void check(ListGraph G) { | |
int k = 1; | |
for (ArrayList<Integer> t = team1; k < 3; k++, t = team2) { | |
for (int i = 0; i < t.size(); i++) { | |
for (int j = 0; j < t.size(); j++) { | |
if (G.isEdge(t.get(i), t.get(j))) { | |
System.out.println("No solution"); | |
System.exit(1); | |
} | |
} | |
} | |
} | |
} | |
} | |
public class Mars { | |
static boolean Greater(ArrayList<Integer> a, ArrayList<Integer> b) { | |
for (int i = 0; i < a.size(); i++) { //Using a.size() cause a.size() alsways = | |
if (a.get(i) < b.get(i)) { | |
return false; | |
} else if (a.get(i) > b.get(i)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
public static void main(String[] args) { | |
Scanner in = new Scanner(System.in); | |
int N = in.nextInt(); | |
char[][] matrix = new char[N][N]; | |
ListGraph plus = new ListGraph(N); | |
for (int i = 0; i < N; i++) { | |
for (int j = 0; j < N; j++) { | |
char c = in.next().toCharArray()[0]; | |
matrix[i][j] = c; | |
if (c == '+') { | |
plus.addEdge(i, j); | |
} | |
} | |
} | |
ArrayList<Rule> rules = new ArrayList<>(); | |
ArrayList<Integer> neutral = new ArrayList<>(); | |
int start = 0; | |
while (!plus.allVisited()) { | |
ArrayList<Node> path = plus.bfs(start++); | |
if (path.size() == 1) { | |
neutral.add(path.get(0).here); | |
} else if (path.size() > 1) { | |
rules.add(new Rule(path)); | |
} | |
} | |
ArrayList<ArrayList<Integer>> branch; | |
ArrayList<Integer> vars = new ArrayList<>(); | |
for (int i = 0; i < rules.size(); i++) { | |
vars.add(i); | |
} | |
ArrayList<Integer> fixed; | |
fixed = new ArrayList<>(); | |
int comb = (int) Math.pow(2, rules.size()); | |
for (int i = 0; i < rules.size(); i++) { | |
Rule r = rules.get(i); | |
r.check(plus); | |
if (r.team1.size() <= r.team2.size()) { | |
comb /= 2; | |
fixed.add(i); | |
vars.remove(new Integer(i)); | |
} | |
} | |
branch = new ArrayList<>(); | |
int maxLen = rules.size() - fixed.size(); | |
ArrayList<Integer> finalExp = new ArrayList<>(); | |
if (maxLen == 0) { | |
for (Rule r : rules) { | |
finalExp.addAll(r.team1); | |
} | |
int size = finalExp.size(); | |
for (int i = 0; i < (N / 2) - size; i++) { | |
finalExp.add(neutral.get(i)); | |
} | |
Collections.sort(finalExp); | |
} else { | |
for (int i = 0; i < comb; i++) { | |
ArrayList<Integer> temp = new ArrayList<>(); | |
String tmp = Integer.toBinaryString(i); | |
while (tmp.length() < maxLen) { | |
tmp = "0" + tmp; | |
} | |
char[] combArr = tmp.toCharArray(); | |
for (int j = 0; j < combArr.length; j++) { | |
char c = combArr[j]; | |
if (c == '0') { | |
temp.addAll(rules.get(vars.get(j)).team1); | |
} else { | |
temp.addAll(rules.get(vars.get(j)).team2); | |
} | |
} | |
for (int f = 0; f < rules.size(); f++) { | |
if (fixed.indexOf(f) != -1) { | |
temp.addAll(rules.get(f).team1); | |
} | |
} | |
int rem = temp.size(); | |
for (int k = 0; k < (N / 2) - rem; k++) { | |
temp.add(neutral.get(k)); | |
} | |
Collections.sort(temp); | |
branch.add(temp); | |
} | |
/* HERE COMES THE BUBBLE SORT!!! */ | |
boolean flag = true; | |
ArrayList<Integer> temp; | |
while (flag) { | |
flag = false; | |
for (int j = 0; j < branch.size() - 1; j++) { | |
if (Greater(branch.get(j), branch.get(j + 1))) { | |
temp = branch.get(j); | |
branch.set(j, branch.get(j + 1)); | |
branch.set(j + 1, temp); | |
flag = true; | |
} | |
} | |
} | |
/* It's over, you can look now */ | |
finalExp = branch.get(0); | |
} | |
for (int n : finalExp) { | |
System.out.print((n + 1) + " "); | |
} | |
} | |
} |
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
package Prim; | |
import java.util.PriorityQueue; | |
import java.util.Random; | |
import java.util.Scanner; | |
class Vertex implements Comparable<Vertex> { | |
PriorityQueue<Edge> list; | |
public int key, index; | |
public Vertex() { | |
list = new PriorityQueue<>(); | |
this.key = -1; | |
this.index = -1; | |
} | |
public String toString() { | |
return list.toString(); | |
} | |
public int compareTo(Vertex v) { | |
return (this.key - v.key); | |
} | |
} | |
class Edge implements Comparable<Edge> { | |
public int end, len; | |
public Edge(int end, int len) { | |
this.end = end; | |
this.len = len; | |
} | |
public int compareTo(Edge e) { | |
return (this.len - e.len); | |
} | |
public String toString() { | |
return "->" + String.valueOf(end) + "(" + String.valueOf(len) + ")"; | |
} | |
} | |
class ListGraph { | |
Vertex[] data; | |
public ListGraph(int n) { | |
data = new Vertex[n]; | |
for (int i = 0; i < n; i++) | |
data[i] = new Vertex(); | |
} | |
public void addEdge(int start, int end, int len) { | |
data[start].list.add(new Edge(end, len)); | |
data[end].list.add(new Edge(start, len)); | |
} | |
public void print() { | |
for (Vertex v : this.data) { | |
PriorityQueue<Edge> clone = new PriorityQueue<>(v.list); | |
while (!clone.isEmpty()) { | |
System.out.print(clone.poll() + " "); | |
} | |
System.out.println(); | |
} | |
} | |
} | |
public class Prim { | |
public static void main(String[] args) { | |
Scanner in = new Scanner(System.in); | |
int vNum = in.nextInt(), eNum = in.nextInt(), res = 0; | |
ListGraph G = new ListGraph(vNum); | |
for (int i = 0; i < eNum; i++) { | |
G.addEdge(in.nextInt(), in.nextInt(), in.nextInt()); | |
} | |
//G.print(); | |
PriorityQueue<Vertex> q = new PriorityQueue<>(); | |
Random rand = new Random(); | |
Vertex v = G.data[rand.nextInt(vNum)]; | |
while (true) { | |
v.index = -2; | |
for (Edge e : v.list) { | |
Vertex u = G.data[e.end]; | |
if (u.index == -1) { | |
u.key = e.len; | |
u.index = 1; | |
q.add(u); | |
} else if (u.index != -2 && e.len < u.key) { | |
q.remove(u); | |
u.key = e.len; | |
q.add(u); | |
} | |
} | |
if (q.isEmpty()) { | |
break; | |
} | |
v = q.poll(); | |
res += v.key; | |
} | |
System.out.println(res); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment