Guest User

Untitled

a guest
Mar 22nd, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. import java.util.*;
  2. public class Solution {
  3. static int answer;
  4. public static void main(String[] args) {
  5. Scanner sc=new Scanner(System.in);
  6. int t=sc.nextInt();
  7. for(int a=1;a<=t;a++) {
  8. answer=0;
  9. int n=sc.nextInt();
  10. int m=sc.nextInt();
  11. boolean[][]map=new boolean[n+1][n+1];
  12. for(int b=0;b<m;b++) {
  13. int x=sc.nextInt();
  14. int y=sc.nextInt();
  15. map[x][y]=true;
  16. map[y][x]=true;
  17. }
  18. for(int b=1;b<=n;b++) {
  19. boolean[]visited=new boolean[n+1];
  20. dfs(b,visited,n,map);
  21. }
  22. System.out.println("#"+a+" "+answer);
  23. }
  24. sc.close();
  25. }
  26.  
  27. private static void dfs(int z, boolean[] visited, int n, boolean[][] map) {
  28. boolean[]newvisited=new boolean[n+1];
  29. for(int a=1;a<=n;a++) {
  30. newvisited[a]=visited[a];
  31. }
  32. newvisited[z]=true;
  33. int count=0;
  34. for(int a=1;a<=n;a++) {
  35. if(newvisited[a]) {
  36. count++;
  37. }
  38. }
  39. answer=Math.max(answer, count);
  40. for(int a=1;a<=n;a++) {
  41. if(map[z][a]&&!newvisited[a]) {
  42. dfs(a,newvisited,n,map);
  43. }
  44. }
  45. }
  46. }
Add Comment
Please, Sign In to add comment