forked from tmwilliamlin168/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathG027-C.java
More file actions
66 lines (63 loc) · 1.32 KB
/
Copy pathG027-C.java
File metadata and controls
66 lines (63 loc) · 1.32 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
import java.io.*;
import java.util.*;
public class Main {
static final Reader in = new Reader();
public static void main(String[] args) {
int n=in.nextInt(), m=in.nextInt();
char[] s = in.next().toCharArray();
for(int i=0; i<n; ++i)
s[i]-='A';
List<Integer>[] adj = new List[n];
for(int i=0; i<n; ++i)
adj[i]=new ArrayList<Integer>();
int[][] d = new int[n][2];
for(int i=0; i<m; ++i) {
int u=in.nextInt()-1, v=in.nextInt()-1;
adj[u].add(v);
adj[v].add(u);
++d[u][s[v]];
++d[v][s[u]];
}
boolean[] g = new boolean[n];
int[] qu = new int[n];
int qt=0;
for(int i=0; i<n; ++i) {
if(d[i][0]<=0||d[i][1]<=0) {
g[i]=true;
qu[qt++]=i;
}
}
for(int qh=0; qh<n; ++qh) {
if(qh>=qt) {
System.out.println("Yes");
return;
}
int u=qu[qh];
for(int v : adj[u]) {
if(g[v])
continue;
--d[v][s[u]];
if(d[v][s[u]]<=0) {
g[v]=true;
qu[qt++]=v;
}
}
}
System.out.println("No");
}
static class Reader {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
String next() {
while(st==null||!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch(Exception e) {}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
}
}