题意:
有两台机器A,B,分别有n,m种模式,初始都在0模式,现在有k项任务,每项任务要求A或者B调到对应的模式才能完成。问最少要给机器A,B调多少次模式可以完成任务。
思路:
相当于是在以n、m个点构成的二分图中,求二分图的最小顶点覆盖数(就是每个任务都涉及到,所需的顶点数)。根据Konig定理,二分图的最小顶点覆盖数就是求最大匹配数,注意这里是Base 0的,就是初始不用调整模式就可以完成0模式的任务,所以读入的时候不用考虑与0相连的边。
#include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdlib> #include <iomanip> #include <bitset> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <cmath> #include <queue> #include <list> #include <map> #include <set> using namespace std; //#pragma GCC optimize(3) //#pragma comment(linker, "/STACK:102400000,102400000") //c++ #define lson (l , mid , rt << 1) #define rson (mid + 1 , r , rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << "\n"; #define pb push_back #define pq priority_queue typedef long long ll; typedef unsigned long long ull; typedef pair<ll ,ll > pll; typedef pair<int ,int > pii; typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q #define fi first #define se second //#define endl ‘\n‘ #define OKC ios::sync_with_stdio(false);cin.tie(0) #define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行 #define REP(i , j , k) for(int i = j ; i < k ; ++i) //priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 1e9+7; const double esp = 1e-8; const double PI=acos(-1.0); template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<‘0‘||ch>‘9‘) f|=(ch==‘-‘),ch=getchar(); while (ch>=‘0‘&&ch<=‘9‘) x=x*10+ch-‘0‘,ch=getchar(); return x=f?-x:x; } // #define _DEBUG; //*// #ifdef _DEBUG freopen("input", "r", stdin); // freopen("output.txt", "w", stdout); #endif /*-----------------------showtime----------------------*/ const int maxn = 1e5+9; struct edge{ int v,nx; }e[maxn]; int h[maxn],tot = 0; void addedge(int u,int v){ e[tot].v = v; e[tot].nx = h[u]; h[u] = tot++; } int mx[maxn],my[maxn],vis[maxn]; bool dfs(int x){ for(int i = h[x]; ~i; i = e[i].nx){ int v = e[i].v; if(vis[v]==0){ vis[v] = 1; if(mx[v]==-1||dfs(mx[v])){ mx[v] = x; my[x] = v; return true; } } } return false; } int main(){ int n,m,k; while(~scanf("%d", &n) && n){ tot = 0; memset(h,-1,sizeof(h)); memset(mx,-1,sizeof(mx)); memset(my,-1,sizeof(my)); scanf("%d%d", &m, &k); for(int i=1; i<=k; i++){ int u,v,q; scanf("%d%d%d", &q, &u, &v); if(u*v)addedge(u,v); } int ans = 0; for(int i=1; i<n; i++){ memset(vis,0,sizeof(vis)); if(dfs(i))ans++; } printf("%d\n", ans); } return 0; }
POJ1325
原文地址:https://www.cnblogs.com/ckxkexing/p/9527124.html
时间: 2024-10-02 10:35:17