C. Basic Diplomacy (贪心 + 暴力) (网络流匹配)

C. Basic Diplomacy

主要分析一下网络流

(贪心 + 暴力)代码

int cnt[maxn];
vector<int> v[maxn];

void solve()
{
	int n,m;
	cin>>n>>m;
	
	for(int i=1;i<=m;i++) v[i].clear();
	for(int i=1;i<=n;i++) cnt[i] = 0;
	
	int f = 1;
	for(int i=1;i<=m;i++)
	{
		int k; cin>>k;
		
		for(int j=1;j<=k;j++)
		{
			int x;
			cin>>x;
			v[i].push_back(x);
			
			if(k == 1)
			{
				cnt[x] ++;
				if(cnt[x] > (m/2 + m%2)) f = 0;
			}
		}
	}
	
	if(!f)
	{
		cout<<"NO\n";
		return;
	}
	
	cout<<"YES\n";
	for(int i=1;i<=m;i++)
	{
		if(v[i].size() == 1)
		{
			cout<<v[i][0]<<' ';		
			continue;
		}
		
		int mn = inf;
		int ans = -1;
		for(auto x : v[i])
			if(mn > cnt[x])
			{
				mn = cnt[x];
				ans = x;
			}
		
		cout<<ans<<' ';
		cnt[ans] ++;
	}
	
	cout<<'\n';
}

int main()
{
	IOS;
	
	int t;
//	ire(t);
	cin>>t;
//	t = 1;
	while(t--)
	{
		solve();
	}
	
 	return 0;
}

分析(网络流匹配)

这个题就是很经典的匹配问题;
把天数放在左边(编号1~m), 把朋友放右边(编号m+1~m+n);
天数向朋友连容量为1的边(因为一天只能匹配一个朋友);
建立源点S, 由S向每一天建容量为1的边;
建立汇点T, 每个朋友向汇点建立容量为(m/2 + m%2)的边;
因为每个朋友最多可以被配匹配(m/2 + m%2)次.

然后跑最大流, 如果跑出来ans < m, 说明有的天不能匹配到朋友, 此时答案就是NO;
否则就是YES, 然后我们再遍历每一天, 每一条边, 看一下哪条边是满流, 那么这个边的邻点(朋友)就是天数匹配到的点.

代码

#include<iostream>
#include<queue>
#include<cstring>
#include<vector>
#include<stdio.h>
#include<map>
#include<algorithm>
#include<deque>
#include<stack>
#include<set>
#include <random>
#include<bitset>
// #include <unordered_map>
#include<math.h>
#include<string.h>
#define IOS ios::sync_with_stdio(false),cin.tie(0);
using namespace std;
 
#define pb push_back
#define coutl cout<<"------------"<<endl;
#define fi first
#define se second

#define ire(x) scanf("%d",&x)
#define iire(a,b) scanf("%d %d",&a,&b)
#define lre(x) scanf("%lld",&x)
#define llre(a,b) scanf("%lld %lld",&a,&b)
#define dre(x) scanf("%lf",&x)
#define ddre(a,b) scanf("%lf %lf",&a,&b)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define endl "\n"
#define PI acos(-1.0)
//#define int long long
// #define double long double
// typedef __int128 ll;
typedef long long ll;
typedef unsigned long long ull;

typedef pair<int, int> PII;
typedef pair<double, int> PDI;
typedef pair<ll, ll> PLL;
typedef pair<double, double> PDD;
typedef pair<double, pair<int, double> > PDID;
typedef pair<char, char> PCC;
typedef pair<char, pair<int, int> > PCII;
typedef pair<int, pair<int, int> > PIII;
typedef pair<int, pair<int, pair<int, int> > > PIIII;
typedef pair<ll, pair<int, int> > PLII;

const int maxn = 1e5 + 7;
const int N = 3e5 + 7;
const int M = 1e6 + 7;
const int mod = 998244353;
const int inv = mod - mod/2;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1);
const double eps = 1e-8;
  
ll gcd(ll a,ll b) {return b==0 ? a : gcd(b,a%b);}
ll lcm(ll a,ll b) {return a*b / gcd(a,b);}
ll qmi(ll a,ll b,ll p) {ll ans = 1; while(b) { if(b & 1) ans = ans * a % p; a = a * a % p; b >>= 1; } return ans;}
int lowbit(int x) {return x & (-x);}

//试一下网络流

int n,m,S,T;
int h[N],e[M],ne[M],f[M],idx;   //其中f[]表示残留网络中正向边的容量
int d[N];       //记录一下每个点的层数
int cur[N];     //当前弧优化,记录一下当前点从哪条边开始搜

void add(int a,int b,int c) //(因为此时流网络中改变流量为0,因此残留网络中正向边容量为c,反向边为0)
{
    e[idx]=b; f[idx]=c; ne[idx]=h[a]; h[a]=idx++;   //正向边
    e[idx]=a; f[idx]=0; ne[idx]=h[b]; h[b]=idx++;   //反向边
}

bool bfs()  //建立分层图
{
    queue<int> q;  
    memset(d,-1,sizeof d);

    q.push(S); 
    d[S] = 0;       //起点的层数是0
    cur[S] = h[S];  //起点的当前弧是第一条

    while(q.size())
    {
        int t = q.front();
        q.pop();

        for(int i=h[t]; i!=-1; i=ne[i]) //枚举领边
        {
            int j = e[i];

            if(d[j] == -1 && f[i])    //该点没被更新过,且边的容量>0(可以走)
            {
                d[j] = d[t] + 1;
                cur[j] = h[j];      //该点的当前弧是其第一条边

                if(j == T) return true; //当前分层图下存在增广路径
                q.push(j);
            }
        }
    }
    return false;
}

int find(int u,int limit)   //从起点S到当前点u允许流过的最大流量为limit
{
    if(u == T) return limit;
    int flow = 0;       //从u开始向后面流的最大的流量

    for(int i=cur[u]; i!=-1 && flow < limit; i=ne[i])
    {
        cur[u] = i;     //当前弧优化!!!能搜到i边,表示u的邻接边i前面的边都被走过了(被更新过增广)
        int j = e[i];

        if(d[j] == d[u] + 1 && f[i])    //按照分层图的顺序去搜它
        {
            int t = find(j, min(f[i],limit-flow));

            if(!t) d[j] = -1;   // t==0 说明不能找到从j到T的增广路,因此下一次遍历到j时直接跳过即可

            f[i] -= t; f[i^1] += t; //这里的t就相当于是从j开始向后找的增广路径中边的最小值
            flow += t;
        }
    }
    return flow;
}

int dinic()
{
    int r = 0, flow;
    while(bfs())    //bfs建分层图
    {
        while(flow = find(S,inf)) r += flow;    //找到该图层上的所有增广路径,然后累加流量
    }
    return r;
}

void solve()
{
    memset(h, -1, sizeof h);
    idx = 0;
	
	cin>>n>>m;
    for(int i=1;i<=m;i++)
    {
    	int k; cin>>k;
    	
    	for(int j=1;j<=k;j++)
    	{
    		int x; cin>>x;
    		
    		add(i,x+m,1);	//天数向朋友建边
		}
	}
	
	S = 0, T = n + m + 1;
    for(int i=1;i<=m;i++) add(S,i,1);	//起点向天数
    for(int i=1;i<=n;i++) add(i+m,T,(m/2 + m%2));	//朋友向汇点建容量为(m/2 + m%2)的边
	
	int ans = dinic();
	if(ans < m)	cout<<"NO\n";//不是所有天都能匹配
	else
	{	
		cout<<"YES\n";
		
		for(int no=1;no<=m;no++)
		{
			for(int i=h[no];i!=-1;i=ne[i])
				if(f[i] == 0)
				{
					cout<<e[i]-m<<' ';
					break;
				}
		}
		cout<<'\n';
	}
}

int main()
{
	IOS;
	
	int t;
//	ire(t);
	cin>>t;
//	t = 1;
	while(t--)
	{
		solve();
	}
	
 	return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值