bzoj 3624 贪心+Kruscal(并查集)
2017-03-13 18:48
417 查看
题意:n个点,m条无向边。共有两类边,分别是0类边和1类边,要求找出一棵最小生成树满足其中0类边恰好有K条,如果无解输出“no solution”
实际上就是一道经典的特殊的MST
跑两边Kruscal
第一遍:优先选择1类边,即在保证是树的前提下,能用1类边就用1类边,然后剩下的用0类边补全,则这些0类边是必须要选的,如果必选的0类边的数量大于K,则无解
第二遍:先加入上一次必须要选的0类边,然后在保证是树的前提下,在剩下的0类边里选取来补足K条,如果最后选择的0类边总数依旧小于K条,则无解,否则用1类边来补全最小生成树
type
rec=record
x,y,c:longint;
end;
var
n,m,k,tt,tx,ty,t1:longint;
i :longint;
father :array[0..20010] of longint;
l :array[0..100010] of rec;
flag :array[0..100010] of boolean;
function get_father(x:longint):longint;
begin
if x=father[x] then exit(x);
father[x]:=get_father(father[x]);
exit(father[x]);
end;
begin
read(n,m,k);
for i:=1 to n do father[i]:=i;
for i:=1 to m do read(l[i].x,l[i].y,l[i].c);
tt:=0;
// use 1 as possible as you can
for i:=1 to m do
if l[i].c=1 then
begin
tx:=get_father(l[i].x);
ty:=get_Father(l[i].y);
if tx<>ty then
begin
father[tx]:=ty; inc(tt);
if tt=n-1 then break;
end;
end;
//find then necessary 0
for i:=1 to m do
if (l[i].c=0) and (tt<n-1) then
begin
tx:=get_father(l[i].x);
ty:=get_father(l[i].y);
if tx<>ty then
begin
inc(t1); inc(tt); father[tx]:=ty; flag[i]:=true;
if tt=n-1 then break;
end;
end;
//you have to use t1 0
if t1>k then
begin
writeln('no solution');exit;
end;
for i:=1 to n do father[i]:=i; tt:=0;
//use the necessary 0
for i:=1 to m do
if flag[i] then
begin
tx:=get_father(l[i].x);
ty:=get_father(l[i].y);
if tx<>ty then
begin
dec(k); inc(tt);
father[tx]:=ty;
if tt=n-1 then break;
end;
end;
//ensure use k 0
for i:=1 to m do
if (l[i].c=0) and not flag[i] and (tt<n-1) then
begin
tx:=get_father(l[i].x);
ty:=get_father(l[i].y);
if tx<>ty then
begin
dec(k); inc(tt);
father[tx]:=ty;
flag[i]:=true;
if (k=0) or (tt=n-1) then break;
end;
end;
//the number of 0 is less than k
if k>0 then
begin
writeln('no solution');exit;
end;
//find other 1 to make it complete
for i:=1 to m do
if (l[i].c=1) and (tt<n-1) then
begin
tx:=get_father(l[i].x);
ty:=get_father(l[i].y);
if tx<>ty then
begin
inc(tt);
father[tx]:=ty;
flag[i]:=true;
if tt=n-1 then break;
end;
end;
//
for i:=1 to m do if flag[i] then writeln(l[i].x,' ',l[i].y,' ',l[i].c);
end.——by Eirlys
实际上就是一道经典的特殊的MST
跑两边Kruscal
第一遍:优先选择1类边,即在保证是树的前提下,能用1类边就用1类边,然后剩下的用0类边补全,则这些0类边是必须要选的,如果必选的0类边的数量大于K,则无解
第二遍:先加入上一次必须要选的0类边,然后在保证是树的前提下,在剩下的0类边里选取来补足K条,如果最后选择的0类边总数依旧小于K条,则无解,否则用1类边来补全最小生成树
type
rec=record
x,y,c:longint;
end;
var
n,m,k,tt,tx,ty,t1:longint;
i :longint;
father :array[0..20010] of longint;
l :array[0..100010] of rec;
flag :array[0..100010] of boolean;
function get_father(x:longint):longint;
begin
if x=father[x] then exit(x);
father[x]:=get_father(father[x]);
exit(father[x]);
end;
begin
read(n,m,k);
for i:=1 to n do father[i]:=i;
for i:=1 to m do read(l[i].x,l[i].y,l[i].c);
tt:=0;
// use 1 as possible as you can
for i:=1 to m do
if l[i].c=1 then
begin
tx:=get_father(l[i].x);
ty:=get_Father(l[i].y);
if tx<>ty then
begin
father[tx]:=ty; inc(tt);
if tt=n-1 then break;
end;
end;
//find then necessary 0
for i:=1 to m do
if (l[i].c=0) and (tt<n-1) then
begin
tx:=get_father(l[i].x);
ty:=get_father(l[i].y);
if tx<>ty then
begin
inc(t1); inc(tt); father[tx]:=ty; flag[i]:=true;
if tt=n-1 then break;
end;
end;
//you have to use t1 0
if t1>k then
begin
writeln('no solution');exit;
end;
for i:=1 to n do father[i]:=i; tt:=0;
//use the necessary 0
for i:=1 to m do
if flag[i] then
begin
tx:=get_father(l[i].x);
ty:=get_father(l[i].y);
if tx<>ty then
begin
dec(k); inc(tt);
father[tx]:=ty;
if tt=n-1 then break;
end;
end;
//ensure use k 0
for i:=1 to m do
if (l[i].c=0) and not flag[i] and (tt<n-1) then
begin
tx:=get_father(l[i].x);
ty:=get_father(l[i].y);
if tx<>ty then
begin
dec(k); inc(tt);
father[tx]:=ty;
flag[i]:=true;
if (k=0) or (tt=n-1) then break;
end;
end;
//the number of 0 is less than k
if k>0 then
begin
writeln('no solution');exit;
end;
//find other 1 to make it complete
for i:=1 to m do
if (l[i].c=1) and (tt<n-1) then
begin
tx:=get_father(l[i].x);
ty:=get_father(l[i].y);
if tx<>ty then
begin
inc(tt);
father[tx]:=ty;
flag[i]:=true;
if tt=n-1 then break;
end;
end;
//
for i:=1 to m do if flag[i] then writeln(l[i].x,' ',l[i].y,' ',l[i].c);
end.——by Eirlys
相关文章推荐
- 【BZOJ3624】【APIO2008】免费道路(贪心+并查集)
- 【bzoj3624】[Apio2008]免费道路 贪心+并查集
- BZOJ 3624: [Apio2008]免费道路(贪心+并查集)
- [BZOJ3624][Apio2008]免费道路(贪心+并查集)
- bzoj1821 [JSOI2010]Group 部落划分(贪心+并查集)
- bzoj 1854 游戏 并查集+贪心 解题报告
- [BZOJ3624][Apio2008]免费道路(并查集)
- 【BzoJ 1601】【灌水】【最小生成树】【贪心决策】【并查集】
- BZOJ_1821_[JSOI2010]_部落划分_(贪心,并查集)
- bzoj 1104 贪心+并查集
- 【Kruskal+贪心思想】BZOJ3624-[Apio2008]免费道路
- bzoj3714 [PA2014]Kuglarz(贪心+并查集)
- 【并查集】【生成树】【APIO 2008】【bzoj 3624】免费道路
- [bzoj1854][Scoi2010]游戏【并查集】【贪心】
- BZOJ 1854: [Scoi2010]游戏 贪心+并查集
- 【bzoj1821】【jsoi2010】【部落划分】【贪心+并查集】
- 【kruscal】【最小生成树】【并查集扩展】bzoj3714 [PA2014]Kuglarz
- BZOJ 1050: [HAOI2006]旅行comf 贪心,并查集
- BZOJ1821 [JSOI2010]Group 部落划分 贪心+并查集
- 【BZOJ1854】游戏[SCOI2009](神奇贪心+并查集)