-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFleurey
More file actions
108 lines (101 loc) · 3.14 KB
/
Fleurey
File metadata and controls
108 lines (101 loc) · 3.14 KB
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
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
/**
* Created by linh on 17/09/2017.
* Kiem tra tu canh x co di duoc toi y khong va neu xoa canh x va y
* do thi co tang so thanh phan lien thong (cau) hay khong
*/
public class Fleurey {
int numberOfVertex;
int numberOfEdge;
int edge[][];
boolean isAlone[];
boolean visited[];
void input() {
Scanner scanner = new Scanner(System.in);
numberOfVertex = scanner.nextInt();
numberOfEdge = scanner.nextInt();
edge = new int[numberOfVertex + 1][numberOfVertex + 1];
isAlone = new boolean[numberOfVertex+1];
for (int i = 1; i <= numberOfEdge; i++) {
int x = scanner.nextInt();
int y = scanner.nextInt();
edge[x][y] = edge[y][x] = scanner.nextInt();
}
}
boolean checkIsAlone(int v){
boolean check = true;
for (int i = 1;i<=numberOfVertex;i++)
if (edge[v][i] != 0){
check = false;
break;
}
return check;
}
void findPath(int startVertex) {
Queue<Integer> queue = new LinkedList<>();
queue.add(startVertex);
int tempVertex = startVertex;
while (!queue.isEmpty()) {
boolean isPrint = true;
for (int i = 1; i <= numberOfVertex; i++) {
// System.out.println(edge[tempVertex][i] != 0 && !isBridge(tempVertex, i));
if (edge[tempVertex][i] != 0 && !isBridge(tempVertex, i)) {
edge[tempVertex][i]--;
edge[i][tempVertex]--;
// System.out.println(tempVertex+" "+isAlone[tempVertex]);
tempVertex = i;
isPrint = false;
queue.add(i);
break;
}
}
if (isPrint){
System.out.print(queue.remove()+" ");
}
}
}
int countLT(){
int count = 0;
visited = new boolean[numberOfVertex+1];
for (int i = 1; i <= numberOfVertex; i++)
if (!visited[i]) {
dfs(i);
count++;
}
return count;
}
private boolean isBridge(int x, int y) {
visited = new boolean[numberOfVertex + 1];
int count = 1;
edge[x][y]--;
edge[y][x]--;
isAlone[x] = checkIsAlone(x);
dfs(y);
for (int i = 1; i <= numberOfVertex; i++)
if (!isAlone[i] && !visited[i]) {
dfs(i);
count++;
}
// System.out.println(count);
edge[x][y]++;
edge[y][x]++;
return count > 1;
}
void dfs(int v) {
visited[v] = true;
for (int i = 1; i <= numberOfVertex; i++)
if (!isAlone[i] && edge[v][i] != 0 && !visited[i]) {
dfs(i);
}
}
public static void main(String[] args) {
Fleurey fleurey = new Fleurey();
fleurey.input();
/* fleurey.edge[1][2]--;
fleurey.edge[2][1]--;
System.out.println(fleurey.countLT());*/
fleurey.findPath(1);
}
}