-
Notifications
You must be signed in to change notification settings - Fork 1
/
Graph.cs
130 lines (120 loc) · 2.73 KB
/
Graph.cs
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.IO;
namespace Graph{
class graph{
private int simpul;
private List<Int32>[] sisi;
private Int32[] j1;
private Stack<Int32> jalur;
public graph(int v){
simpul = v;
sisi = new List<Int32>[v+1];
for(int i=0;i<=v;i++){
sisi[i] = new List<Int32>();
}
}
public void addEdge(int v,int s){
sisi[v].Add(s);
sisi[s].Add(v);
}
public void kasus0(int x,int y){
DFS(y,1);
if(j1.Contains(x)){
Console.WriteLine("YA");
} else{
Console.WriteLine("TIDAK");
}
}
public void kasus1(int x,int y){
DFS(y,1);
int[] jalur1 = new Int32[j1.Length];
Array.Copy(j1,0,jalur1,0,j1.Length);
DFS(y,x);
bool cek = true;
int n = jalur1.Length;
int m = j1.Length;
int j,i =0;
while(i<n-1 && cek){ // tidak mengecek start state
j= 0;
while(j<m-1 && cek){ // tidak mengecek start state
if(j1[j] == jalur1[i]){
cek = false;
} else{
j++;
}
}
i++;
}
if(cek){
Console.WriteLine("YA");
} else{
Console.WriteLine("TIDAK");
}
}
public void DFS(int s,int d){
bool[] visited = new bool[simpul+1];
jalur = new Stack<Int32>();
visited[s] = true;
jalur.Push(s);
if(s!=d){
foreach(int i in sisi[s]) {
DFSR(i,d,visited);
//Console.WriteLine(s);
}
} else{
j1 = new int[jalur.Count];
jalur.CopyTo(j1,0);
}
}
public void DFSR(int s,int d,bool[] visited){
jalur.Push(s);
//Console.WriteLine(s);
visited[s] = true;
if(s != d){
foreach(int i in sisi[s]){
if(!visited[i]){
DFSR(i,d,visited);
}
}
} else{
j1 = new int[jalur.Count];
jalur.CopyTo(j1,0);
}
visited[s] = false;
jalur.Pop();
}
}
class Test{
public static void Main(String[] args){
using(TextReader reader = new StreamReader("tc.txt")){
int v = Convert.ToInt32(reader.ReadLine());
int x,y,k;
string[] tokens;
graph g = new graph(v);
for(int i =1;i<v;i++){
tokens = reader.ReadLine().Split();
x = int.Parse(tokens[0]);
y = int.Parse(tokens[1]);
g.addEdge(x,y);
}
using(TextReader reader2 = new StreamReader("queue.txt")){
int q = Convert.ToInt32(reader2.ReadLine());
for(int i=0;i<q;i++){
tokens = reader2.ReadLine().Split();
k = int.Parse(tokens[0]);
x = int.Parse(tokens[1]);
y = int.Parse(tokens[2]);
if(k==0){
g.kasus0(x,y);
} else if(k==1){
g.kasus1(x,y);
}
}
}
}
}
}
}