Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.*;
- public class Test {
- public static void main(String args[]) throws IOException {
- new Test(new Scanner(System.in));
- }
- Graph g;
- // functions as defined above
- int []dp1, dp2;
- int []C;
- // pV is parent of node V
- void dfs(int V, int pV) {
- // for storing sums of dp1 and max(dp1, dp2) for all children of V
- int sum1=0, sum2=0;
- // traverse over all children
- for(Edge v : g.adj[V]) {
- if(v.j == pV) continue;
- dfs(v.j, V);
- sum1 += dp2[v.j];
- sum2 += Math.max(dp1[v.j], dp2[v.j]);
- }
- dp1[V] = C[V] + sum1;
- dp2[V] = sum2;
- }
- Test(Scanner in) throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- dp1 = new int[50];
- dp2 = new int[50];
- g = new Graph(in.nextInt()+1);
- int M = in.nextInt();
- C = new int[] { 1, 0, 0, 22, 22, 0, 0 };
- while(M-->0)
- {
- int i = in.nextInt();
- int j = in.nextInt();
- g.add(i, j);
- }
- dfs(1, 0);
- int ans = Math.max(dp1[1], dp2[1]);
- System.out.println(ans);
- }
- class Graph
- {
- int N, M;
- ArrayList<Edge>[] adj;
- Graph(int NN)
- {
- M = 0;
- adj = new ArrayList[N=NN];
- for(int i=0; i<N; i++)
- adj[i] = new ArrayList<>();
- }
- void add(int i, int j)
- {
- adj[i].add(new Edge(j, M));
- adj[j].add(new Edge(i, M));
- M++;
- }
- }
- class Edge
- {
- int j, id;
- Edge(int jj, int ii) {
- j=jj; id=ii;
- }
- }
- }
Add Comment
Please, Sign In to add comment