Sol

这做法我是想不到\(TAT\)

每个筐子拆成三个相互连边

球向三个筐子连边

然后跑一般图最大匹配

这三个筐子间最多有一个匹配

那么显然每个球一定会放在一个筐子里,一定有一个匹配

如果筐子间有匹配,则有一个半空的筐子,因为它一定只匹配了小于等于\(1\)个球

答案为匹配数\(-n\)

使答案最大即匹配数最大

上带花树就好了

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(1005);
const int __(2e5 + 5);
typedef int Arr[_]; IL int Input(){
RG int x = 0, z = 1; RG char c = getchar();
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x * z;
} Arr first, match, fa, vis, tim, pre;
int n, m, cnt, idx, ans, E, t1, t2, t3;
queue <int> Q;
struct Edge{
int to, next;
} edge[__]; IL void Add(RG int u, RG int v){
edge[cnt] = (Edge){v, first[u]}, first[u] = cnt++;
edge[cnt] = (Edge){u, first[v]}, first[v] = cnt++;
} IL int Find(RG int x){
return x == fa[x] ? x : fa[x] = Find(fa[x]);
} IL int LCA(RG int x, RG int y){
++idx, x = Find(x), y = Find(y);
while(tim[x] != idx){
tim[x] = idx;
x = Find(pre[match[x]]);
if(y) swap(x, y);
}
return x;
} IL void Blossom(RG int x, RG int y, RG int p){
while(Find(x) != p){
pre[x] = y, y = match[x];
if(vis[y] == 2) vis[y] = 1, Q.push(y);
if(Find(x) == x) fa[x] = p;
if(Find(y) == y) fa[y] = p;
x = pre[y];
}
} IL int Aug(RG int S){
for(RG int i = 1; i <= t3; ++i) vis[i] = pre[i] = 0, fa[i] = i;
while(!Q.empty()) Q.pop();
Q.push(S), vis[S] = 1;
while(!Q.empty()){
RG int u = Q.front(); Q.pop();
for(RG int e = first[u]; e != -1; e = edge[e].next){
RG int v = edge[e].to;
if(Find(v) == Find(u) || vis[v] == 2) continue;
if(!vis[v]){
vis[v] = 2, pre[v] = u;
if(!match[v]){
for(RG int x = v, lst; x; x = lst)
lst = match[pre[x]], match[pre[x]] = x, match[x] = pre[x];
return 1;
}
vis[match[v]] = 1, Q.push(match[v]);
}
else{
RG int p = LCA(u, v);
Blossom(u, v, p);
Blossom(v, u, p);
}
}
}
return 0;
} int main(RG int argc, RG char *argv[]){
for(RG int T = Input(); T; --T){
n = Input(), m = Input(), E = Input();
t1 = n + m, t2 = t1 + m, t3 = t2 + m;
ans = cnt = idx = 0;
for(RG int i = 1; i <= t3; ++i) first[i] = -1, match[i] = 0, tim[i] = 0;
for(RG int i = 1; i <= m; ++i)
Add(n + i, t1 + i), Add(t1 + i, t2 + i), Add(n + i, t2 + i);
for(RG int i = 1, u, v; i <= E; ++i){
u = Input(), v = Input();
Add(u, n + v), Add(u, t1 + v), Add(u, t2 + v);
}
for(RG int i = 1; i <= t3; ++i) if(!match[i]) ans += Aug(i);
printf("%d\n", ans - n);
}
return 0;
}

[WC2016]挑战NPC的更多相关文章

  1. [WC2016]挑战NPC(一般图最大匹配)

    [WC2016]挑战NPC(一般图最大匹配) Luogu 题解时间 思路十分有趣. 考虑一个筐只有不多于一个球才有1的贡献代表什么. 很明显等效于有至少两个位置没有被匹配时有1的贡献. 进而可以构造如 ...

  2. [BZOJ]4405: [wc2016]挑战NPC(带花树)

    带花树模板 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; ...

  3. [bzoj4405][wc2016]挑战NPC

    来自FallDream的博客,未经允许,请勿转载,谢谢. 小N最近在研究NP完全问题,小O看小N研究得热火朝天,便给他出了一道这样的题目: 有n个球,用整数1到n编号.还有m个筐子,用整数1到m编号. ...

  4. BZOJ 4405 [wc2016]挑战NPC 带花树 一般图最大匹配

    https://www.lydsy.com/JudgeOnline/problem.php?id=4405 这道题大概就是考场上想不出来,想出来也调不出来的题. 把每个桶拆成三个互相有边的点,每个球向 ...

  5. [UOJ171][WC2016]挑战NPC

    uoj luogu bzoj sol 你可以列一个表格. 一个框子里放球的数量 0 1 2 3 对"半空框子"数量的贡献 1 1 0 0 把一个框子拆三个点.两两之间连边. 会发现 ...

  6. bzoj 4405: [wc2016]挑战NPC【带花树】

    把每个筐子拆成3个,分别表示放0/1/2个,然后把这三个点两两连起来,每一个可以放在筐里的球都想这三个点连边. 这样可以发现,放0个球的时候,匹配数为1,放1个球的时候,匹配数为1,放2个球的时候,匹 ...

  7. 「WC2016」挑战NPC

    「WC2016」挑战NPC 解题思路 这个题建图非常厉害,带花树什么的只会口胡根本写不动,所以我写了机房某大佬教我的乱搞. 考虑把一个筐 \(x\) 拆成 \(x1,x2,x3\) 三个点,且这三个点 ...

  8. 【BZOJ4405】【WC2016】挑战NPC(带花树)

    [BZOJ4405][WC2016]挑战NPC(带花树) 题面 BZOJ 洛谷 Uoj Description 小N最近在研究NP完全问题,小O看小N研究得热火朝天,便给他出了一道这样的题目: 有n个 ...

  9. UOJ171 【WC2016】挑战NPC

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

随机推荐

  1. centos6 编译安装nodejs4.3

    官网下载源码包 https://nodejs.org/en/download/ source code #下载 wget https://nodejs.org/dist/v4.3.2/node-v4. ...

  2. iOS8 dismissViewControllerAnimated:YES在Show Segue不工作

    最近使用iOS8 Sdk(xcode6.1.1)使用Show Segue功能,V_A视图push到V_B视图,然后想通过使用按键返回V_A,我记得ios6的时候可以使用[self dismissVie ...

  3. linux命令行添加图形化界面

    安装一个图形化的包即可!! yum update grub2-common yum install fwupdate-efi yum groupinstall "GNOME Desktop& ...

  4. 最近总想着写一个模拟alert和confirm插件,代替原生的

    msgbox-confirm github:  https://github.com/tong-mikasa/msgbox-confirm 主要js代码,scss代码 (function($) { $ ...

  5. 递归迭代vector三种方法实现二路归并排序

    https://mp.csdn.net/mdeditor/84933084# 附链接

  6. Jquery ajax, Axios, Fetch区别

    1.   Jquery ajax, Axios, Fetch区别之我见 2.   ajax.axios.fetch之间的详细区别以及优缺点

  7. VirtualBox 命令行操作

    vboxmanage list vmsvboxmanage list runningvmsvboxmanage startvmvboxmanage controlvm "RHEL6.1_fo ...

  8. SeaJS结合javascript面向对象使用笔记(一)

    Sea.JS Seajs结合javascript面向对象 所需页面 /app/index.html /lib/factory.js /lib/sea.js /lib/constructor.js /j ...

  9. C++ 隐含的this 指针

    c++primer   页数:376-379 备份, 很有嚼头 #include <iostream> #include <string> #include <fstre ...

  10. [转]OData/WebApi

    本文转自:https://github.com/OData/WebApi/tree/vNext OData Web API Introduction OData Web API (i.e., ASP. ...