標籤:class blog code http tar color
題目來源:HDU 3118 Arbiter
題意:翻譯過來就是不能有奇圈 每走一步狀態會變化 當他回到起點時如果和原來的狀態不一樣 可能會死 求至少去掉多少條邊可以避免這種狀況發生
思路:二分圖是沒有奇圈的 最多就15個點 我們用狀態壓縮枚舉那些點是在二分圖的一邊和另外一邊 確定二分圖之後枚舉輸入的邊 每條邊串連的不能是同一集合的點
不符合就去掉 最後取小
#include <cstdio>#include <cstring>#include <cstdlib>#include <vector>#include <algorithm>using namespace std;const int maxn = 510;struct node{int t1, t2, x1, y1, x2, y2;}a[maxn];int vis[maxn];int y[maxn];vector <int> G[maxn];int n;int color[maxn];int sum = 0;bool dfs(int u){for(int i = 0; i < G[u].size(); i++){int v = G[u][i];if(vis[v])continue;vis[v] = true;if(y[v] == -1 || dfs(y[v])){y[v] = u;return true;}}return false;}int match(){int ans = 0;memset(y, -1, sizeof(y));for(int i = 0; i < n; i++){memset(vis, 0, sizeof(vis));if(dfs(i))ans++;}return ans;}int main(){int T;scanf("%d", &T);while(T--){int m;scanf("%d %d", &n, &m);for(int i = 0; i < n; i++)G[i].clear();while(m--){int u, v;scanf("%d %d", &u, &v);G[u].push_back(v);//G[v].push_back(u);}int ans = 999999999;for(int s = 0; s < (1<<n); s++){memset(color, 0, sizeof(color));for(int i = 0; i < n; i++)if(s&(1<<i))color[i] = 1;sum = 0;for(int i = 0; i < n; i++)for(int j = 0; j < G[i].size(); j++)if(color[i] == color[G[i][j]])sum++;ans = min(ans, sum);}printf("%d\n", ans);}return 0;}