UOJ171 【WC2016】挑战NPC
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。
本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!
Description
Input
Output
对于每组数据,先输出一行,包含一个整数,表示半空的筐子最多有多少个。
Sample Input
4 3 6
1 1
2 1
2 2
3 2
3 3
4 3
Sample Output
HINT
对于所有数据,T≤5,1≤n≤3m。保证 1≤vi≤n,1≤ui≤m,且不会出现重复的条件。
//It is made by ljh2000
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
const int MAXN = 1211;
const int MAXM = 1000011;
int N,n,m,E,ecnt,first[MAXN],to[MAXM],next[MAXM],match[MAXN],pre[MAXN],vis[MAXN],Tim,dui[MAXM],head,tail,id[MAXN],Match,father[MAXN],ans;
inline void link(int x,int y){ next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y; next[++ecnt]=first[y]; first[y]=ecnt; to[ecnt]=x; }
inline int find(int x){ if(father[x]!=x) father[x]=find(father[x]); return father[x]; }
inline int getint(){
int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
} inline int lca(int x,int y){
Tim++;
while(vis[x]!=Tim) {
if(x){
x=find(x);
if(vis[x]==Tim) return x;
vis[x]=Tim;
if(match[x]!=0) x=find(pre[match[x]]);//!!!
else x=0;
}
swap(x,y);
}
return x;
} inline void change(int x,int y,int k){
while(find(x)!=k) {
pre[x]=y; int z=match[x];//!!!
if(id[z]==1) { id[z]=0; dui[++tail]=z; }
if(find(z)==z) father[z]=k;
if(find(x)==x) father[x]=k;
y=z; x=pre[y];
}
} inline bool bfs(int s){
for(int i=1;i<=N;i++) id[i]=-1,father[i]=i; head=tail=0;
dui[++tail]=s; id[s]=0; int u;
while(head<tail) {
head++; u=dui[head];
for(int i=first[u];i;i=next[i]) {
int v=to[i];
if(id[v]==-1) {
pre[v]=u; id[v]=1;
if(!match[v]) {
int last,t,now=v;
while(now!=0) {
t=pre[now]; last=match[t];
match[t]=now; match[now]=t;
now=last;
}
return true;
}
id[match[v]]=0; dui[++tail]=match[v];
}
else if(id[v]==0/*!!!*/ && find(u)!=find(v)) {
int g=lca(u,v);
change(u,v,g);
change(v,u,g);
}
}
}
return false;
} inline void work(){
int T=getint(); int x,y;
while(T--) {
n=getint(); m=getint(); E=getint(); ecnt=0; memset(first,0,sizeof(first));
for(int i=1;i<=E;i++) {
x=getint(); y=getint(); x+=3*m;
link(x,(y-1)*3+1);
link(x,(y-1)*3+2);
link(x,(y-1)*3+3);
}
for(int i=1;i<=m;i++) {//每个框内部连边
int base=(i-1)*3+1;
link(base,base+1);
link(base,base+2);
link(base+1,base+2);
}
N=n+3*m; memset(match,0,sizeof(match)); memset(pre,0,sizeof(pre));
Tim=0; memset(vis,0,sizeof(vis)); Match=0;//!!!
for(int i=N;i>=1;i--) if(!match[i] && bfs(i)) Match++;
ans=Match-n;
printf("%d\n",ans);
for(int i=3*m+1;i<=N;i++) printf("%d ",(match[i]-1)/3+1);
printf("\n");
}
} int main()
{
work();
return 0;
}
UOJ171 【WC2016】挑战NPC的更多相关文章
- [UOJ171][WC2016]挑战NPC
uoj luogu bzoj sol 你可以列一个表格. 一个框子里放球的数量 0 1 2 3 对"半空框子"数量的贡献 1 1 0 0 把一个框子拆三个点.两两之间连边. 会发现 ...
- [WC2016]挑战NPC(一般图最大匹配)
[WC2016]挑战NPC(一般图最大匹配) Luogu 题解时间 思路十分有趣. 考虑一个筐只有不多于一个球才有1的贡献代表什么. 很明显等效于有至少两个位置没有被匹配时有1的贡献. 进而可以构造如 ...
- [BZOJ]4405: [wc2016]挑战NPC(带花树)
带花树模板 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; ...
- [bzoj4405][wc2016]挑战NPC
来自FallDream的博客,未经允许,请勿转载,谢谢. 小N最近在研究NP完全问题,小O看小N研究得热火朝天,便给他出了一道这样的题目: 有n个球,用整数1到n编号.还有m个筐子,用整数1到m编号. ...
- BZOJ 4405 [wc2016]挑战NPC 带花树 一般图最大匹配
https://www.lydsy.com/JudgeOnline/problem.php?id=4405 这道题大概就是考场上想不出来,想出来也调不出来的题. 把每个桶拆成三个互相有边的点,每个球向 ...
- [WC2016]挑战NPC
Sol 这做法我是想不到\(TAT\) 每个筐子拆成三个相互连边 球向三个筐子连边 然后跑一般图最大匹配 这三个筐子间最多有一个匹配 那么显然每个球一定会放在一个筐子里,一定有一个匹配 如果筐子间有匹 ...
- bzoj 4405: [wc2016]挑战NPC【带花树】
把每个筐子拆成3个,分别表示放0/1/2个,然后把这三个点两两连起来,每一个可以放在筐里的球都想这三个点连边. 这样可以发现,放0个球的时候,匹配数为1,放1个球的时候,匹配数为1,放2个球的时候,匹 ...
- 「WC2016」挑战NPC
「WC2016」挑战NPC 解题思路 这个题建图非常厉害,带花树什么的只会口胡根本写不动,所以我写了机房某大佬教我的乱搞. 考虑把一个筐 \(x\) 拆成 \(x1,x2,x3\) 三个点,且这三个点 ...
- 【BZOJ4405】【WC2016】挑战NPC(带花树)
[BZOJ4405][WC2016]挑战NPC(带花树) 题面 BZOJ 洛谷 Uoj Description 小N最近在研究NP完全问题,小O看小N研究得热火朝天,便给他出了一道这样的题目: 有n个 ...
随机推荐
- Atitit.程序包装exe启动器 打包 发布 设计 -生成exe java
Atitit.程序包装exe启动器 打包 发布 设计 -生成exe java 1. 要实现的功能1 2. ahk是个好东东..能启动了...1 3. exe4j vs nativej1 4. 2 ...
- Python基础之迭代器
迭代器的优点: 1.可以使用for循环遍历: 2.可以节省内存空间: 3.可以有序的访问集合(set)数据结构内的元素. 迭代器的缺点: 只能向前,不能后退. 可迭代对象与不可迭代对象: 可迭代对象: ...
- CentOS yum 命令出现 [Errno 14] curl#6 - "Couldn't resolve host ..." 的解决方法
安装svn的时候,发现报错说一个地址无法訪问. # yum list | grep subversion http://opensource.wandisco.com/centos/7/svn-1.8 ...
- centos7 firefox 安装flash
在官网下载flash的tar包 https://get.adobe.com/flashplayer/?spm=a2h0j.8191423.movie_player.5~5~5~8~A 在下载tar包的 ...
- ubuntu openfire Server install
1.首先登录到ubuntu server.在安装openfire 服务器之前,先确保你的系统已经更新到最新.然后输入下面的命令,一行一行执行,最后安装可用的更新 sudo apt-get update ...
- WEBserver、应用程序server、HTTPserver差别
WEBserver.应用程序server.HTTPserver差别 WEBserver.应用程序server.HTTPserver有何差别?IIS.Apache.Tomcat.Weblogic.Web ...
- Spring Cloud 微服务二:API网关spring cloud zuul
前言:本章将继续上一章Spring Cloud微服务,本章主要内容是API 网关,相关代码将延续上一章,如需了解请参考:Spring Cloud 微服务一:Consul注册中心 Spring clou ...
- vue面试题,知识点汇总(有答案)
一. Vue核心小知识点 1.vue中 key 值的作用 key 的特殊属性主要用在 Vue的虚拟DOM算法,在新旧nodes对比时辨识VNodes.如果不使用key,Vue会使用一种最大限度减少动态 ...
- android--SDK Manager下载Connection to http://dl-ssl.google.com refused
错误 Failed to fetch URL https://dl-ssl.google.com/android/repository/repository-6.xml, reason: Connec ...
- python在windows下安装paramiko模块和安装pycrypto模块(3步搞定)(转)
Python中使用SSH需要用到OpenSSH,而OpenSSH依赖于paramiko模块,而paramiko模块又依赖于pycrypto模块,因此要在Python中使用SSH,我们需要先安装pycr ...