2022西安补题

(一)Problem - L - Codeforces (长链剖分)

        (1)题目大意

                给你一颗树,你要分成最少的集合,要求每一个集合中任意点对要么是父子关系,要么都不是父子关系。

         (2)解题思路

                都是父子关系,可以看成是一条链,显然这里可以很容易想到长链剖分,对于每一个答案来说,最大应该是长链中最长的那条,那么我们考虑选择前面长的,然后每次枚举这条链不作为父子关系,把后面每一条都抽出一个节点挂在这个上面的某一个节点。

        (3)代码实现

#include "bits/stdc++.h"
#define rep(i, z, n) for (int i = z; i <= n; i++)
#define per(i, n, z) for (int i = n; i >= z; i--)
#define ll long long
#define db double
#define PII pair<int, int>
#define fi first
#define se second
#define vi vector<int>
#define yes cout << "YES" << endl;
#define no cout << "NO" << endl;
using namespace std;
const int N = 2e6 + 10;
int dep[N], son[N];
vector<int> G[N], v;
void dfs1(int now, int d)
{
    for (auto x : G[now])
    {
        dfs1(x, d + 1);
        if (dep[x] > dep[son[now]])
            son[now] = x;
    }
    dep[now] = dep[son[now]] + 1;
}
void dfs2(int now, int d)
{
    if (!son[now])
    {
        v.push_back(d);
    }
    else
    {
        dfs2(son[now], d + 1);
        for (auto x : G[now])
        {
            if (x == son[now])
                continue;
            dfs2(x, 1);
        }
    }
}
void solve()
{
    int n, x;
    cin >> n;
    v.clear();
    for (int i = 1; i <= n; i++)
    {
        G[i].clear();
        dep[i] = son[i] = 0;
    }
    for (int i = 2; i <= n; i++)
    {
        cin >> x;
        G[x].push_back(i);
    }
    dfs1(1, 0);
    dfs2(1, 1);
    int ans = v.size();
    sort(v.rbegin(), v.rend());
    for (int i = 0; i < v.size(); i++)
    {
        ans = min(ans, v[i] + i);
    }
    cout << ans << endl;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int T = 1;
    cin >> T;
    while (T--)
        solve();
    return 0;
}

(二)Problem - B - Codeforces (网络流)
            (1)题目大意

          题意是让你选择一个k,然后你拥有k+1种颜色0-k,你要把矩阵中那些点用数字填起来,保证每行每列不能有相同的数字,除0外,价值为ck+dz z是0的个数,(c,d题目给的)你要最小化这个价值

        (2)解题思路

                我们考虑除去0之外的颜色k最大为多少,显然k最大为行或列最大的为空的数量,然后我们根据行列建边,我们定义一个源点和汇点,S,T,另S向每一行建立一条容量为0的管道,从列向T建立一条容量为0的管道,我们考虑每一个空地,那么这个空地的行列坐标存在一条容量为1的管道,因此我们枚举k,给每一条原点到行,列到汇点的边的容量+k,然后跑k遍最大流,那么花费就是k*c + d*(tot - dinic()),tot是总的空地个数。

        (3)代码实现

// Problem: B. Cells Coloring
// Contest: Codeforces - The 2022 ICPC Asia Xian Regional Contest
// URL: https://codeforces.com/gym/104077/problem/B
// Memory Limit: 512 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include "bits/stdc++.h"
#define rep(i, z, n) for (int i = z; i <= n; i++)
#define per(i, n, z) for (int i = n; i >= z; i--)
#define ll long long
#define db double
#define PII pair<int, int>
#define fi first
#define se second
#define vi vector<int>
#define yes cout << "YES" << endl;
#define no cout << "NO" << endl;
using namespace std;
const int N = 251;
char mp[N][N], up = 1;
int cur[N * 2 + 10], di[N * 2 + 10], he[N * 2 + 10], ccur[N * 2 + 10], idx;
struct Edge
{
    int nt, to, wi;
} e[N * N * 3];
int S, T;
void add(int u, int v, int w)
{
    e[idx] = {he[u], v, w};
    he[u] = idx++;

    e[idx] = {he[v], u, 0};
    he[v] = idx++;
}
bool bfs()
{
    queue<int> q;
    q.push(S);
    memset(di, -1, sizeof(di));
    di[S] = 0, cur[S] = he[S];
    while (q.size())
    {
        int v = q.front();
        q.pop();
        for (int i = he[v]; ~i; i = e[i].nt)
        {
            int j = e[i].to, w = e[i].wi;
            if (di[j] == -1 && w)
            {
                di[j] = di[v] + 1;
                cur[j] = he[j];
                if (j == T)
                    return true;
                q.push(j);
            }
        }
    }
    return false;
}
int dfs(int u, int limit)
{
    if (u == T)
        return limit;
    int mxFlow = 0;
    for (int i = cur[u]; ~i && mxFlow < limit; i = e[i].nt)
    {
        int j = e[i].to;
        cur[u] = i;
        if (di[j] == di[u] + 1 && e[i].wi)
        {
            int tmp = dfs(j, min(e[i].wi, limit - mxFlow));
            if (!tmp)
                di[j] = -1;
            mxFlow += tmp, e[i ^ 1].wi += tmp, e[i].wi -= tmp;
        }
    }
    return mxFlow;
}
int dinic()
{
    int mxFlow = 0, Flow = 0;
    while (bfs())
    {
        while (Flow = dfs(S, 1e9))
        {
            mxFlow += Flow;
        }
    }
    return mxFlow;
}
int n, m, c, d;
void solve()
{
    cin >> n >> m >> c >> d;
    S = 0, T = n + m + 1;
    memset(he, -1, sizeof(he));
    for (int i = 1; i <= n; i++)
        add(S, i, 0);
    for (int i = 1; i <= m; i++)
        add(i + n, T, 0);
    int tot = 0, mx = 0;
    for (int i = 1; i <= n; i++)
    {
        int cnt = 0;
        for (int j = 1; j <= m; j++)
        {
            cin >> mp[i][j];
            if (mp[i][j] == '.')
            {
                add(i, j + n, 1);
                tot++;
                cnt++;
            }
        }
        mx = max(mx, cnt);
    }
    for (int i = 1; i <= m; i++)
    {
        int cnt = 0;
        for (int j = 1; j <= n; j++)
        {
            if (mp[j][i] == '.')
            {
                cnt++;
            }
        }
        mx = max(mx, cnt);
    }
    ll ans = 1e18, res = 0;
    for (int i = 0; i <= mx; i++)
    {
        ll pt = dinic();
        res += pt;
        ans = min(ans, (ll)c * i + (ll)d * (tot - res));
        for (int j = 0; j < n + m; j++)
            e[j * 2].wi++;
    }
    cout << ans << endl;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int T = 1;
    // cin >> T;
    while (T--)
        solve();
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值