Created
October 30, 2013 04:38
-
-
Save MasterAlish/7227294 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
public class Solution { | |
public static void main(String[] args){ | |
new Thread(new ThreadGroup("master"), new Runnable() { | |
public void run() { | |
Solution localTry4 = new Solution(); | |
localTry4.start(); | |
} | |
}, "alish", 30*1024*1024).start(); | |
} | |
private BufferedReader input; | |
private Map<Integer, List<Conditional>> subscriptions = new HashMap<Integer, List<Conditional>>(); | |
private int[] facts; | |
private boolean[] numbers = new boolean[5001]; | |
private int activatedNumbersCount = 0; | |
private void start() { | |
input = new BufferedReader(new InputStreamReader(System.in)); | |
MyScanner in = new MyScanner(nextLine()); | |
int numbersCount = in.nextInt(); | |
int factsCount = in.nextInt(); | |
int conditionalsCount = in.nextInt(); | |
rememberFacts(factsCount); | |
createAndSubscribeConditions(conditionalsCount); | |
for(int activatableNumber:facts){ | |
activateNumber(activatableNumber); | |
} | |
System.out.println(activatedNumbersCount); | |
for(int i=0;i<5001;i++) | |
if(numbers[i]) | |
System.out.print(i+" "); | |
} | |
private String nextLine(){ | |
try { | |
return input.readLine(); | |
} catch (IOException e) { | |
return ""; | |
} | |
} | |
private void createAndSubscribeConditions(int conditionalsCount) { | |
for(int c=0;c< conditionalsCount;c++){ | |
MyScanner numbers = new MyScanner(nextLine()); | |
int conditionResult = numbers.nextInt(); | |
int requiredNumbersCount = 0; | |
Conditional conditional = new Conditional(conditionResult, this); | |
while(numbers.hasNextInt()){ | |
int nextRequiredNumber = numbers.nextInt(); | |
conditional.subscribe(nextRequiredNumber, subscriptions); | |
requiredNumbersCount++; | |
} | |
conditional.setRequiredNumbersCount(requiredNumbersCount); | |
} | |
} | |
private void rememberFacts(int factsCount) { | |
facts = new int[factsCount]; | |
for(int f=0;f< factsCount;f++){ | |
facts[f] = Integer.parseInt(nextLine().trim()); | |
} | |
} | |
private void activateNumber(int number){ | |
if(numberIsNotActiveYet(number)){ | |
numbers[number] = true; | |
activatedNumbersCount++; | |
List<Conditional> subscribers = subscriptions.get(number); | |
if(subscribers != null) { //1st fault: subscribers may be NULL | |
for(Conditional conditional:subscribers){ | |
conditional.oneRequiredNumberAppeared(); | |
} | |
for(Conditional conditional:subscribers){ | |
conditional.activateIfAllRequirementsExist(); | |
} | |
} | |
} | |
} | |
private boolean numberIsNotActiveYet(int number){ | |
return !numbers[number]; | |
} | |
public void conditionActivated(int number) { | |
activateNumber(number); | |
} | |
} | |
class Conditional{ | |
private int requiredNumbersCount; | |
private int resultNumber; | |
private Solution delegate; | |
public Conditional(int resultNumber, Solution delegate){ | |
this.resultNumber = resultNumber; | |
this.delegate = delegate; | |
} | |
public void setRequiredNumbersCount(int requiredNumbersCount){ | |
this.requiredNumbersCount = requiredNumbersCount; | |
} | |
public void subscribe(int anotherRequiredNumber, Map<Integer, List<Conditional>> subscriptions){ | |
if(!subscriptions.containsKey(anotherRequiredNumber)) | |
subscriptions.put(anotherRequiredNumber, new ArrayList<Conditional>()); | |
subscriptions.get(anotherRequiredNumber).add(this); | |
} | |
public void oneRequiredNumberAppeared(){ | |
if(requiredNumbersCount>0){ | |
requiredNumbersCount--; | |
} | |
} | |
public void activateIfAllRequirementsExist(){ | |
if(requiredNumbersCount==0) | |
delegate.conditionActivated(resultNumber); | |
} | |
} | |
class MyScanner{ | |
int intsCount = 0; | |
int currentInt = 0; | |
int[] ints; | |
public MyScanner(String source) { | |
String[] intsS = source.split(" "); | |
ints = new int[intsS.length]; | |
for(int i=0;i<intsS.length;i++){ | |
ints[i] = Integer.parseInt(intsS[i]); | |
} | |
intsCount = ints.length; | |
} | |
public int nextInt() { | |
return ints[currentInt++]; | |
} | |
public boolean hasNextInt() { | |
return currentInt<intsCount; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment