-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUva11902.java
More file actions
130 lines (120 loc) · 3.91 KB
/
Uva11902.java
File metadata and controls
130 lines (120 loc) · 3.91 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import java.util.* ;
import java.io.* ;
/**
* Uva 11902 - Dominator
* @author mostafa
*/
public class Uva11902
{
static ArrayList<Integer> [] adjList ;
static boolean [] visited ;
static int N ;
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
PrintWriter pr = new PrintWriter(System.out);
StringBuilder sb = new StringBuilder();
int tc = sc.nextInt();
for (int c = 0; c < tc ; c++)
{
N = sc.nextInt();
adjList = new ArrayList[N];
visited = new boolean[N];
for (int i = 0; i < N ; i++)
adjList[i] = new ArrayList();
for (int i = 0; i < N ; i++)
for (int j = 0; j < N ; j++)
if(sc.nextInt() == 1)
adjList[i].add(j);
sb.append("Case "+(c+1)+":\n");
appendLine(N, sb);
for (int i = 0; i < N ; i++)
{
sb.append("|");
for (int j = 0; j < N ; j++)
{
if(DFS_Check_Connectivity(j)) // first check if this node is conneted
{ // connecitivity have to check firstly ...
sb.append( DFS(i, j) ? "Y|" : "N|" ); // hide(unvisit) dominant node , run dfs
} // if hit dominant node , then not dominanet
else
sb.append("N|");
}
sb.append("\n");
appendLine(N, sb);
}
}
pr.print(sb);
pr.close();
sc.close();
}
public static boolean DFS_Check_Connectivity(int node)
{
if(node==0) // node 0 always connects with graph , given in problem
return true ;
Arrays.fill(visited, false); // every time , clean this array with default valuse --> false
Stack<Integer> stack = new Stack();
stack.push(0);
visited[0] = true ;
while(!stack.isEmpty())
{
int x = getUnvisitedVertex(stack.peek());
if(x==node)
return true ;
else if( x == -1 )
stack.pop();
else
{
stack.push(x);
visited[x] = true ;
}
}
return false ;
}
public static boolean DFS(int dominator , int target )
{
if( dominator == target || dominator == 0) // every node dominates itself . node 0 always domainates
return true ; // all other nodes if this node is connected .
if( target == 0 )
return false ;
Arrays.fill(visited, false);
visited[dominator] = true ;
Stack<Integer> stack = new Stack();
stack.push(0);
visited[0] = true ;
while(!stack.isEmpty())
{
int x = getUnvisitedVertex(stack.peek());
if( x == target )
return false ;
else if( x == -1 )
stack.pop();
else
{
stack.push(x);
visited[x] = true ;
}
}
return true ;
}
public static int getUnvisitedVertex(int node)
{
int size = adjList[node].size();
for (int i = 0; i < size ; i++) {
int x = adjList[node].get(i);
if(!visited[x])
return x ;
}
return -1 ;
}
public static void appendLine(int n , StringBuilder sb)
{
for (int i = 0; i < 2*n+1 ; i++) {
if(i==0||i==2*n)
sb.append("+");
else
sb.append("-");
}
sb.append("\n");
}
}