1 / 2
Apr 2020
 import java.util.*;
class BENEFACT{
	public static class Pair{
		int v;
		int w;
		public Pair(int v, int w){
			this.v=v;
			this.w=w;
		}
	}
	static int res = 0;	
	public static void main(String[] args) throws Exception{
		Scanner sc = new Scanner(System.in);
		StringBuilder sb = new StringBuilder("");		
		int t = sc.nextInt();
		while(t-- != 0){
			int n = sc.nextInt();
			Map<Integer, List<Pair>> mp = new HashMap<>();
			for(int i=1; i<=n; ++i)
				mp.putIfAbsent(i, new ArrayList<Pair>());
			for(int i=0; i<n-1; ++i){
				int src = sc.nextInt();
				int dst = sc.nextInt();
				int w = sc.nextInt();
				mp.get(src).add(new Pair(dst,w));
				mp.get(dst).add(new Pair(src,w));
			}
			for(int i=1; i<=n; ++i)
				dfs(new Pair(i,0), mp, new HashSet<Integer>());
			sb.append(res + "\n");
			res = 0;
		}
		System.out.print(sb.toString());
	}
	public static void dfs(Pair node, Map<Integer, List<Pair>> mp, Set<Integer> set){
		set.add(node.v);
		for(Pair adj : mp.get(node.v)){
			if(!set.contains(adj.v)){
				dfs(new Pair(adj.v, node.w+adj.w), mp, set);
			}
		}
		res = Math.max(res, node.w);
		set.remove(node.v);
	} 

}

I am trying to solve BENEFACT problem which seems to be an easy problem. This program compiles and runs on my system but as soon as I submit I am getting compilation error on spoj. Thanks in advance.

  • created

    Apr '20
  • last reply

    Apr '20
  • 1

    reply

  • 461

    views

  • 2

    users

  • 1

    link

Click on the “Compilation error” text in the status, and it will show you what the actual error is.

You could try the code on ideone.com1, as that’s run by the same people as SPOJ and may be a closer match than your system.