3 3 2 B 1 2 B 2 3 R 3 1 2 1 1 R 1 2 0 0 0
1 0
设红边为2,蓝边为1,求MST得最多蓝边数
同理得最少蓝边数,观察k是否在区间中
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (3*2000+10)
#define MAXM (10000000+10)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
class bingchaji
{
public:
int father[MAXN],n;
void mem(int _n)
{
n=_n;
For(i,n) father[i]=i;
}
int getfather(int x)
{
if (father[x]==x) return x;
return father[x]=getfather(father[x]);
}
void unite(int x,int y)
{
father[x]=getfather(father[x]);
father[y]=getfather(father[y]);
father[father[x]]=father[father[y]];
}
bool same(int x,int y)
{
return getfather(x)==getfather(y);
}
}S;
int n,m,k;
struct edge
{
int a,b,c;
edge(){}
edge(int _x,int _y,int _c):a(_x),b(_y),c(_c){}
friend bool operator<(edge a,edge b){return a.c<b.c;}
}e[MAXM];
int kr()
{
S.mem(n);
sort(e+1,e+1+m);
int ans1=0,t=0;
For(i,m) if (!S.same(e[i].a,e[i].b))
{
S.unite(e[i].a,e[i].b),ans1+=e[i].c,++t;
// cout<<e[i].a<<' '<<e[i].b<<' '<<e[i].c<<endl;
}
if (t<n-1) return -1;
return ans1;
}
int main()
{
// freopen("g.in","r",stdin);
// freopen(".out","w",stdout);
while(cin>>n>>m>>k)
{
if (n+m+k==0) return 0;
S.mem(n);
For(i,m)
{
char c;int a,b,t;
scanf("\n%c %d %d",&c,&a,&b);
if (c=='B') t=1;else t=2;
e[i]=edge(a,b,t);
}
int t1=kr();
For(i,m) e[i].c=3-e[i].c;
int t2=kr();
// cout<<t1<<' '<<t2<<endl;
if (t1!=-1)
{
t1=t1-(n-1);
t1=n-1-t1;
t2=t2-(n-1);
// cout<<t1<<' '<<t2<<endl;
if (t1>=k&&t2<=k)
{
cout<<"1\n";
continue;
}
}
cout<<"0\n";
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
HDU 4263(Red/Blue Spanning Tree-取边贪心)
原文:http://blog.csdn.net/nike0good/article/details/47448389