-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathA.java
53 lines (47 loc) · 1.74 KB
/
A.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
import java.io.PrintStream;
import java.util.*;
/**
* APAC 2016 Practice Round Problem A: Bad Horse
* Check README.md for explanation.
*/
public class Main {
private String solve(Scanner scanner) {
HashMap<String, Integer> map=new HashMap<>();
int n=0, m=scanner.nextInt();
String[][] strings=new String[m][2];
for (int i=0;i<m;i++) {
strings[i][0]=scanner.next();
strings[i][1]=scanner.next();
if (!map.containsKey(strings[i][0])) map.put(strings[i][0], n++);
if (!map.containsKey(strings[i][1])) map.put(strings[i][1], n++);
}
boolean[][] linked=new boolean[n][n];
for (String[] s: strings) {
int x=map.get(s[0]), y=map.get(s[1]);
linked[x][y]=linked[y][x]=true;
}
return dfs(n, linked, 0, 1, new int[n])?"Yes":"No";
}
private boolean dfs(int n, boolean[][] linked, int curr, int color, int[] mark) {
if (mark[curr]!=0) {
if (mark[curr]!=color) return false;
return true;
}
mark[curr]=color;
for (int i=0;i<n;i++) {
if (linked[curr][i] && !dfs(n, linked, i, -color, mark)) return false;
}
return true;
}
public static void main(String[] args) throws Exception {
System.setOut(new PrintStream("output.txt"));
Scanner scanner=new Scanner(System.in);
int times=scanner.nextInt();
long start=System.currentTimeMillis();
for (int t=1;t<=times;t++) {
System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner)));
}
long end=System.currentTimeMillis();
System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0));
}
}