Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.io.PrintWriter;
- import java.math.BigInteger;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- import java.util.StringTokenizer;
- /**
- * <b> Algorithm on Codeforces. Problem C div 2 </b> </br>
- * @author Huynh Quang Thao
- *
- */
- public class Main {
- public static void main(String[] args) throws Exception {
- Main main = new Main();
- main.run();
- }
- public void run() throws Exception {
- Scanner sc = null;
- PrintWriter pr = null;
- pr=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
- sc = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
- // sc = new Scanner(new File("input.txt"));
- int n = sc.nextInt();
- int[] revpath = new int[n+1];
- int[] type = new int[n+1];
- int[] count = new int[n+1];
- int[] path = new int[n+1];
- for (int i = 1; i <= n; i++) type[i] = sc.nextInt();
- for (int i = 1; i <= n; i++) {
- int a = sc.nextInt();
- // exist path from a to i
- path[a] = i;
- count[a]++;
- }
- // building rev-path
- for (int i = 1; i <= n; i++) {
- int to = path[i];
- if (count[i] == 1) revpath[to] = i;
- }
- /* for (int i = 1; i <= n; i++) {
- System.out.println(path[i]);
- }
- System.out.println("---------");
- for (int i = 1; i <= n; i++) {
- System.out.println(revpath[i]);
- }*/
- // dfs from hotel
- List<Integer> resList = new ArrayList<Integer>();
- for (int i = 1; i <= n; i++) {
- if (type[i] == 1) {
- int v = i;
- // dfs from v - vertex
- List<Integer> list = new ArrayList<Integer>();
- list.add(v);
- while (revpath[v] != 0) {
- v = revpath[v];
- list.add(v);
- }
- if (list.size() > resList.size()) resList = list;
- }
- }
- System.out.printf("%d\n", resList.size());
- for (int i = resList.size()-1; i >= 0; i--) System.out.printf("%d ", resList.get(i));
- pr.close();
- sc.close();
- }
- public void dfs(int v) {
- }
- static class Team {
- int baloon;
- int rA;
- int rB;
- public Team(int baloon, int rA, int rB) {
- this.baloon = baloon;
- this.rA = rA;
- this.rB = rB;
- }
- }
- static class InputReader {
- public BufferedReader reader;
- public StringTokenizer tokenizer;
- public InputReader(InputStream stream) {
- reader = new BufferedReader(new InputStreamReader(stream));
- tokenizer = null;
- }
- public String next() {
- while (tokenizer == null || !tokenizer.hasMoreTokens()) {
- try {
- tokenizer = new StringTokenizer(reader.readLine());
- }
- catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- return tokenizer.nextToken();
- }
- public int nextInt() {
- return Integer.parseInt(next());
- }
- public double nextDouble() {
- return Double.parseDouble(next());
- }
- public float nextFloat() {
- return Float.parseFloat(next());
- }
- public long nextLong() {
- return Long.parseLong(next());
- }
- public BigInteger nextBigInteger() {
- return new BigInteger(next());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment