-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUva10505.java
More file actions
85 lines (80 loc) · 2.56 KB
/
Copy pathUva10505.java
File metadata and controls
85 lines (80 loc) · 2.56 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
import java.util.*;
import java.io.*;
/*
* Uva 10505 - Montesco vs Capuleto
* Description : The first thing you will do is connect each two enemes by an edge .
* what will that do ?
* It makes the graph to be bipartite .It means that eac two neighbours must be assigned
* different values (different colors) . by dfs assign the valus to each subgraph .
* calculate max color for each subgraph .
* @ author Mostafa Kamel
*/
public class Uva10505
{
static ArrayList<Integer> [] adjList ;
static int [] marked ;
static int montesco = 0 , capuleto = 0 ;
static boolean isBipartite ;
static final int N = 200 ;
public static void main(String[] args) throws IOException
{
Scanner sc = new Scanner(System.in);
PrintWriter pr = new PrintWriter(System.out);
StringBuilder sb = new StringBuilder();
int tc = sc.nextInt();
while(tc-->0)
{
int n = sc.nextInt();
marked = new int[N];
adjList = (ArrayList<Integer>[]) new ArrayList[N];
for (int i = 0; i < N; i++)
adjList[i] = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
int t = sc.nextInt();
while (t-->0)
{
int x = sc.nextInt()-1;
if(x>=n) continue;
adjList[i].add(x);
adjList[x].add(i);
}
}
int max = 0 ;
for (int i = 0; i < n; i++) {
if(marked[i] == 0 )
{
isBipartite = true ;
marked[i] = 1 ;
capuleto = montesco = 0 ;
bipartite(i);
if(isBipartite)
max += Integer.max(montesco , capuleto);
}
}
sb.append(max).append("\n");
}
pr.print(sb.toString());
sc.close();
pr.close();
}
static void bipartite(int u)
{
if(marked[u] == 1)
montesco++ ;
else if( marked[u] == -1 )
capuleto++ ;
for (int i = 0; i < adjList[u].size(); i++) {
int v = adjList[u].get(i);
if(marked[v] == 0)
{
if(marked[u]==1)
marked[v] = -1 ;
else
marked[v] = 1 ;
bipartite(v);
}
else if( marked[v] == marked[u] )
isBipartite = false ;
}
}
}