コード例 #1
0
    public static void Main(string[] args)
    {
        Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
        int CASES = int.Parse(Console.ReadLine());

        for (int CASE = 1; CASE <= CASES; CASE++)
        {
            Console.Error.WriteLine("Case " + CASE);

            int        n  = int.Parse(Console.ReadLine());
            string[][] ws = new string[n][];
            for (int i = 0; i < n; i++)
            {
                ws[i] = Console.ReadLine().Split();
            }

            Dictionary <string, int> count1 = new Dictionary <string, int>();
            Dictionary <string, int> count2 = new Dictionary <string, int>();
            for (int i = 0; i < n; i++)
            {
                if (count1.ContainsKey(ws[i][0]))
                {
                    count1[ws[i][0]]++;
                }
                else
                {
                    count1[ws[i][0]] = 1;
                }
                if (count2.ContainsKey(ws[i][1]))
                {
                    count2[ws[i][1]]++;
                }
                else
                {
                    count2[ws[i][1]] = 1;
                }
            }

            string[] firsts  = count1.Keys.ToArray();
            string[] seconds = count2.Keys.ToArray();

            MaxFlow mf = new MaxFlow(n * 2);
            for (int i = 0; i < firsts.Length; i++)
            {
                mf.Add(mf.Source, i, count1[firsts[i]] - 1);
            }

            for (int i = 0; i < seconds.Length; i++)
            {
                mf.Add(n + i, mf.Sink, count2[seconds[i]] - 1);
            }

            for (int i = 0; i < n; i++)
            {
                mf.Add(Array.IndexOf(firsts, ws[i][0]), n + Array.IndexOf(seconds, ws[i][1]), 1);
            }

            Console.WriteLine("Case #" + CASE + ": " + mf.NetworkFlow());
        }
    }
コード例 #2
0
    public static int Solve(HashSet <string>[] sent)
    {
        int N = sent.Length;
        Dictionary <string, int> words = new Dictionary <string, int>();

        int[][] ws = new int[N][];
        for (int i = 0; i < N; i++)
        {
            ws[i] = new int[sent[i].Count];
            int j = 0;
            foreach (string w in sent[i])
            {
                if (!words.ContainsKey(w))
                {
                    words[w] = words.Count;
                }
                ws[i][j++] = words[w];
            }
        }

        MaxFlow mf = new MaxFlow(N + words.Count * 2);

        for (int i = 0; i < words.Count; i++)
        {
            mf.Add(N + i, N + words.Count + i, 1);
        }

        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < ws[i].Length; j++)
            {
                mf.Add(i, N + ws[i][j], 1);
                mf.Add(N + words.Count + ws[i][j], i, 1);
            }
        }

        return(mf.NetworkFlow(0, 1));
    }
コード例 #3
0
ファイル: 16.cs プロジェクト: qifanyyy/CLCDSA
    public static string Solve()
    {
        // Read input
        string[] ps = Console.ReadLine().Split();
        int      N  = int.Parse(ps[0]);
        int      R  = int.Parse(ps[1]);
        int      O  = int.Parse(ps[2]);
        int      Y  = int.Parse(ps[3]);
        int      G  = int.Parse(ps[4]);
        int      B  = int.Parse(ps[5]);
        int      V  = int.Parse(ps[6]);

        // Static stuff
        int[]  counts = new int[6];
        string chars  = "RYBGVO";

        counts[0]         = R;
        counts[1]         = Y;
        counts[2]         = B;
        counts[3]         = G;
        counts[4]         = V;
        counts[5]         = O;
        bool[,] conflicts = new bool[7, 7];
        for (int i = 0; i < 6; i++)
        {
            conflicts[i, i] = true;
        }
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                if (i != j)
                {
                    conflicts[i, 3 + j] = conflicts[3 + j, i] = true;
                }
            }
        }

        // Do a matching to find a neighbour for everyone
        MaxFlow mf  = new MaxFlow(N * 2);
        int     idx = 0;

        int[] col = new int[N];
        for (int i = 0; i < 6; i++)
        {
            for (int j = 0; j < counts[i]; j++)
            {
                col[idx++] = i;
            }
        }
        for (int i = 0; i < N; i++)
        {
            mf.Add(mf.Source, i, 1);
            mf.Add(N + i, mf.Sink, 1);
            for (int j = 0; j < N; j++)
            {
                if (!conflicts[col[i], col[j]])
                {
                    mf.Add(i, N + j, 1);
                }
            }
        }
        if (mf.NetworkFlow() != N)
        {
            return(null);
        }
        int[] next = new int[N];
        for (int i = 0; i < N; i++)
        {
            while (mf.flow[i, N + next[i]] == 0)
            {
                next[i]++;
            }
        }

        // Construct cycles
        bool[]             done   = new bool[N];
        List <List <int> > cycles = new List <List <int> >();

        for (int i = 0; i < N; i++)
        {
            if (done[i])
            {
                continue;
            }
            List <int> cycle = new List <int>();
            cycle.Add(i);
            int cur = next[i];
            while (cur != i)
            {
                done[cur] = true;
                cycle.Add(cur);
                cur = next[cur];
            }
            cycles.Add(cycle);
        }

        while (cycles.Count > 1)
        {
            // merge two cycles
            bool found  = false;
            int  mergeC = -1;
            int  mergeI = -1;
            int  mergeK = -1;
            for (int i = 0; !found && i < cycles[0].Count; i++)
            {
                for (int j = 1; !found && j < cycles.Count; j++)
                {
                    for (int k = 0; !found && k < cycles[j].Count; k++)
                    {
                        if (col[cycles[0][i]] == col[cycles[j][k]])
                        {
                            found  = true;
                            mergeC = j;
                            mergeI = i;
                            mergeK = k;
                        }
                    }
                }
            }
            if (!found)
            {
                return(null);
            }
            List <int> comb = new List <int>();

            comb.AddRange(cycles[0].GetRange(0, mergeI));
            comb.AddRange(cycles[mergeC].GetRange(mergeK, cycles[mergeC].Count - mergeK));
            comb.AddRange(cycles[mergeC].GetRange(0, mergeK));
            comb.AddRange(cycles[0].GetRange(mergeI, cycles[0].Count - mergeI));

            cycles.RemoveAt(0);
            cycles.RemoveAt(0);
            cycles.Add(comb);
        }

        // Construct the anwer
        char[] res = new char[N];
        for (int i = 0; i < N; i++)
        {
            res[i] = chars[col[cycles[0][i]]];
        }
        return(new string(res));
    }
コード例 #4
0
    public static void Main(string[] args)
    {
        int cases = int.Parse(Console.ReadLine());

        while (cases-- > 0)
        {
            int      N    = int.Parse(Console.ReadLine());
            string[] grid = new string[N];
            for (int i = 0; i < N; i++)
            {
                grid[i] = Console.ReadLine();
            }
            int notPlayed = 0;
            int[,] ind = new int[N, N];
            for (int i = 0; i < N; i++)
            {
                for (int j = i + 1; j < N; j++)
                {
                    if (grid[i][j] == '.')
                    {
                        ind[i, j] = N + notPlayed++;
                    }
                }
            }
            MaxFlow mf     = new MaxFlow(notPlayed + N);
            int[]   pts    = new int[N];
            int[]   optpts = new int[N];
            int     totalP = 0;
            for (int i = 0; i < N; i++)
            {
                mf.Add(i, mf.Sink, 2 * N);
                for (int j = 0; j < N; j++)
                {
                    if (grid[i][j] == '.')
                    {
                        optpts[i] += 2;
                        if (i < j)
                        {
                            totalP += 2;
                            mf.Add(mf.Source, ind[i, j], 2);
                            mf.Add(ind[i, j], i, 2);
                            mf.Add(ind[i, j], j, 2);
                        }
                    }
                    else if (grid[i][j] == '1')
                    {
                        pts[i] += 2;
                    }
                    else if (grid[i][j] == 'd')
                    {
                        pts[i] += 1;
                    }
                }
            }
            List <int> canWin = new List <int>();
            for (int i = 0; i < N; i++)
            {
                bool ok = true;
                for (int j = 0; j < N; j++)
                {
                    if (j == i)
                    {
                        mf.Capacity[j, mf.Sink] = optpts[i];
                    }
                    else
                    {
                        mf.Capacity[j, mf.Sink] = pts[i] + optpts[i] - pts[j];
                        ok &= mf.Capacity[j, mf.Sink] >= 0;
                    }
                }
                if (ok && mf.NetworkFlow() == totalP)
                {
                    canWin.Add(i + 1);
                }
            }
            Console.WriteLine(string.Join(" ", canWin.Select(i => i.ToString()).ToArray()));
        }
    }