火眼金睛

来源:互联网 发布:魔兽世界mac air 编辑:程序博客网 时间:2024/04/29 16:30

现在我们需要查出一些作弊的问答社区中的ID,作弊有两种:1.A回答了B的问题,同时B回答了A的问题。那么A和B都是作弊。2.作弊ID用户A和作弊ID用户B同时回答了C的问题,那么C也是作弊。已知每个用户的ID是一串数字,一个问题可能有多个人回答

输入描述:
每组数据第一行为总问题数N(N小于等于200000),第二行开始每行一个问题,第一个数字为提问人ID,第二个数字为回答人数,后面则为所有回答人的ID。(ID均为0-1000000的整数)
输出描述:
第一行为作弊ID数量,第二行开始为从小到大的每行一个作弊ID。
输入例子:
31 1 22 1 13 2 1 2
输出例子:
31 2 3

这道题我参考使用连接图的方式来做,这样做出来的程序看起来比较清楚。

代码如下:

import java.util.*;public class Main {    public static void main(String[] args) {        Scanner s = new Scanner(System.in);        while(s.hasNext()) {            int N = s.nextInt();            int[][] table = new int[N+1][N+1];            for(int i=0; i<N; i++) {                int id = s.nextInt();                int nums = s.nextInt();                int[] ids = new int[nums];                for(int j=0; j<nums; j++) {                    ids[j] = s.nextInt();                    table[id][ids[j]] = 1;                }            }                        boolean[] isWro = new boolean[N+1];            for(int i=1; i<=N; i++) {                for(int j=1; j<=N; j++) {                if(j!=i && table[i][j]==1 && table[j][i]==1) {                        isWro[i] = true;                        isWro[j] = true;                    }                }            }                        for(int i=1; i<=N; i++) {                int count = 0;                if(isWro[i]==false) {                    for(int j=1; j<=N; j++) {                        if(j!=i && table[i][j]==1 && isWro[j]==true) {                            count++;                            if(count==2) {                                isWro[i] = true;                                break;                            }                        }                    }                }            }            int num_all = 0;            for(int i=1; i<=N; i++) {                if(isWro[i]==true)                    num_all++;            }                        System.out.println(num_all);            int t = 0;            for(int i=0; i<=N; i++) {                if(isWro[i]) {                    t++;                    System.out.print(i);                    if(t != num_all)                        System.out.print(" ");                    else                        System.out.println();                }            }        }        s.close();    }}



0 0
原创粉丝点击