链接:
https://codeforces.com/contest/1230/problem/C
题意:
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?
思路:
直接DFS枚举不会超.
标准题解:当n<=6时,任何一种方法都可以填充.
当n>6时, 考虑有两个点填充相同值的情况为最优, 所以枚举两个相同点,然后判断多少种情况不满足,减一下.
代码:DFS暴力版本
#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; }