結果

問題 No.382 シャイな人たち (2)
ユーザー eitahoeitaho
提出日時 2016-08-21 07:20:14
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 459 ms / 8,000 ms
コード長 9,193 bytes
コンパイル時間 1,166 ms
コンパイル使用メモリ 113,152 KB
実行使用メモリ 23,296 KB
最終ジャッジ日時 2024-11-07 21:48:00
合計ジャッジ時間 9,624 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 459 ms
23,168 KB
testcase_01 AC 361 ms
23,040 KB
testcase_02 AC 334 ms
23,296 KB
testcase_03 AC 340 ms
23,296 KB
testcase_04 AC 374 ms
23,296 KB
testcase_05 AC 412 ms
23,296 KB
testcase_06 AC 453 ms
23,296 KB
testcase_07 AC 295 ms
23,296 KB
testcase_08 AC 383 ms
23,296 KB
testcase_09 AC 433 ms
23,296 KB
testcase_10 AC 320 ms
23,296 KB
testcase_11 AC 326 ms
23,168 KB
testcase_12 AC 367 ms
23,296 KB
testcase_13 AC 337 ms
23,168 KB
testcase_14 AC 345 ms
23,296 KB
testcase_15 AC 318 ms
23,296 KB
testcase_16 AC 357 ms
23,168 KB
testcase_17 AC 234 ms
23,296 KB
testcase_18 AC 276 ms
23,168 KB
testcase_19 AC 282 ms
23,168 KB
testcase_20 AC 29 ms
18,944 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
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 searcher = Init(Reader.Int());
        var set = searcher.Search();
        int ans = set.Count() + 1;
        if (ans > searcher.N) Console.WriteLine(-1);
        else Console.WriteLine(ans + "\n" + string.Join(" ", Enu.Range(1, searcher.N).Where(i => set.Get(i - 1))));
    }


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

        public MaxCliqueSearcher(int n)
        {
            N = n;
            E = new BitSet[N];
            ColorSet = new BitSet[N];
        }
        public void AddEdge(int a, int b)
        {
            E[a].Set(b);
            E[b].Set(a);
        }
        public BitSet Search()
        {
            BitSet rem = new BitSet();
            for (int i = 0; i < N; i++) rem.Set(i);
            Rec(new BitSet(), rem);
            return Ans;
        }

        void Rec(BitSet selected, BitSet rem)
        {
            RecCount++;
            if (selected.Count() > AnsCount)
            {
                AnsCount = selected.Count();
                Ans = selected;
            }
            if (selected.Count() + rem.Count() <= AnsCount) return;
            var cand = SortCand(AnsCount - selected.Count() + 1, rem);
            foreach (int a in cand)
            {
                selected.Set(a);
                rem.Reset(a);
                Rec(selected, rem.And(E[a]));
                selected.Reset(a);
            }
        }

        int[] SortCand(int atleast, BitSet rem)
        {
            int count = rem.Count(), numColor;
            int[] vs, deg;
            BitSet[] colorSet = new BitSet[count];
            SortByDegDescending(rem, count, out vs, out deg);
            Coloring(atleast, count, vs, out numColor, colorSet);
            if (numColor < atleast) return new int[0];

            int num = 0, ni = 0, startCol = atleast - 1;
            for (int col = startCol; col < numColor; col++)
                num += colorSet[col].Count();
            var cand = new int[num];
            for (int col = startCol; col < numColor; col++)
                for (var set = colorSet[col]; set.Any(); )
                {
                    int a = set.Rightmost();
                    cand[ni] = a;
                    deg[ni++] = E[a].And(rem).Count();
                    set.Reset(a);
                }
            Array.Sort(deg, cand, 0, num);

            return cand;
        }

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

        BitSet[] ColorSet;
        void Coloring(int atleast, int count, int[] vs, out int numColor, BitSet[] colorSet)
        {
            numColor = 0;
            for (int i = 0; i < vs.Length; i++)
            {
                if (numColor + vs.Length - i < atleast) return;
                int a = vs[i];
                int c = 0;
                for (var e = E[a]; e.Intersect(colorSet[c]); ) c++;
                if (c == numColor) numColor++;
                colorSet[c].Set(a);
                if (c == numColor - 1 && c == atleast)
                {
                    FixColor(a, c, atleast, colorSet);
                    if (!colorSet[c].Any()) numColor--;
                }
            }
            if (numColor == atleast && colorSet[numColor - 1].Count() <= 3)
            {
                for (var set = colorSet[numColor - 1]; set.Any(); )
                {
                    int a = set.Rightmost();
                    if (!FixColor(a, numColor - 1, atleast, colorSet)) return;
                    set.Reset(a);
                }
                numColor--;
            }
        }

        bool FixColor(int a, int col, int ub, BitSet[] colorSet)
        {
            var e = E[a];
            for (int col1 = 0; col1 < ub - 1; col1++)
            {
                int b = e.And(colorSet[col1]).Single();
                if (b == -1) continue;
                for (int col2 = col1 + 1; col2 < ub; col2++)
                    if (!E[b].Intersect(colorSet[col2]))
                    {
                        colorSet[col].Reset(a);
                        colorSet[col1].Set(a);
                        colorSet[col1].Reset(b);
                        colorSet[col2].Set(b);
                        return true;
                    }
            }
            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 int Single()
        {
            if (M2 == 0)
            {
                if (M1 != 0 && (M1 & M1 - 1) == 0)
                    return BitCount64((M1 & -M1) - 1);
            }
            else if (M1 == 0 && (M2 & M2 - 1) == 0)
                return 64 + BitCount64((M2 & -M2) - 1);
            return -1;
        }
        public bool Intersect(BitSet B)
        {
            return (M1 & B.M1) != 0 || (M2 & B.M2) != 0;
        }
        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);
    }

    MaxCliqueSearcher Init(int S, int minN = 2, int maxN = 121)
    {
        Func<int> Next = () => S = (int)(S * 12345L % 1000003);
        int N = minN + Next() % (maxN - minN + 1);
        var clique = new MaxCliqueSearcher(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