bzoj 3624 贪心+Kruscal(并查集)

25 篇文章 0 订阅
11 篇文章 0 订阅

题意: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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值