Codeforces 709 C Basic Diplomacy 贪心/网络流二分图匹配

3 篇文章 0 订阅

链接https://codeforces.com/contest/1484/problem/C

题意
有m天和n个朋友,给出每一天哪些朋友是空闲的,每天邀请一个朋友,不能有朋友的出现次数超过 m 2 \frac{m}{2} 2m,输出任一答案,如果没有答案输出NO。

思路一:贪心

优先选择空闲朋友少的天,对于每一天选择出现次数最少的朋友。

代码:

#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn = 100005;
vector<int> f[maxn];
int vis[maxn];
int ans[maxn];
 
int main()
{
    int t,n,m,k;
    cin>>t;
    while (t--)
    {
        pair<int,int> so[maxn]; 
        cin>>n>>m;
        int ff,flag = 1;
        memset(vis,0,sizeof(vis));
        memset(ans,0,sizeof(ans));
        for(int i = 1; i <= m; i++){
            f[i].clear();
            cin>>k;
            so[i].first = k;
            so[i].second = i;
            for(int j = 1;j <= k; j++){
                cin>>ff;
                f[i].push_back(ff);
            }
        }
        sort(so + 1,so + m + 1);
        // for(int i = 1;i <= m;i++)
        //     cout<<so[i].second<<endl;
        // cout<<"**"<<endl;
        int no = INF;
        int po = 0;
        for(int c = 1;c <= m ;c++){
            int i = so[c].second;
            if(f[i].size() == 1)
                po = f[i][0];
            else{
                no = INF;
                for(int j = 0;j < f[i].size(); j++){
                    int v = f[i][j];
                    if(vis[v] < no){
                        no = vis[v];
                        po = v;
                    }
                }
            }
            ans[i] = po;
            vis[po]++;
            if(vis[po] > (m + 1) / 2){
                flag = 0;
                break;
            }
        }
        if(flag){
            cout<<"YES"<<endl;
            for(int i = 1;i <= m - 1; i++)
                cout<<ans[i]<<" ";
            cout<<ans[m]<<endl;
        }
        else
            cout<<"NO"<<endl;
    }
    return 0;
}

思路二
可以把这个题目当做一个网络流来做。

将天数作为编号为1~m的点,朋友作为编号为m+1~m+n的点,再添加一个起始s点为n+m+1,终点t为s+1,转化为二分图匹配。

对于所有的起始点和天数点,连一条流量为1的边,对应的天数和朋友连一条流量为1的边,为了满足次数小于 m 2 \frac{m}{2} 2m,让所有朋友和终点的连边的流量为 m 2 \frac{m}{2} 2m

对这个图求他的最大匹配,如果匹配等于m说明有解,该天对应的答案就是两个点之间权值变为0的点。

一开始用的FF算法,结果超时了,FF的算法时间复杂度是 O ( F E ) O(FE) O(FE),不适用于流量太大的网络,换成了dinic

代码(dinic)

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5+5;
#define INF 0x3f3f3f3f
struct  edge
{
    int to,cap,rev;
    void add(int a,int b,int c){
        to = a,cap = b,rev = c;
    }
};

int level[maxn],iter[maxn],ans[maxn];
vector<edge> g[maxn];

void add_edge(int u,int v,int cap){
    edge e1,e2;
    e1.add(v,cap,g[v].size());
    g[u].push_back(e1);
    e2.add(u,0,g[u].size() - 1);
    g[v].push_back(e2);
}

void bfs(int s)
{
    memset(level,-1,sizeof(level));
    queue<int> q;
    level[s] = 0;
    q.push(s);
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        for(int i = 0;i < g[u].size(); i++){
            edge &e = g[u][i];
            if(e.cap > 0 && level[e.to] < 0){
                level[e.to] = level[u] + 1;
                q.push(e.to);
            }
        }
    }
    
}



int dfs(int u,int t,int f){
    if(u == t)
        return f;
    for(int &i = iter[u];i < g[u].size() ;i++){
        edge &e = g[u][i];
        if(e.cap > 0 && level[u] < level[e.to]){
            int d = dfs(e.to,t,min(f,e.cap));
            // cout<<d<<endl;
            if(d > 0){
                e.cap -= d;
                g[e.to][e.rev].cap += d;
                return d;
            }
        }
    }
    return 0;
}



int max_flow(int s,int t){
    int flow = 0;
    for(;;){
        bfs(s);
        if(level[t] < 0){
            return flow;
            cout<<"flow"<<flow<<endl;
        }
        memset(iter,0,sizeof(iter));
        int f;
        while(f = dfs(s,t,INF) > 0)
            flow += f;
    }
    return flow;
}


void init(int x)
{
    for(int i = 1;i <= x; i++)
        g[i].clear();
    // memset(ans,0,sizeof(ans));
}

int main()
{
    int T,n,m;
    cin>>T;
    while (T--)
    {
        int k,j,s,t;
        cin>>n>>m;
        s = n + m + 1,t = s + 1;
        init(t);
        for(int i = 1;i <= m; i++){
            cin>>k;
            while(k--){
                cin>>j;
                add_edge(i,m + j,1);
            }
            add_edge(s,i,1);
        }
        for(int i = 1; i <= n; i++)
            add_edge(m + i,t,(m + 1) / 2);
        int an = max_flow(s,t);
        // cout<<an<<endl;
        if(an == m){
            cout<<"YES"<<endl;
            for(int i = 1; i <= m; i++){
                for(int j = 0;j < g[i].size(); j++){
                    if(g[i][j].cap == 0 )
                        ans[i] = g[i][j].to - m ;
                }
            }
            for(int i = 1;i <= m; i++)
                cout<<ans[i] <<" ";
            cout<<endl;
        }
        else
            cout<<"NO"<<endl;;
    }
    
    return 0;
}

超时的FF也贴一下:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5+10;
#define INF 0x3f3f3f3f
struct  edge
{
    int to,cap,rev;
    void add(int a,int b,int c){
        to = a,cap = b,rev = c;
    }
};

vector<edge> g[maxn];
int vis[maxn],ans[maxn];
int T,n,m,k;

void add(int u,int v,int cap){
    edge e1,e2;
    e1.add(v,cap,g[v].size());
    g[u].push_back(e1);
    e2.add(u,0,g[u].size() - 1);
    g[v].push_back(e2);
}

int dfs(int u,int t,int f)
{
    if(u == t)
        return f;
    vis[u] = 1;
    for(int i = 0;i < g[u].size(); i++){
        edge &e = g[u][i];
        // cout<<e.to<<endl;
        if(!vis[e.to] && e.cap > 0){
            int d = dfs(e.to,t,min(f,e.cap));
            if(d > 0){
                e.cap -= d;
                g[e.to][e.rev].cap += d;
                // cout<<d<<endl;
                return d;   
            }
        }
    }
    // puts("0");
    return 0;
}

int max_flow(int s,int t)
{
    int res = 0;
    for(;;){
        memset(vis,0,sizeof(vis));
        int f = dfs(s,t,INF);
        if(f == 0) return res;
        res += f;
    }
}

void init(int x)
{
    for(int i = 1;i <= x; i++)
        g[i].clear();
    // memset(ans,0,sizeof(ans));
}

int main()
{
    ios::sync_with_stdio(false);
    cin>>T;
    while(T--){
        int f;
        cin>>n>>m;
        int s = n + m + 1,t = s + 1;
        // cout<<s<<t<<endl;
        init(t);
        for(int i = 1;i <= m; i++){
            cin>>k;
            for(int j = 1;j <= k; j++){
                cin>>f;
                add(i, f + m,1);
            }
             add(s,i,1);
        }
        for(int i = 1;i <= n; i++)
            add(m + i, t, (m + 1) / 2);
        int an = max_flow(s,t);
        // cout<<an<<endl;
        if(an == m){
            cout<<"YES"<<endl;
            for(int i = 1; i <= m; i++){
                for(int j = 0;j < g[i].size(); j++){
                    if(g[i][j].cap == 0 )
                        ans[i] = g[i][j].to - m ;
                }
            }
            for(int i = 1;i <= m; i++)
                cout<<ans[i] <<" ";
            cout<<endl;
        }
        else
             cout<<"NO"<<endl;

    }
}
The problem statement can be found at Codeforces website. Approach: Let's start by looking at some examples: - 1, 2, 3, 4, 5 → No moves needed. - 2, 1, 3, 5, 4 → One move needed: swap index 1 and 2. - 5, 4, 3, 2, 1 → Two moves needed: swap index 1 and 5, then swap index 2 and 4. We can observe that in order to minimize the number of moves, we need to sort the array in non-descending order and keep track of the number of swaps we make. We can use bubble sort to sort the array and count the number of swaps. Let's see how bubble sort works: - Start from the first element, compare it with the second element, and swap them if the second element is smaller. - Move to the second element, compare it with the third element, and swap them if the third element is smaller. - Continue this process until the second-to-last element. At this point, the largest element is in the last position. - Repeat the above process for the remaining elements, but exclude the last position. In each iteration of the above process, we can count the number of swaps made. Therefore, the total number of swaps needed to sort the array can be obtained by summing up the number of swaps made in each iteration. Implementation: We can implement the above approach using a simple bubble sort algorithm. Here's the code: - First, we read the input array and store it in a vector. - We define a variable to keep track of the total number of swaps made and set it to 0. - We run a loop from the first element to the second-to-last element. - In each iteration of the above loop, we run another loop from the first element to the second-to-last element minus the current iteration index. - In each iteration of the inner loop, we compare the current element with the next element and swap them if the next element is smaller. - If a swap is made, we increment the total number of swaps made. - Finally, we output the total number of swaps made. Time Complexity: The time complexity of bubble sort is O(n^2). Therefore, the overall time complexity of the solution is O(n^2). Space Complexity: We are using a vector to store the input array. Therefore, the space complexity of the solution is O(n). Let's see the implementation of the solution.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值