トップページに戻る
   次の競技プログラミングの問題へ
   前の競技プログラミングの問題へ
ABC-118-B Foods Loved by Everyone
■■■問題■■■
カツサンドくんはオムライスが好きです。
他にも明太子や寿司、クリームブリュレやテンダーロインステーキなどが好きで、
これらの食べ物は全て、誰もが好きだと信じています。
その仮説を証明するために、N人の人にM種類の食べ物について好きか嫌いかの調査を行いました。
調査の結果、i番目の人は、Ai1番目 , Ai2番目 , ... , AiKi番目の食べ物だけ好きだと答えました。
N人全ての人が好きだと答えた食べ物の種類数を求めてください。
■■■入力■■■
N M
K1 A11 A12 ・・・ A1K1
K2 A21 A22 ・・・ A2K2
・
・
・
KN AN1 AN2 ・・・ ANKN
●入力は全て整数である
●1 <= N,M <= 30
●1 <= Ki <= 30
●1 <= Aij <= M
●各i (1 <= i <= N) について Ai1 , Ai2 , ・・・ , AiKi は全て異なる。
■■■出力■■■
N人全ての人が好きだと答えた食べ物の種類数を出力せよ。
C#のソース
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
    static string InputPattern = "InputX";
    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();
        if (InputPattern == "Input1") {
            WillReturn.Add("3 4");
            WillReturn.Add("2 1 3");
            WillReturn.Add("3 1 2 3");
            WillReturn.Add("2 3 2");
            //1
            //3人全員が好きだと答えた食べ物は
            //3番目の食べ物だけなので1を出力します。
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("5 5");
            WillReturn.Add("4 2 3 4 5");
            WillReturn.Add("4 1 3 4 5");
            WillReturn.Add("4 1 2 4 5");
            WillReturn.Add("4 1 2 3 5");
            WillReturn.Add("4 1 2 3 4");
            //0
            //カツサンドくんの仮説は全く正しくありませんでした。
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("1 30");
            WillReturn.Add("3 5 10 30");
            //3
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }
    static void Main()
    {
        List<string> InputList = GetInputList();
        var AArrList = new List<int[]>();
        foreach (string EachStr in InputList.Skip(1)) {
            int[] wkArr = { };
            Action<string> SplitAct = pStr =>
                wkArr = pStr.Split(' ').Select(X => int.Parse(X)).Skip(1).ToArray();
            SplitAct(EachStr);
            AArrList.Add(wkArr);
        }
        var IntersectSet = new HashSet<int>(AArrList[0]);
        for (int I = 1; I <= AArrList.Count - 1; I++) {
            IntersectSet.IntersectWith(AArrList[I]);
        }
        Console.WriteLine(IntersectSet.Count);
    }
}
解説
HashSetジェネリックで、IntersectWithメソッドを繰り返してます。