POJ-4109:公共朋友-Common Friends

import java.util.Scanner;

// Common Friends-4109
public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int c = input.nextInt();
        
        for (int i = 0; i < c; i++) {
            int n = input.nextInt();
            int m = input.nextInt();
            int k = input.nextInt();
            int[][] f_s = new int[m][2]; // 朋友关系
            int[][] p_s = new int[k][2]; // 问题关系
            int[][] record = new int[n][n]; // 记录每个人的朋友关系
            
            // 获得关系
            for (int j = 0; j < m; j++) {
                f_s[j][0] = input.nextInt();
                f_s[j][1] = input.nextInt();
                
                record[f_s[j][0] - 1][f_s[j][1] - 1] = 1;
                record[f_s[j][1] - 1][f_s[j][0] - 1] = 1;
            }
            
            // 获得问题
            for (int j = 0; j < k; j++) {
                p_s[j][0] = input.nextInt();
                p_s[j][1] = input.nextInt();
            }
            
            // 处理问题
            System.out.println("Case " + (i + 1) + ":");
            
            for (int j = 0; j < k; j++) {
                System.out.println(getCommandFirends(p_s[j], record));
            }
        }
    }

    private static int getCommandFirends(int[] p, int[][] record) {
        int count = 0;
        double n = record.length;
        for (int i = 0; i < n; i++) {
            if ((record[p[0] - 1][i] == 1) && (record[p[1] - 1][i] == 1)) {
                count++;
            }
        }
        
        return count;
    }
    
}

 

你可能感兴趣的:(com)