-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathL4Q2.java
99 lines (90 loc) · 1.78 KB
/
L4Q2.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
class No{
int v;
No next;
No(int v){
this.next = null;
this.v = v;
}
}
class Lista{
No head;
Lista(){
this.head = null;
}
void push (int v){
No novo = new No(v);
if (head == null){
head = novo;
} else {
No aux = head;
while (aux.next!=null){
aux = aux.next;
}
aux.next = novo;
}
}
}
public class L4Q2{
static void push(Lista[] graph, int u, int v){
graph[u].push(v);
graph[v].push(u);
}
static boolean corAdj(Lista[] graph, int[] cores, int v, int cor){
No aux = graph[v].head;
while(aux!= null){
if(cores[aux.v] == cor) return true;
aux = aux.next;
}
return false;
}
static boolean ncoloring(Lista[] graph, int[] cores, int v, int nColours, int n){
if(v == n) return true;
int w = v+1;
for(int i = 1; i<= nColours; ++i){
if(!corAdj(graph, cores, w, i)){
cores[w] = i;
if(ncoloring(graph, cores, w, nColours, n))return true;
}
}
cores[w] = 0;
return false;
}
static void print(int[] colours, Arquivo arq){
boolean notFirst = false;
for (int i=1; i<colours.length; ++i){
if (!notFirst){
arq.print(colours[i]);
notFirst = true;
} else {
arq.print(" " + colours[i]);
}
}
}
public static void main(String[] args) {
int n, m, u, v, nColours;
Lista[] graph;
int[] colours;
Arquivo arq = new Arquivo("L4Q2.in", "L4Q2.out");
while (!arq.isEndOfFile()){
n = arq.readInt();
m = arq.readInt();
graph = new Lista[n+1];
colours = new int[n+1];
for (int i=1; i<n+1; ++i){
graph[i] = new Lista();
}
for (int i=0; i<m; ++i){
u = arq.readInt();
v = arq.readInt();
push(graph, u, v);
}
colours[1]=1;
nColours = 1;
while (!ncoloring(graph, colours, 1, nColours, n)){
nColours++;
}
print(colours, arq);
arq.println();
}
}
}