@@ -9,8 +9,8 @@ public static void main(String[] args) throws IOException {
99 int M = Integer .parseInt (st .nextToken ());
1010 char [] lookup = f .readLine ().toCharArray ();
1111 Map <Integer , List <Integer >> connections = new HashMap <>();
12- for (int i =0 ; i < N ; i ++) {
13- connections .put (i + 1 , new ArrayList <>());
12+ for (int i =1 ; i < ( N + 1 ) ; i ++) {
13+ connections .put (i , new ArrayList <>());
1414 }
1515 for (int i = 0 ; i < N -1 ; i ++) {
1616 st = new StringTokenizer (f .readLine ());
@@ -19,46 +19,59 @@ public static void main(String[] args) throws IOException {
1919 connections .get (x ).add (y );
2020 connections .get (y ).add (x );
2121 }
22- String answer = "" ;
23- for (int i =0 ; i < M ; i ++) {
22+ int [] seg = new int [N ];
23+ int segNum = 1 ;
24+ Stack <Traversal > ts = new Stack <>();
25+
26+
27+ //int test = 0;
28+ //char target = lookup[test];
29+
30+ for (int i = 1 ; i < (N +1 ); i ++) {
31+ segNum ++;
32+ if (seg [i -1 ] != 0 ) {
33+ continue ;
34+ }
35+ char target = lookup [i -1 ];
36+ ts .push (new Traversal (i ));
37+ while (!ts .empty ()) {
38+ Traversal t = ts .pop ();
39+ seg [t .lastNode -1 ] = segNum ;
40+ for (int node : connections .get (t .lastNode )) {
41+ if (lookup [node -1 ] == target && seg [node -1 ]==0 ) {
42+ ts .push (new Traversal (node ));
43+ }
44+ }
45+ }
46+ }
47+ //System.out.println("Node numbers : {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,...}");
48+ //System.out.println("Generated Seg Data: "+Arrays.toString(seg));
49+ //System.out.println("Lookup database : "+Arrays.toString(lookup));
50+ for (int i = 0 ; i < M ; i ++) {
2451 st = new StringTokenizer (f .readLine ());
2552 int a = Integer .parseInt (st .nextToken ());
2653 int b = Integer .parseInt (st .nextToken ());
27- char pref = st .nextToken ().charAt (0 );
28- Set <Integer > visited = new HashSet <>();
29- Queue <Traversal > options = new LinkedList <>();
30- options .add (new Traversal (a , lookup [a -1 ] == pref ));
31- while (!options .isEmpty ()) {
32- Traversal t =options .poll ();
33- int lastNode = t .lastNode ;
34- if (lastNode == b ) {
35- if (lookup [b -1 ] == pref || t .good ) {
36- answer += "1" ;
37- }else {
38- answer += "0" ;
39- }
40- break ; // FINALLY
41- }
42- for (int node : connections .get (lastNode )) {
43- if (visited .contains (node )) {
44- continue ;
45- }
46- options .add (new Traversal (node , lookup [node -1 ] == pref || t .good ));
54+ char T = st .nextToken ().charAt (0 );
55+ if (seg [a -1 ] != seg [b -1 ]) {
56+ pw .print ("1" );
57+ }else {
58+ if (lookup [a -1 ] == T && lookup [b -1 ] == T ) {
59+ pw .print ("1" );
60+ }else {
61+ pw .print ("0" );
4762 }
48- visited .add (lastNode );
4963 }
5064 }
51- pw .println (answer );
65+ pw .println ();
5266 f .close ();
5367 pw .close ();
5468 }
5569
5670}
5771class Traversal {
5872 int lastNode ;
59- boolean good ;
60- public Traversal (int lastNode , boolean gotMilk ) {
61- this .good =gotMilk ;
73+
74+ public Traversal (int lastNode ) {
6275 this .lastNode = lastNode ;
6376 }
6477}
0 commit comments