SCU 4439 Vertex Cover(二分圖最小覆蓋點)題解
阿新 • • 發佈:2018-11-02
題意:每一條邊至少有一個端點要塗顏色,問最少塗幾個點
思路:最小頂點覆蓋:用最少的點,讓每條邊都至少和其中一個點關聯,顯然是道裸最小頂點覆蓋題;
參考:二分圖
程式碼:
#include<iostream> #include<algorithm> #include<cstdio> #include<stdio.h> #include<string.h> #include<queue> #include<cmath> #include<map> #include<set> #include<vector> using namespace std; typedef unsigned long long ull; const int maxn = 500 + 10; const ull seed = 131; struct Edge{ int v, next; }edge[maxn * maxn * 2]; int head[maxn], match[maxn], vis[maxn], tot; void addEdge(int u, int v){ edge[tot].v = v; edge[tot].next = head[u]; head[u]= tot++; } bool dfs(int u){ for(int i = head[u]; i != -1; i = edge[i].next){ int v = edge[i].v; if(!vis[v]){ vis[v] = 1; if(match[v] == -1 || dfs(match[v])){ match[v] = u; match[u] = v; return true; } } }return false; } int solve(int n){ int ans = 0; memset(match, -1, sizeof(match)); for(int i = 1; i <= n; i++){ if(match[i] == -1){ memset(vis, 0, sizeof(vis)); if(dfs(i)) ans++; } } return ans; } int main(){ int n, m; while(~scanf("%d%d", &n, &m)){ memset(head, -1, sizeof(head)); tot = 0; for(int i = 1; i <= m; i++){ int u, v; scanf("%d%d", &u, &v); addEdge(u, v); addEdge(v, u); } printf("%d\n", solve(n)); } return 0; }