-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grafos Explícitos.java
152 lines (140 loc) · 4.44 KB
/
Grafos Explícitos.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class NoList<T>{
public T object;
public NoList<T> next;
public NoList(T object){
this.object = object;
}
}
class ImplementedList<T> {
public NoList<T> initial;
public int quantidade = 0;
public void add(T object){
if(initial == null){
initial = new NoList(object);
}else{
NoList<T> temp = initial;
while(temp.next!=null){
temp = temp.next;
}
temp.next = new NoList(object);
}
quantidade++;
}
}
class Vertex {
public int value;
public Boolean visited;
public Vertex(int value){
this.value = value;
visited = false;
}
}
class Edges {
public Vertex origem, destino;
public Edges(Vertex origem, Vertex destino){
this.origem = origem;
this.destino = destino;
}
}
class Graph {
private ImplementedList<Vertex> vertices = new ImplementedList<>();
private ImplementedList<Edges> edges = new ImplementedList<>();
public Vertex findVertex(Integer exist){
NoList<Vertex> temp = vertices.initial;
while(temp!=null){
if(temp.object.value == exist){
break;
}
temp = temp.next;
}
if(temp!=null){
if(temp.object==null){
return null;
}else{
return temp.object;
}
}else{
return null;
}
}
public void addEdge(Integer src, Integer dst) {
Vertex srcIn;
Vertex dstOut;
if(findVertex(src)!=null){
srcIn = findVertex(src);
}else{
srcIn = new Vertex(src);
vertices.add(srcIn);
}
if(findVertex(dst)!=null){
dstOut = findVertex(dst);
}else{
dstOut = new Vertex(dst);
vertices.add(dstOut);
}
edges.add(new Edges(srcIn, dstOut));
}
public String toString() {
//podia nao precisar disso tudo, mas precisa ordenar. EU SEI QUE DA PRA FAZER MELHOR, mas eh foda neh, tava tudo bonitinho, passei a tarde toda, dai no final vc percebe que precisa ordenar, sad times
String r = "";
Vertex menor = vertices.initial.object;
NoList<Vertex> temp = vertices.initial;
for(int i=0;i<vertices.quantidade;i++){
temp = vertices.initial;
while(temp!=null){
if(!temp.object.visited){
menor = temp.object;
break;
}
temp = temp.next;
}
temp = vertices.initial;
while(temp!=null){
if(!temp.object.visited && temp.object.value<menor.value){
menor = temp.object;
}
temp = temp.next;
}
menor.visited = true;
r += String.format("(%d [%s])\n", menor.value, NodesPointingTo(menor));
}
return r;
}
public String NodesPointingTo(Vertex vertex) {
String r = "";
NoList<Edges> temp = edges.initial;
while(temp!=null){
if(temp.object.origem == vertex){
r += temp.object.destino.value + " ";
}
temp = temp.next;
}
return r.trim();
}
}
public class Solution {
public static void main(String args[]) throws Exception{
try (Scanner scanner = new Scanner(new InputStreamReader(System.in))) {
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")))) {
Graph graph = new Graph();
while (scanner.hasNext()) {
String[] nodes = scanner.nextLine().split(" ");
graph.addEdge(Integer.parseInt(nodes[0]), Integer.parseInt(nodes[1]));
}
bufferedWriter.write(graph.toString());
bufferedWriter.newLine();
}
}
}
}