Codeforces Round #588 (Div. 2) C. Anadi and Domino(思维)

link:

https://codeforces.com/contest/1230/problem/C

Meaning of the questions:

Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1≤a≤b≤6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set:

Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph.

When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots.

How many dominoes at most can Anadi place on the edges of his graph?

Ideas:

Direct enumeration does not exceed DFS.
Standard Solution: when n <= 6, any method can be filled.
When n> 6, has the same value considering the case of two points as an optimum filling, the two enumeration identical point, and then determine how many cases are not met, subtract it.

Code: DFS violent version

#include <bits/stdc++.h>
using namespace std;

int node[10];
pair<int, int> edge[30];
int cnt[10];
int ans;
int n, m;

void Dfs(int step)
{
    if (step > n)
    {
        memset(cnt, 0, sizeof(cnt));
        map<pair<int, int>, bool> Use;
        int tmp = 0;
        for (int i = 1;i <= m;i++)
        {
            int l = edge[i].first, r = edge[i].second;
            int vl = node[edge[i].first], vr = node[edge[i].second] ;
            if (vl == 0 || vr == 0)
                continue;
            if (vl > vr)
                swap(vl, vr);
            if (Use[make_pair(vl, vr)])
                continue;
            tmp++;
            Use[make_pair(vl, vr)] = true;
        }
        ans = max(ans, tmp);
        return ;
    }
    for (int i = 0;i <= 6;i++)
    {
        node[step] = i;
        Dfs(step+1);
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    int u, v;
    for (int i = 1;i <= m;i++)
    {
        cin >> u >> v;
        edge[i].first = u;
        edge[i].second = v;
    }
    Dfs(1);
    cout << ans << endl;

    return 0;
}

Guess you like

Origin www.cnblogs.com/YDDDD/p/11621681.html
Recommended