Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- public class friends {
- public static void main(String[] args) throws IOException{
- BufferedReader k = new BufferedReader(new FileReader(new File("friends.in")));
- int n = Integer.parseInt(k.readLine());
- for(int asdf = 0; asdf < n; asdf++){
- int f = Integer.parseInt(k.readLine());
- String[] names = k.readLine().split(" ");
- int[][] friendswith = new int[f][f];
- int c = Integer.parseInt(k.readLine());
- for(int i = 0; i < c; i++){
- String[] connections = k.readLine().split(" ");
- int pos1 = arrayindex(names,connections[0]);
- int pos2 = arrayindex(names,connections[1]);
- friendswith[pos1][pos2] = 1;
- friendswith[pos2][pos1] = 1;
- }
- int r = Integer.parseInt(k.readLine());
- System.out.println("Social Network "+(asdf+1)+":");
- int[] ycool = new int[names.length];
- int yourfriends = sumup(friendswith[arrayindex(names,"You")]);
- int yourcoolness = sumup(findCoolness(friendswith,names,"You","You",ycool))+yourfriends;
- for(int i = 0; i < r; i++){
- String rival = k.readLine();
- int rcoolness = 0;
- int[] rcool = new int[names.length];
- if(arrayindex(names,rival) != -1){
- rcoolness = sumup(findCoolness(friendswith,names,rival,rival,rcool))+sumup(friendswith[arrayindex(names,rival)]);
- }
- System.out.println("\t"+rival+": Difference of "+ (yourcoolness-rcoolness) + " point(s)");
- }
- }
- }
- public static int arrayindex(String[] names, String name){
- for(int i = 0; i < names.length; i++){
- if(names[i].equals(name))
- return i;
- }
- return -1;
- }
- public static int[] findCoolness(int[][] friendswith, String[] names, String name, String currname, int[] allfriends){
- //name is either you or the rival
- int[] isFriends = new int[names.length]; //friends with whom
- int index = arrayindex(names, currname);//index of current name
- isFriends = friendswith[index];
- for(int i = 0; i < names.length; i++){
- if(isFriends[i] == 1 && !names[i].equals(name) && allfriends[i] == 0){
- allfriends[i] = 1;
- findCoolness(friendswith,names,name,names[i],allfriends);
- }
- }
- return allfriends;
- }
- public static int sumup(int[] coolness){
- int y = 0;
- for(int x: coolness)
- y = y + x;
- return y;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement