R - Work scheduling

Time Limit:500MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

There is certain amount of night guards that are available to protect the local junkyard from possible junk robberies. These guards need to scheduled in pairs, so that each pair guards at different night. The junkyard CEO ordered you to write a program which given the guards characteristics determines the maximum amount of scheduled guards (the rest will be fired). Please note that each guard can be scheduled with only one of his colleagues and no guard can work alone.

Input

The first line of the input contains one number N ≤ 222 which is the amount of night guards. Unlimited number of lines consisting of unordered pairs ( ij) follow, each such pair means that guard # i and guard # j can work together, because it is possible to find uniforms that suit both of them (The junkyard uses different parts of uniforms for different guards i.e. helmets, pants, jackets. It is impossible to put small helmet on a guard with a big head or big shoes on guard with small feet). The input ends with Eof.

Output

You should output one possible optimal assignment. On the first line of the output write the even number C, the amount of scheduled guards. Then output C/2 lines, each containing 2 integers ( ij) that denote that i and j will work together.

Sample Input

input output
3
1 2
2 3
1 3
2
1 2

就是有n个人,之后给出若干个关系,之后求最大可以保留多少对关系,要求可以删除若干个人

下面两个都是模板,两个模板都是可以套用的·1

1

#include<stdio.h>
#include<string.h>
#include<string.h>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
const int MAXN = ;
int N; //点的个数,点的编号从1到N
bool Graph[MAXN][MAXN];
int Match[MAXN];
bool InQueue[MAXN],InPath[MAXN],InBlossom[MAXN];
int Head,Tail;
int Queue[MAXN];
int Start,Finish;
int NewBase;
int Father[MAXN],Base[MAXN];
int Count;//匹配数,匹配对数是Count/2
void CreateGraph()
{
int u,v;
memset(Graph,false,sizeof(Graph));
scanf("%d",&N);
while(scanf("%d%d",&u,&v) == )
{
Graph[u][v] = Graph[v][u] = true;
}
}
void Push(int u)
{
Queue[Tail] = u;
Tail++;
InQueue[u] = true;
}
int Pop()
{
int res = Queue[Head];
Head++;
return res;
}
int FindCommonAncestor(int u,int v)
{
memset(InPath,false,sizeof(InPath));
while(true)
{
u = Base[u];
InPath[u] = true;
if(u == Start) break;
u = Father[Match[u]];
}
while(true)
{
v = Base[v];
if(InPath[v])break;
v = Father[Match[v]];
}
return v;
}
void ResetTrace(int u)
{
int v;
while(Base[u] != NewBase)
{
v = Match[u];
InBlossom[Base[u]] = InBlossom[Base[v]] = true;
u = Father[v];
if(Base[u] != NewBase) Father[u] = v;
}
}
void BloosomContract(int u,int v)
{
NewBase = FindCommonAncestor(u,v);
memset(InBlossom,false,sizeof(InBlossom));
ResetTrace(u);
ResetTrace(v);
if(Base[u] != NewBase) Father[u] = v;
if(Base[v] != NewBase) Father[v] = u;
for(int tu = ; tu <= N; tu++)
if(InBlossom[Base[tu]])
{
Base[tu] = NewBase;
if(!InQueue[tu]) Push(tu);
}
}
void FindAugmentingPath()
{
memset(InQueue,false,sizeof(InQueue));
memset(Father,,sizeof(Father));
for(int i = ; i <= N; i++)
Base[i] = i;
Head = Tail = ;
Push(Start);
Finish = ;
while(Head < Tail)
{
int u = Pop();
for(int v = ; v <= N; v++)
if(Graph[u][v] && (Base[u] != Base[v]) && (Match[u] != v))
{
if((v == Start) || ((Match[v] > ) && Father[Match[v]] > ))
BloosomContract(u,v);
else if(Father[v] == )
{
Father[v] = u;
if(Match[v] > )
Push(Match[v]);
else
{
Finish = v;
return;
}
}
}
}
}
void AugmentPath()
{
int u,v,w;
u = Finish;
while(u > )
{
v = Father[u];
w = Match[v];
Match[v] = u;
Match[u] = v;
u = w;
}
}
void Edmonds()
{
memset(Match,,sizeof(Match));
for(int u = ; u <= N; u++)
if(Match[u] == )
{
Start = u;
FindAugmentingPath();
if(Finish > )AugmentPath();
}
}
void PrintMatch()
{
Count = ;
for(int u = ; u <= N; u++)
if(Match[u] > )
Count++;
printf("%d\n",Count);
for(int u = ; u <= N; u++)
if(u < Match[u])
printf("%d %d\n",u,Match[u]);
}
int main()
{
CreateGraph();//建图
Edmonds();//进行匹配
PrintMatch();//输出匹配数和匹配
return ;
}

(2)

    #include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
const int N = ;
// 并查集维护
int belong[N];
int findb(int x) {
return belong[x] == x ? x : belong[x] = findb(belong[x]);
}
void unit(int a, int b) {
a = findb(a);
b = findb(b);
if (a != b) belong[a] = b;
} int n, match[N];
vector<int> e[N];
int Q[N], rear;
int next[N], mark[N], vis[N];
// 朴素算法求某阶段中搜索树上两点x, y的最近公共祖先r
int LCA(int x, int y) {
static int t = ; t++;
while (true) {
if (x != -) {
x = findb(x); // 点要对应到对应的花上去
if (vis[x] == t)
return x;
vis[x] = t;
if (match[x] != -)
x = next[match[x]];
else x = -;
}
swap(x, y);
}
} void group(int a, int p) {
while (a != p) {
int b = match[a], c = next[b]; // next数组是用来标记花朵中的路径的,综合match数组来用,实际上形成了
// 双向链表,如(x, y)是匹配的,next[x]和next[y]就可以指两个方向了。
if (findb(c) != p) next[c] = b; // 奇环中的点都有机会向环外找到匹配,所以都要标记成S型点加到队列中去,
// 因环内的匹配数已饱和,因此这些点最多只允许匹配成功一个点,在aug中
// 每次匹配到一个点就break终止了当前阶段的搜索,并且下阶段的标记是重
// 新来过的,这样做就是为了保证这一点。
if (mark[b] == ) mark[Q[rear++] = b] = ;
if (mark[c] == ) mark[Q[rear++] = c] = ; unit(a, b); unit(b, c);
a = c;
}
} // 增广
void aug(int s) {
for (int i = ; i < n; i++) // 每个阶段都要重新标记
next[i] = -, belong[i] = i, mark[i] = , vis[i] = -;
mark[s] = ;
Q[] = s; rear = ;
for (int front = ; match[s] == - && front < rear; front++) {
int x = Q[front]; // 队列Q中的点都是S型的
for (int i = ; i < (int)e[x].size(); i++) {
int y = e[x][i];
if (match[x] == y) continue; // x与y已匹配,忽略
if (findb(x) == findb(y)) continue; // x与y同在一朵花,忽略
if (mark[y] == ) continue; // y是T型点,忽略
if (mark[y] == ) { // y是S型点,奇环缩点
int r = LCA(x, y); // r为从i和j到s的路径上的第一个公共节点
if (findb(x) != r) next[x] = y; // r和x不在同一个花朵,next标记花朵内路径
if (findb(y) != r) next[y] = x; // r和y不在同一个花朵,next标记花朵内路径 // 将整个r -- x - y --- r的奇环缩成点,r作为这个环的标记节点,相当于论文中的超级节点
group(x, r); // 缩路径r --- x为点
group(y, r); // 缩路径r --- y为点
}
else if (match[y] == -) { // y自由,可以增广,R12规则处理
next[y] = x;
for (int u = y; u != -; ) { // 交叉链取反
int v = next[u];
int mv = match[v];
match[v] = u, match[u] = v;
u = mv;
}
break; // 搜索成功,退出循环将进入下一阶段
}
else { // 当前搜索的交叉链+y+match[y]形成新的交叉链,将match[y]加入队列作为待搜节点
next[y] = x;
mark[Q[rear++] = match[y]] = ; // match[y]也是S型的
mark[y] = ; // y标记成T型
}
}
}
} bool g[N][N];
int main() {
scanf("%d", &n);
for (int i = ; i < n; i++)
for (int j = ; j < n; j++) g[i][j] = false; // 建图,双向边
int x, y; while (scanf("%d%d", &x, &y) != EOF) {
x--, y--;
if (x != y && !g[x][y])
e[x].push_back(y), e[y].push_back(x);
g[x][y] = g[y][x] = true;
} // 增广匹配
for (int i = ; i < n; i++) match[i] = -;
for (int i = ; i < n; i++) if (match[i] == -) aug(i); // 输出答案
int tot = ;
for (int i = ; i < n; i++) if (match[i] != -) tot++;
printf("%d\n", tot);
for (int i = ; i < n; i++) if (match[i] > i)
printf("%d %d\n", i + , match[i] + );
return ;
}

URAL 1099 Work scheduling 一般图的最大匹配 带花树算法(模板)的更多相关文章

  1. URAL 1099 Work Scheduling (一般图最大匹配) 模板题【带花树】

    <题目链接> <转载于 >>>  > 题目大意: 给出n个士兵,再给出多组士兵之间两两可以匹配的关系.已知某个士兵最多只能与一个士兵匹配.求最多能够有多少对匹 ...

  2. HDOJ 4687 Boke and Tsukkomi 一般图最大匹配带花树+暴力

    一般图最大匹配带花树+暴力: 先算最大匹配 C1 在枚举每一条边,去掉和这条边两个端点有关的边.....再跑Edmonds得到匹配C2 假设C2+2==C1则这条边再某个最大匹配中 Boke and ...

  3. ZOJ 3316 Game 一般图最大匹配带花树

    一般图最大匹配带花树: 建图后,计算最大匹配数. 假设有一个联通块不是完美匹配,先手就能够走那个没被匹配到的点.后手不论怎么走,都必定走到一个被匹配的点上.先手就能够顺着这个交错路走下去,最后一定是后 ...

  4. 【模板】一般图最大匹配(带花树算法)/洛谷P6113

    题目链接 https://www.luogu.com.cn/problem/P6113 题目大意 给定一个 \(n\) 个点 \(m\) 条边的无向图,求该图的最大匹配. 题目解析 二分图最大匹配,一 ...

  5. URAL 1099. Work Scheduling (一般图匹配带花树)

    1099. Work Scheduling Time limit: 0.5 secondMemory limit: 64 MB There is certain amount of night gua ...

  6. 【learning】一般图最大匹配——带花树

    问题描述 ​ 对于一个图\(G(V,E)\),当点对集\(S\)满足任意\((u,v)\in S\),均有\(u,v\in V,(u,v)\in E\),且\(S\)中没有点重复出现,我们称\(S\) ...

  7. UOJ #79 一般图最大匹配 带花树

    http://uoj.ac/problem/79 一般图和二分图的区别就是有奇环,带花树是在匈牙利算法的基础上对奇环进行缩点操作,复杂度似乎是O(mn)和匈牙利一样. 具体操作是一个一个点做类似匈牙利 ...

  8. Ural1099 Work Scheduling 一般图的最大匹配

    Ural1099 给定无向图, 求最大匹配. 在寻找增广路的过程中,可能出现一个奇环,这时候把奇环收缩,成为一朵“花”,并在新图上继续增广. 为了记录匹配关系,需要在花中寻找路径,每一条增广路径都可以 ...

  9. Ural 1099 Work Scheduling

    http://acm.timus.ru/problem.aspx?space=1&num=1099 题意:有n个人,很多对合作关系,每个人只能和一个人合作,求最多能选出多少人. 一般图匹配 # ...

随机推荐

  1. 洛谷 P1784 数独

    题目描述 数独是根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行.每一列.每一个粗线宫内的数字均含1-9,不重复.每一道合格的数独谜题都有且仅有唯一答案,推理方法也以此为基础,任何无 ...

  2. [神经网络]一步一步使用Mobile-Net完成视觉识别(三)

    1.环境配置 2.数据集获取 3.训练集获取 4.训练 5.调用测试训练结果 6.代码讲解 本文是第三篇,获取tfboard训练集. 前面我们拿到了所有图片对应的标注信息的xml文件,现在我们需要先把 ...

  3. c#中的里氏转换和Java中强制类型转换在多态中的应用

    在c#中: 注意: 子类并没有继承父类的构造函数,而是会默认调用父类那个无参数的构造函数. 如果一个子类继承了一个父类,那么这个子类除了可以使用自己的成员外,还可以使用从父类那里继承过来的成员.但是父 ...

  4. Luogu [P3367] 模板 并查集

    [模板]并查集 题目详见:[[P3367][模板]并查集] (https://www.luogu.org/problemnew/show/P3367) 这是一道裸的并查集题目(要不然叫模板呢) 废话不 ...

  5. ReactiveCocoa入门-part1

    作为一个iOS开发者,你写的每一行代码几乎都是在响应某个事件,例如按钮的点击,收到网络消息,属性的变化(通过KVO)或者用户位置的变化(通过CoreLocation).但是这些事件都用不同的方式来处理 ...

  6. C++中有三种创建对象的方法

    #include <iostream> using namespace std; class A { private: int n; public: A(int m):n(m) { } ~ ...

  7. c++基本配置属性页

    怎么调试一个项目. 需要配置好环境. 在一个release版本的环境中,调试要用release-debug版本,一般不用debug版本. 配置类型一般不变.

  8. 【贪心 计数 倍增】bzoj4458: GTY的OJ

    倍增写挂调了半个晚上 Description 身为IOI金牌的gtyzs有自己的一个OJ,名曰GOJ.GOJ上的题目可谓是高质量而又经典,他在他的OJ里面定义了一个树形的分类目录,且两个相同级别的目录 ...

  9. sphinx关键字套红

    sphinx定义搜索结果,搜索的内容着重显示,可以使用下面代码 <?php /** * Created by PhpStorm. * User: pc00001 * Date: 2015/4/1 ...

  10. my购物车

    sum=0 a=input("请输入“水果”或“衣服”:") if a=="手机": while True: shop = { '蓝葡萄', '水蜜桃', '草 ...