HDU-1845-Jimmy's Assignment
链接:https://vjudge.net/problem/HDU-1845
题意:
给一个有向图,求最大匹配。
思路:
有相图的最大匹配,可以通过加上反向边, 求这个无向图的最大匹配,
原图的最大匹配就是无向图的最大匹配除2.
详细解释:https://xwk.iteye.com/blog/2129301
https://blog.csdn.net/u013480600/article/details/38638219
代码:
#include <iostream>
#include <memory.h>
#include <string>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <algorithm>
#include <map>
#include <queue>
#include <math.h>
#include <cstdio>
#include <set>
#include <iterator>
#include <cstring>
using namespace std; typedef long long LL;
const int MAXN = 5e3+10;
int Next[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; vector<int> G[MAXN];
int Link[MAXN], Vis[MAXN];
int n, m, k; bool Dfs(int x)
{
for (int i = 0;i < G[x].size();i++)
{
int node = G[x][i];
if (Vis[node] == 0)
{
Vis[node] = 1;
if (Link[node] == -1 || Dfs(Link[node]))
{
Link[node] = x;
return true;
}
}
}
return false;
} int Solve()
{
memset(Link, -1, sizeof(Link));
int cnt = 0;
for (int i = 1;i <= n;i++)
{
memset(Vis, 0, sizeof(Vis));
if (Dfs(i))
cnt++;
}
return cnt;
} int main()
{
int t;
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
for (int i = 1;i <= n;i++)
G[i].clear();
int l, r;
for (int i = 1;i <= 3*n/2;i++)
{
scanf("%d%d", &l, &r);
G[l].push_back(r);
G[r].push_back(l);
}
int cnt = Solve();
printf("%d\n", cnt/2);
} return 0;
}
HDU-1845-Jimmy's Assignment的更多相关文章
- HDU 1845 Jimmy’s Assignment(二分匹配)
Jimmy’s Assignment Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Other ...
- HDU - 1845 Jimmy’s Assignment (二分匹配)
Description Jimmy is studying Advanced Graph Algorithms at his university. His most recent assignmen ...
- HDU 4014 Jimmy’s travel plan(图计数)
Jimmy’s travel plan Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Oth ...
- hdu 1845
一看题意就是二分匹配问题,建边是双向的,两个集合都是n个点 这题的图很特殊,每个点都要与三个点相连,在纸上画了六个点的图就感觉此图最大匹配肯定是六,除以2就是原图的匹配了,就感觉这样的图肯定会达到满匹 ...
- 【HDU 6006】Engineer Assignment(状压DP)
Problem Description In Google, there are many experts of different areas. For example, MapReduce exp ...
- hdu1845 Jimmy’s Assignment --- 完整匹配
意甲冠军: 它需要一个特殊的图,以找到最大匹配.该图的特征是:无向图,度的每个节点3.这是一个双边连接组件(the graph is 2-edge-connected (that is, at lea ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- hdu图论题目分类
=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...
- HDU图论题单
=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...
- 【HDOJ图论题集】【转】
=============================以下是最小生成树+并查集====================================== [HDU] How Many Table ...
随机推荐
- entity framework WithRequiredDependent和WithRequiredPrincipal
A->WithRequiredDependent->B 表示 A中包含B的不为null实例 ,A是主键实体 B是外键实体 A->WithRequiredPrincipal-> ...
- Sturs2 -概念讲解 第一弹
源码下载地址:http://struts.apache.org/ struts-2.5.14.1-all.zip --所有内容 struts-2.5.14.1-apps.zip --实例的应用 st ...
- 运算符-----------instanceof
- ACM学习历程—HDU5410 CRB and His Birthday(动态规划)
Problem Description Today is CRB's birthday. His mom decided to buy many presents for her lovely son ...
- 【Opencv】Mat基础
1.Mat::imread() C++: Mat imread(const string& filename, int flags=1 ) filename – Name of file to ...
- poj2228Naptime——环形DP
题目:http://poj.org/problem?id=2228 dp[i][j][0/1]表示前i小时中第j小时睡(1)或不睡(0)的最优值: 注意第一个小时,若睡则对最终取结果有要求,即第n个小 ...
- Unix Timestamp
class Foundation_API DateTime /// This class represents an instant in time, expressed /// in years, ...
- Python pip 报错
1,pip ssl certification ssl: certificate_verify_failed... 2,Could not find a version that satisfies ...
- strTemp.Format ("%.*lf",3,600.0);
CString strTemp; strTemp.Format ("%.*lf",3,600.0); 这句话的含义?求指教 优质解答 这就是一个格式化输出,分号之前的CStri ...
- CF-845C
C. Two TVs time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...