hdu5740
考验代码能力的题目,感觉网络流一要求输出方案我就写的丑
http://www.cnblogs.com/duoxiao/p/5777632.html 官方题解写的很详细
因为如果一个点染色确定后,整个图的染色也就确定了;
对于两个点u和v, 令它们之间的最短路是dis(u,v), 那么交换它们两个颜色的最少步数是dis(u,v), 且存在一种交换序列不会破坏其它节点的颜色
这是两个突破口
我的一个WA的地方在于:构造方案时没有把路径上的颜色交换……
#include<bits/stdc++.h>
#define mp make_pair
#define pi pair<int,int>
using namespace std;
const int inf=1e9+;
struct node{int po,next,flow,cost;} e[];
pi a[];
vector<pi> tmp;
vector<int> g[],s[];
int f[][],from[][],q[],pre[],cur[],d[],p[],mark[],cl[],rc[];
bool v[];
int n,m,len,t,ans,s1,s0;
void add(int x,int y,int f,int c)
{
e[++len].po=y;
e[len].flow=f;
e[len].cost=c;
e[len].next=p[x];
p[x]=len;
} void build(int x,int y, int f, int c)
{
add(x,y,f,c);
add(y,x,,-c);
} bool spfa()
{
int f=,r=;
for (int i=; i<=t; i++) d[i]=inf;
memset(v,,sizeof(v));
d[]=; q[]=;
while (f<=r)
{
int x=q[f++];
v[x]=;
for (int i=p[x]; i!=-; i=e[i].next)
{
int y=e[i].po;
if (e[i].flow&&d[x]+e[i].cost<d[y])
{
d[y]=d[x]+e[i].cost;
pre[y]=x; cur[y]=i;
if (!v[y])
{
q[++r]=y;
v[y]=;
}
}
}
}
return d[t]<inf;
} int mincost()
{
int s=;
while (spfa())
{
s+=d[t];
for (int i=t; i; i=pre[i])
{
int j=cur[i];
e[j].flow--;
e[j^].flow++;
}
}
return s;
} void bfs(int st)
{
memset(v,,sizeof(v));
for (int i=; i<=n; i++) f[st][i]=inf;
int h=,r=; q[]=st; v[st]=; f[st][st]=;
while (h<=r)
{
int x=q[h++];
for (int i=; i<g[x].size(); i++)
{
int y=g[x][i];
if (!v[y])
{
f[st][y]=f[st][x]+;
from[st][y]=x;
v[y]=;
q[++r]=y;
}
}
}
} bool get(int x,int w)
{
mark[x]=w; s[w].push_back(x);
if (rc[x]==) s1++; else s0++;
for (int i=; i<g[x].size(); i++)
{
int y=g[x][i];
if (mark[y]==-) get(y,w^);
else if (mark[y]==mark[x]) return ;
}
return ;
} void path(int st,int en,int w)
{
int x=en,r=; q[]=en;
while (x!=st)
{
x=from[st][x];
q[++r]=x;
if (cl[x]^cl[q[]])
{
for (int i=r; i>; i--)
{
tmp.push_back(mp(q[i],q[i-]));
swap(cl[q[i]],cl[q[i-]]);
}
r=; q[]=x;
}
}
} void way(int w)
{
for (int i=; i<=n; i++) cl[i]=rc[i];
for (int i=; i<=len; i+=)
{
int y=e[i].po,x=e[i^].po;
if (x&&y<t&&e[i].flow==) path(x,y,w);
}
} void graph(int w)
{
len=-; memset(p,,sizeof(p));
for (int i=; i<s[].size(); i++)
{
int x=s[][i];
if (rc[x]^w) build(,x,,);
}
for (int i=; i<s[].size(); i++)
{
int x=s[][i];
if (rc[x]^w) continue;
build(x,t,,);
for (int j=; j<s[].size(); j++)
{
int y=s[][j];
if (rc[y]^w) build(y,x,,f[y][x]);
}
}
} bool check(int x)
{
s1=s0=;
s[].clear(); s[].clear();
if (!get(x,)) return ;
if (s1+s0==) return ;
if (s0!=s[].size()&&s0!=s[].size()) return ;
int ans1=inf,ans2=inf;
if (s0==s[].size())
{
graph(); ans1=mincost();
tmp.clear(); way();
}
if (s1==s[].size())
{
graph();
ans2=mincost();
}
if (ans1==inf&&ans2==inf) return ;
if (ans2<ans1)
{
tmp.clear(); way();
for (int i=; i<tmp.size(); i++)
a[++ans]=tmp[i];
}
else {
for (int i=; i<tmp.size(); i++)
a[++ans]=tmp[i];
}
return ;
} int main()
{
int cas;
scanf("%d",&cas);
while (cas--)
{
scanf("%d%d\n",&n,&m);
for (int i=; i<=n; i++) g[i].clear();
for (int i=; i<=n; i++)
{
char ch=getchar();
rc[i]=ch-'';
}
for (int i=; i<=m; i++)
{
int x,y;
scanf("%d%d",&x,&y);
g[x].push_back(y);
g[y].push_back(x);
}
for (int i=; i<=n; i++) bfs(i);
memset(mark,,sizeof(mark));
bool ff=; ans=; t=n+;
for (int i=; i<=n; i++)
if (mark[i]==-)
{
ff&=check(i);
if (!ff) break;
} if (!ff) puts("-1");
else {
printf("%d\n",ans);
for (int i=; i<=ans; i++)
printf("%d %d\n",a[i].first,a[i].second);
}
}
}
hdu5740的更多相关文章
- HDU5740 Glorious Brilliance【最短路 KM匹配】
HDU5740 Glorious Brilliance 题意: 给出一张不一定合法的染色图,每次可以交换相邻两点的颜色,问最少多少次能使染色图合法 合法的染色图相邻点的颜色不能相同 题解: 首先要确定 ...
随机推荐
- Mobile phones POJ - 1195 二维树状数组求和
Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows ...
- [ldap]ldap相关问题
背景: ldap数据库要同步,按照如下操作步骤: 1.导出: 使用slapcat,slapcat直接对数据库操作, slapcat 2.将所需的条目取出,生成文件in.ldif 3.在目标机器上导入: ...
- python读书笔记-django架站过程总结(from the django book)
django架站过程总结:1.django-admin startproject store2.store这个project的目录下有:__init__,manage,setting,urls3.se ...
- 35 个你也许不知道的 Google 开源项目
转载自:http://blog.csdn.net/cnbird2008/article/details/18953113 Google是支持开源运动的最大公司之一,它们现在总共发布有超过500个的开源 ...
- Android之简易音乐播发器
布局主要代码之ListView: <span style="font-size:14px;"> <ListView android:id="@+id/m ...
- vs 统计有效代码行数
1.Visual Studio中,crtl+Shift+F,输入b*[^:b#/]+.*$ ,查找范围:选择整个解决方案,查找选项:使用正则表达式,文件类型:*.cs;*.cshtml 选择查找全部
- 【bzoj3376-方块游戏】带权并查集
题意: n块积木,m个操作或询问.每次移动积木的时候,约翰会选择两块积木X,Y,把X搬到Y的上方.如果X已经和其它积木叠在一起了,那么应将这叠积木整体移动到Y的上方:如果Y已经和其它积木叠在一起了的, ...
- UIView显示时遮挡导航栏的方法
[self.navigationController.view:addSubview];
- 暑假集训——cf热身赛部分题有感加其题解
刚刚开始集训,集训队队长暂时还没有拉专题,而是拉了部分codeforces上过题人数在2000左右的题组成了一场热身赛(其实就是一场练习),花了一天时间终于把它刷完了,其中很多题让我学到了很多骚操作, ...
- node起本地服务器以及实现代理,前端接口转发
上一篇文章写了使用docker来做nginx镜像实现本地的页面代理以及接口转发,但是需要下载docker,这个对于很多人来说还是显得比较麻烦,于是这个文章就是介绍如何只用node就可以代理本地的页面和 ...