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 题意: 给出一张不一定合法的染色图,每次可以交换相邻两点的颜色,问最少多少次能使染色图合法 合法的染色图相邻点的颜色不能相同 题解: 首先要确定 ...
随机推荐
- [POI2007] ZAP-Queries (莫比乌斯反演)
[POI2007] ZAP-Queries 题目描述 Byteasar the Cryptographer works on breaking the code of BSA (Byteotian S ...
- 洛谷:P2292 [HNOI2004]L语言(DP+Trie树)
P2292 [HNOI2004]L语言 题目链接:https://www.luogu.org/problemnew/show/P2292 题目描述 标点符号的出现晚于文字的出现,所以以前的语言都是没有 ...
- Java重要知识点
1.Java中除了static方法和final方法之外,其它所有的方法都是动态绑定,如同C++的虚函数,但是我们不需要显示的声明. private方法本质上属于final方法(因此不能被子类访问). ...
- DOM动态操纵控件案例
点击登陆显示登陆框 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head ...
- RabbitMQ基础概念(消息、队列、交换机)
1.消息的确认 RabbitMQ需要对每一条发送的消息进行确认.消费者必须通过AMQP的basic.ack命令显式地向RabbitMQ发送一个确认,或者在订阅到队列的时候就将auto_ack参数设置为 ...
- 给定一个数字n,不用for循环实现输出数组 [1,2,3,4,...,n]
一.for循环方式实现输出[1, 2, 3, ..., n] var n = 5; function test(n){ var arr=[]; for( var i = 1; i <= n; i ...
- Golang向Templates 插入对象的值
Go对象可以插入到template中,然后把对象的值表现在template中,你可以一层层的分解这个对象,去找他的子字段,当前对象用'.'来表示,所以当当前对象是一个string的时候,你可以用{{. ...
- java collection (二)
1.集合的概念: (1)现实生活中:很多的事物凑在一起. (2)数学中的集合:具有共同属性的事物的总体. (3java 中的集合类: 是一种工具类,就像是容器,存储任意数量的具有共同属性的对象.(集合 ...
- SpringBoot Caused by: java.lang.NoClassDefFoundError: org/apache/tomcat/util/descriptor/tld/TldParser
最近尝试着用spring boot ,页面模版使用的jsp,在pom里配置了对jsp的支持: <dependency> <groupId>org.apache.tomcat.e ...
- 【BZOJ1272】Gate Of Babylon [Lucas][组合数][逆元]
Gate Of Babylon Time Limit: 10 Sec Memory Limit: 162 MB[Submit][Status][Discuss] Description Input ...