結果

問題 No.382 シャイな人たち (2)
ユーザー eitaho
提出日時 2016-08-19 22:32:21
言語 C#
(csc 3.100.19.26603)
結果
AC  
実行時間 3,566 ms
コード長 6,782 Byte
コンパイル時間 1,359 ms
使用メモリ 24,960 KB
最終ジャッジ日時 2019-10-08 22:08:43

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
input-00 AC 3,113 ms
22,916 KB
input-01 AC 2,709 ms
20,872 KB
input-02 AC 2,354 ms
20,864 KB
input-03 AC 2,719 ms
22,912 KB
input-04 AC 2,561 ms
22,916 KB
input-05 AC 3,218 ms
22,912 KB
input-06 AC 2,849 ms
24,952 KB
input-07 AC 2,162 ms
22,912 KB
input-08 AC 2,430 ms
22,908 KB
input-09 AC 3,566 ms
24,960 KB
input-10 AC 2,118 ms
22,904 KB
input-11 AC 1,793 ms
22,928 KB
input-12 AC 2,978 ms
22,912 KB
input-13 AC 2,433 ms
22,908 KB
input-14 AC 2,833 ms
20,868 KB
input-15 AC 2,399 ms
24,956 KB
input-16 AC 2,451 ms
24,960 KB
input-17 AC 1,716 ms
20,868 KB
input-18 AC 1,863 ms
24,952 KB
input-19 AC 1,779 ms
24,952 KB
input-a0 AC 35 ms
14,356 KB
テストケース一括ダウンロード
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.100.19.26603 (9d80dea7)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using Enu = System.Linq.Enumerable;

public class Program
{
    public void Solve()
    {
        var c = Init(Reader.Int());
        var set = c.Run();
        int ans = set.Count() + 1;
        if (ans > c.N) Console.WriteLine(-1);
        else Console.WriteLine(ans + "\n" + string.Join(" ", Enu.Range(1, c.N).Where(i => set.Get(i - 1))));
    }


    public class MaxClique
    {
        public readonly int N;
        public BitSet[] E;
        public BitSet Ans;
        public int AnsCount;

        public MaxClique(int n)
        {
            N = n;
            E = new BitSet[N];
        }
        public void AddEdge(int a, int b)
        {
            E[a].Set(b);
            E[b].Set(a);
        }
        public BitSet Run()
        {
            Ans = new BitSet();
            AnsCount = 0;
            BitSet rem = new BitSet();
            for (int i = 0; i < N; i++) rem.Set(i);
            SearchClique(new BitSet(), rem);
            return Ans;
        }

        void SearchClique(BitSet selected, BitSet rem, int depth = 0)
        {
            int count = selected.Count();
            if (count + rem.Count() <= AnsCount) return;
            if (count > AnsCount)
            {
                AnsCount = count;
                Ans = selected;
            }
            int[] cand, deg;
            SortCand(rem, out cand, out deg);
            if (!Coloring(AnsCount - selected.Count() + 1, rem, cand, deg)) return;
            foreach (int a in cand)
            {
                rem.Reset(a);
                selected.Set(a);
                SearchClique(selected, rem.And(E[a]), depth + 1);
                selected.Reset(a);
            }
        }

        void SortCand(BitSet rem, out int[] cand, out int[] deg)
        {
            int ni = 0, a;
            int count = rem.Count();
            cand = new int[count];
            deg = new int[count];
            for (var set = rem; set.Any(); set.Reset(a))
            {
                a = set.Rightmost();
                cand[ni] = a;
                deg[ni] = E[a].And(rem).Count();
                ni++;
            }
            Array.Sort(deg, cand);
        }

        bool Coloring(int atleast, BitSet rem, int[] cand, int[] deg)
        {
            int maxColor = 0;
            var colors = new BitSet[rem.Count()];
            for (int i = cand.Length - 1; i >= 0; i--)
            {
                if (maxColor + i + 1 < atleast) return false;
                int a = cand[i];
                var e = E[a];
                for (int c = 0; ; c++)
                    if (!e.And(colors[c]).Any())
                    {
                        if (c + 1 >= atleast) return true;
                        maxColor = Math.Max(maxColor, c + 1);
                        colors[c].Set(a);
                        break;
                    }
            }
            return false;
        }
    }

    public struct BitSet : IEquatable<BitSet>
    {
        public long M1, M2;

        public BitSet(long m1, long m2) { M1 = m1; M2 = m2; }
        public void Set(int i)
        {
            if (i < 64) M1 |= 1L << i;
            else M2 |= 1L << i;
        }
        public void Reset(int i)
        {
            if (i < 64) M1 &= ~(1L << i);
            else M2 &= ~(1L << i);
        }
        public bool Get(int i)
        {
            if (i < 64) return (M1 >> i & 1) == 1;
            else return (M2 >> i & 1) == 1;
        }
        public int Rightmost()
        {
            Debug.Assert(M1 != 0 || M2 != 0);
            if (M1 != 0) return BitCount64((M1 & -M1) - 1);
            else return 64 + BitCount64((M2 & -M2) - 1);
        }
        public BitSet And(BitSet B) { return new BitSet(M1 & B.M1, M2 & B.M2); }
        public bool Any() { return M1 != 0 || M2 != 0; }
        public int Count() { return BitCount64(M1) + BitCount64(M2); }
        public bool Equals(BitSet B) { return M1 == B.M1 && M2 == B.M2; }
        public override string ToString()
        {
            var list = new List<int>();
            for (int i = 0; i < 128; i++) if (Get(i)) list.Add(i);
            return string.Join(" ", list);
        }
    }

    public static int BitCount64(long x)
    {
        x -= x >> 1 & 0x5555555555555555;
        x = (x & 0x3333333333333333) + (x >> 2 & 0x3333333333333333);
        return (int)((x + (x >> 4) & 0x0f0f0f0f0f0f0f0f) * 0x0101010101010101 >> 56);
    }

    MaxClique Init(int S, int minN = 2, int maxN = 121)
    {
        Func<int> Next = () => S = (int)(S * 12345L % 1000003);
        Next();
        int N = minN + S % (maxN - minN + 1);
        var clique = new MaxClique(N);
        int P = Next();
        for (int a = 0; a < N; a++)
            for (int b = a + 1; b < N; b++)
                if (Next() < P) clique.AddEdge(a, b);
        return clique;
    }

}

class Entry { static void Main() { new Program().Solve(); } }
class Reader
{
    static TextReader reader = Console.In;
    static readonly char[] separator = { ' ' };
    static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries;
    static string[] A = new string[0];
    static int i;
    static void Init() { A = new string[0]; }
    public static void Set(TextReader r) { reader = r; Init(); }
    public static void Set(string file) { reader = new StreamReader(file); Init(); }
    public static bool HasNext() { return CheckNext(); }
    public static string String() { return Next(); }
    public static int Int() { return int.Parse(Next()); }
    public static long Long() { return long.Parse(Next()); }
    public static double Double() { return double.Parse(Next()); }
    public static int[] IntLine() { return Array.ConvertAll(Split(Line()), int.Parse); }
    public static int[] IntArray(int N) { return Range(N, Int); }
    public static int[][] IntTable(int H) { return Range(H, IntLine); }
    public static string[] StringArray(int N) { return Range(N, Next); }
    public static string[][] StringTable(int N) { return Range(N, () => Split(Line())); }
    public static string Line() { return reader.ReadLine().Trim(); }
    static string[] Split(string s) { return s.Split(separator, op); }
    static T[] Range<T>(int N, Func<T> f) { var r = new T[N]; for (int i = 0; i < N; r[i++] = f()) ; return r; }
    static string Next() { CheckNext(); return A[i++]; }
    static bool CheckNext()
    {
        if (i < A.Length) return true;
        string line = reader.ReadLine();
        if (line == null) return false;
        if (line == "") return CheckNext();
        A = Split(line);
        i = 0;
        return true;
    }
}
0