Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5.  
  6. public class friends {
  7. public static void main(String[] args) throws IOException{
  8. BufferedReader k = new BufferedReader(new FileReader(new File("friends.in")));
  9. int n = Integer.parseInt(k.readLine());
  10. for(int asdf = 0; asdf < n; asdf++){
  11. int f = Integer.parseInt(k.readLine());
  12. String[] names = k.readLine().split(" ");
  13. int[][] friendswith = new int[f][f];
  14. int c = Integer.parseInt(k.readLine());
  15. for(int i = 0; i < c; i++){
  16. String[] connections = k.readLine().split(" ");
  17. int pos1 = arrayindex(names,connections[0]);
  18. int pos2 = arrayindex(names,connections[1]);
  19. friendswith[pos1][pos2] = 1;
  20. friendswith[pos2][pos1] = 1;
  21. }
  22. int r = Integer.parseInt(k.readLine());
  23. System.out.println("Social Network "+(asdf+1)+":");
  24. int[] ycool = new int[names.length];
  25. int yourfriends = sumup(friendswith[arrayindex(names,"You")]);
  26. int yourcoolness = sumup(findCoolness(friendswith,names,"You","You",ycool))+yourfriends;
  27. for(int i = 0; i < r; i++){
  28. String rival = k.readLine();
  29. int rcoolness = 0;
  30. int[] rcool = new int[names.length];
  31. if(arrayindex(names,rival) != -1){
  32. rcoolness = sumup(findCoolness(friendswith,names,rival,rival,rcool))+sumup(friendswith[arrayindex(names,rival)]);
  33. }
  34. System.out.println("\t"+rival+": Difference of "+ (yourcoolness-rcoolness) + " point(s)");
  35. }
  36. }
  37. }
  38. public static int arrayindex(String[] names, String name){
  39. for(int i = 0; i < names.length; i++){
  40. if(names[i].equals(name))
  41. return i;
  42. }
  43. return -1;
  44. }
  45. public static int[] findCoolness(int[][] friendswith, String[] names, String name, String currname, int[] allfriends){
  46. //name is either you or the rival
  47. int[] isFriends = new int[names.length]; //friends with whom
  48. int index = arrayindex(names, currname);//index of current name
  49. isFriends = friendswith[index];
  50. for(int i = 0; i < names.length; i++){
  51. if(isFriends[i] == 1 && !names[i].equals(name) && allfriends[i] == 0){
  52. allfriends[i] = 1;
  53. findCoolness(friendswith,names,name,names[i],allfriends);
  54. }
  55. }
  56. return allfriends;
  57. }
  58. public static int sumup(int[] coolness){
  59. int y = 0;
  60. for(int x: coolness)
  61. y = y + x;
  62. return y;
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement