Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* package whatever; // don't place package name! */
- // By Max Osipyonok
- import java.util.*;
- import java.lang.*;
- import java.io.*;
- /* Name of the class has to be "Main" only if the class is public. */
- class Ideone
- {
- static int a , s , s0 , sz_f;
- static Set<Integer> F;
- public static void main (String[] args) throws java.lang.Exception
- {
- Scanner sc = new Scanner(System.in);
- a = sc.nextInt();
- s = sc.nextInt();
- s0 = sc.nextInt();
- --s0;
- sz_f = sc.nextInt();
- F = new HashSet<Integer>();
- Queue<Integer> q = new PriorityQueue();
- for(int i = 0 ; i < sz_f ; ++i){
- int t = sc.nextInt();
- --t;
- F.add(t);
- q.add(t);
- }
- //Set<Character>[][] w = new Set<Character>[s + 1][s + 1];
- List<List<Set<Character>>> w = new ArrayList<List<Set<Character>>>();
- for(int i = 0 ; i < s ; ++i){
- List<Set<Character>> tmp = new ArrayList<Set<Character>>();
- for(int j = 0 ; j < s ; ++j){
- Set<Character> tmp2 = new HashSet<Character>();
- tmp.add(tmp2);
- }
- w.add(tmp);
- }
- while(sc.hasNext()){
- int u , v;
- char ch;
- u = sc.nextInt();
- ch = sc.next().charAt(0);
- v = sc.nextInt();
- --u;
- --v;
- w.get(u).get(v).add(ch);
- }
- for(int i = 0 ; i < s ; ++i){
- for(int j = 0 ; j < s ; ++j){
- if(w.get(i).get(j).size() == 0){
- w.get(i).get(j).add('A');
- }
- }
- }
- for(int k = 0 ; k < s ; ++k){
- for(int i = 0 ; i < s ; ++i){
- for(int j = 0 ; j < s ; ++j){
- if(w.get(i).get(j).contains('A'))continue;
- if(w.get(i).get(k).contains('*') && w.get(k).get(j).contains('*')){
- w.get(i).get(j).add('*');
- }
- }
- }
- }
- while(q.size() > 0){
- int v = q.element();
- q.remove();
- for(int u = 0 ; u < s ; ++u){
- if(w.get(u).get(v).contains('*')){
- if(!F.contains(u)){
- F.add(u);
- q.add(u);
- }
- }
- }
- }
- for(int k = 0 ; k < s ; ++k){
- for(int i = 0 ; i < s ; ++i){
- for(int j = 0 ; j < s ; ++j){
- if(w.get(i).get(k).contains('*')){
- for(char cur : w.get(k).get(j)){
- if(cur != 'A' && cur != '*'){
- w.get(i).get(j).add(cur);
- }
- }
- }
- }
- }
- }
- for(int i = 0 ; i < s ; ++i){
- for(int j = 0 ; j < s ; ++j){
- for(char cur : w.get(i).get(j)){
- if(cur != 'A' && cur != '*'){
- System.out.println((i + 1) + " " + cur + " " + (j + 1));
- }
- }
- }
- }
- System.out.print("F = { ");
- Iterator<Integer> it = F.iterator();
- while(it.hasNext()){
- int cur = it.next();
- System.out.print(cur + 1);
- if(it.hasNext()){
- System.out.print(" , ");
- }
- }
- System.out.print(" }");
- }
- }
- /*
- 2
- 4
- 1
- 2
- 1 4
- 1 a 2
- 1 * 4
- 1 b 3
- 2 b 2
- 2 a 4
- 3 a 2
- 3 * 1
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement