fork(1) download
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5. import java.io.PrintWriter;
  6. import java.util.ArrayList;
  7. import java.util.StringTokenizer;
  8.  
  9. public class SagheerAppleTree_AC1 {
  10.  
  11. static final int MAXA = 10000000;
  12. static ArrayList<Integer>[] adjList;
  13. static boolean[] blue;
  14. static int xorValue, cnt, cntBlue[], cntRed[],a[];
  15.  
  16. static void dfs(int u)
  17. {
  18. for(int v: adjList[u])
  19. dfs(v);
  20. blue[u] = adjList[u].size() == 0 || !blue[adjList[u].get(0)];
  21. if(blue[u])
  22. {
  23. xorValue ^= a[u];
  24. cntBlue[a[u]]++;
  25. ++cnt;
  26. }
  27. else
  28. cntRed[a[u]]++;
  29. }
  30.  
  31. public static void main(String[] args) throws IOException {
  32. Scanner sc = new Scanner(System.in);
  33. PrintWriter out = new PrintWriter(System.out);
  34.  
  35. int n = sc.nextInt();
  36. a = new int[n];
  37. for(int i = 0; i < n; ++i)
  38. a[i] = sc.nextInt();
  39. adjList = new ArrayList[n];
  40. for(int i = 0; i < n; ++i)
  41. adjList[i] = new ArrayList<>(1);
  42. for(int i = 1; i < n; ++i)
  43. adjList[sc.nextInt() - 1].add(i);
  44. blue = new boolean[n];
  45. cntBlue = new int[MAXA + 1];
  46. cntRed = new int[MAXA + 1];
  47. dfs(0);
  48. long ans = 0;
  49. if(xorValue == 0)
  50. {
  51. for(int i = 1; i <= MAXA; ++i)
  52. ans += 1l * cntBlue[i] * cntRed[i];
  53. long x = cnt, y = n - x;
  54. ans += x * (x - 1) / 2 + y * (y - 1) / 2;
  55. }
  56. else
  57. {
  58. for(int i = 1; i <= MAXA; ++i)
  59. {
  60. int j = xorValue ^ i;
  61. if(j <= MAXA)
  62. ans += 1l * cntBlue[i] * cntRed[j];
  63. }
  64. }
  65. out.println(ans);
  66. out.flush();
  67. out.close();
  68. }
  69.  
  70. static class Scanner
  71. {
  72.  
  73. public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
  74.  
  75. public String next() throws IOException
  76. {
  77. while (st == null || !st.hasMoreTokens())
  78. st = new StringTokenizer(br.readLine());
  79. return st.nextToken();
  80. }
  81.  
  82. public int nextInt() throws IOException {return Integer.parseInt(next());}
  83.  
  84. public long nextLong() throws IOException {return Long.parseLong(next());}
  85.  
  86. public String nextLine() throws IOException {return br.readLine();}
  87.  
  88. public double nextDouble() throws IOException { return Double.parseDouble(next()); }
  89.  
  90. public boolean ready() throws IOException {return br.ready();}
  91. }
  92. }
  93.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:9: error: class SagheerAppleTree_AC1 is public, should be declared in a file named SagheerAppleTree_AC1.java
public class SagheerAppleTree_AC1 {
       ^
Note: Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
stdout
Standard output is empty