BZOJ 5335 智力竞赛

大致题意:给出一个DAG,问能否用n+1条可重复路径覆盖整个图。

最小有重复路径覆盖问题,先传递闭包,转化成无重复路径覆盖问题。
然后把原图每个点拆成两个点建立二分图,然后用原图点数 − - 最大匹配数就是答案。
如果可以覆盖就输出 A K AK AK,否则二分一个最大可行权值 m i d mid mid,大于 m i d mid mid的点连一个 i − > i i -> i i>i的边,表示忽略这个点,然后照常建图即可。

#define others
#ifdef poj
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#endif // poj
#ifdef others
#include <bits/stdc++.h>
#include <ext/rope>
#include <ext/pb_ds/priority_queue.hpp>
#endif // others
//#define file
#define all(x) x.begin(), x.end()
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define eps 1e-8
const double pi = acos(-1.0);


typedef long long LL;
typedef long long DLL;
typedef unsigned long long ULL;
void umax(LL &a, LL b) {
    a = max(a, b);
}
void umin(LL &a, LL b) {
    a = min(a, b);
}
int dcmp(double x) {
    return fabs(x) <= eps?0:(x > 0?1:-1);
}
void file() {
    freopen("data_in.txt", "r", stdin);
    freopen("data_out.txt", "w", stdout);
}

DLL mod = 1e9;

DLL Pow(DLL a,DLL b) {
    DLL res=1;
    a%=mod;
    for(; b; b>>=1) {
        if(b&1)res=res*a%mod;
        a=a*a%mod;
    }
    return res;
}
//
//void print(DLL x) {
//    if(x < 0) {
//        x = -x;
//        putchar('-');
//    }
//    if(x > 9) print(x/10);
//    putchar(x%10 + '0');
//}
//#define iostart
//#define iostart
#define pb(x) push_back(x)
namespace solver {
    const int maxn = 510;
    int n, m;
    int g[maxn][maxn];
    vector<int> G[maxn];
    int link[maxn], vis[maxn];
    int v[maxn];
    bool dfs(int u) {
        for(int i = 0; i < G[u].size(); i++) {
            int v = G[u][i];
            if(!vis[v]) {
                vis[v] = 1;
                if(link[v] == -1 || dfs(link[v])) {
                    link[v] = u;
                    return 1;
                }
            }
        }
        return 0;
    }
    int gao() {
        int ans = 0;
        memset(link, -1, sizeof link);
        for(int i = 1; i <= m; i++) {
            memset(vis, 0, sizeof vis);
            if(dfs(i))
                ans++;
        }
        return ans;
    }
    bool check_val(int x) {
        for (int i = 1; i <= m; i++) {
            G[i].clear();
        }
        for (int i = 1; i <= m; i++){
            if (v[i] > x) {
                G[i].push_back(i);
            }
            for (int j = 1; j <= m; j++){
                if (g[i][j]) {
                    G[i].push_back(j);
//                    cout << i << " xx " << j << endl;
                }
            }
        }
        int match = gao();
//        cout << x << " " << m - match << endl;
        return m - match <= n + 1;
    }
    vector<int> hash_val;
    void solve() {
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= m; i++){
            int val, k;
            scanf("%d%d", &val, &k);
            v[i] = val;
            hash_val.push_back(val);
            while (k--) {
                int to;
                scanf("%d", &to);
                g[i][to] = 1;
            }
        }
        for (int k = 1; k <= m; k++) {
            for (int i = 1; i <= m; i++) {
                for (int j = 1; j <= m; j++) {
                    if (g[i][k] && g[k][j]) {
                        g[i][j] = 1;
                    }
                }
            }
        }
        sort(all(hash_val));
        hash_val.erase(unique(all(hash_val)), hash_val.end());
        int L = 0, R = (int)hash_val.size() - 2, M;
        while (L + 4 < R) {
            M = L + R >> 1;
            if (check_val(hash_val[M])) {
                L = M;
            } else {
                R = M;
            }
        }
        for (; L < R; L++) {
            if (!check_val(hash_val[L]))
                break;
        }
        if (check_val(hash_val[L])) {
            puts("AK");
        } else {
            printf("%d\n", hash_val[L]);
        }
    }
}

int main() {
#ifdef iostart
    ios::sync_with_stdio(0);
    cin.tie(0);
#endif // iostart
//    file();
    solver::solve();
    return 0;
}

由于传递闭包之后是一个稠密图,所以可以用bitset去优化匈牙利算法。

#define others
#ifdef poj
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#endif // poj
#ifdef others
#include <bits/stdc++.h>
#include <ext/rope>
#include <ext/pb_ds/priority_queue.hpp>
#endif // others
//#define file
#define all(x) x.begin(), x.end()
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define eps 1e-8
const double pi = acos(-1.0);


typedef long long LL;
typedef long long DLL;
typedef unsigned long long ULL;
void umax(LL &a, LL b) {
    a = max(a, b);
}
void umin(LL &a, LL b) {
    a = min(a, b);
}
int dcmp(double x) {
    return fabs(x) <= eps?0:(x > 0?1:-1);
}
void file() {
    freopen("data_in.txt", "r", stdin);
    freopen("data_out.txt", "w", stdout);
}

DLL mod = 1e9;

DLL Pow(DLL a,DLL b) {
    DLL res=1;
    a%=mod;
    for(; b; b>>=1) {
        if(b&1)res=res*a%mod;
        a=a*a%mod;
    }
    return res;
}
//
//void print(DLL x) {
//    if(x < 0) {
//        x = -x;
//        putchar('-');
//    }
//    if(x > 9) print(x/10);
//    putchar(x%10 + '0');
//}
//#define iostart
//#define iostart
#define pb(x) push_back(x)
namespace solver {
    const int maxn = 510;
    int n, m;
    int g[maxn][maxn];
    int a[maxn][maxn/32+1], b[maxn][maxn/32+1];
    int link[maxn], vis[maxn];
    int q[maxn];

    typedef int U;
    int tot = m >> 5;
    inline void set1(U v[],int x){v[x>>5]|=1U<<(x&31);}
    inline void flip(U v[],int x){v[x>>5]^=1U<<(x&31);}
    bool dfs(int u) {
        for (int i = 0; i <= tot; i++) {
            for (;;) {
                U o = b[u][i]&vis[i];
                if (!o) break;
                int y = i<<5|__builtin_ctz(o);
                flip(vis, y);
                if (!link[y] || dfs(link[y])) return link[y]=u, 1;
            }
        }
        return 0;
    }
    int gao() {
        for (int i = 1; i <= m; i++) link[i] = 0;
        int ans = 0;
        for(int i = 1; i <= m; i++) {
            for (int j = 1; j <= m; j++) set1(vis, j);
            if(dfs(i)) ans++;
        }
        return ans;
    }
    bool check_val(int x) {
        for (int i = 1; i <= m; i++){
            for (int j = 0; j <= tot; j++) {
                b[i][j] = a[i][j];
            }
        }
        for (int i = 1; i <= m; i++) {
            if (q[i] > x) {
                set1(b[i], i);
            }
        }
        for (int i = 1; i <= m; i++) link[i] = 0;
        int match = gao();
        return m - match <= n + 1;
    }
    vector<int> hash_val;
    void solve() {
        scanf("%d%d", &n, &m);
        tot = m >> 5;
        for (int i = 1; i <= m; i++){
            int val, k;
            scanf("%d%d", &val, &k);
            q[i] = val;
            hash_val.push_back(val);
            while (k--) {
                int to;
                scanf("%d", &to);
                g[i][to] = 1;
            }
        }
        for (int k = 1; k <= m; k++) {
            for (int i = 1; i <= m; i++) {
                for (int j = 1; j <= m; j++) {
                    if (g[i][k] && g[k][j]) {
                        g[i][j] = 1;
                    }
                }
            }
        }
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= m; j++) {
                if (g[i][j])
                    set1(a[i], j);
            }
        }
        sort(all(hash_val));
        hash_val.erase(unique(all(hash_val)), hash_val.end());
        int L = 0, R = (int)hash_val.size() - 2, M;
        while (L + 4 < R) {
            M = L + R >> 1;
            if (check_val(hash_val[M])) {
                L = M;
            } else {
                R = M;
            }
        }
        for (; L < R; L++) {
            if (!check_val(hash_val[L]))
                break;
        }
        if (check_val(hash_val[L])) {
            puts("AK");
        } else {
            printf("%d\n", hash_val[L]);
        }
    }
}

int main() {
#ifdef iostart
    ios::sync_with_stdio(0);
    cin.tie(0);
#endif // iostart
//    file();
    solver::solve();
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: bzoj作为一个计算机竞赛的在线评测系统,不仅可以提供大量的题目供程序员练习和学习,还可以帮助程序员提升算法和编程能力。为了更好地利用bzoj进行题目的学习和刷题,制定一个bzoj做题计划是非常有必要的。 首先,我们需要合理安排时间,每天留出一定的时间来做bzoj的题目。可以根据自己的时间安排,每天挑选适量的题目进行解答。可以先从难度较低的题目开始,逐渐提高难度,这样既能巩固基础知识,又能挑战自己的思维能力。 其次,要有一个计划和目标。可以规划一个每周或每月的题目数量目标,以及每个阶段要学习和掌握的算法知识点。可以根据bzoj的题目分类,如动态规划、图论、贪心算法等,结合自己的实际情况,有针对性地选择题目进行学习。 此外,要充分利用bzoj提供的资源。bzoj网站上有很多高质量的题解和优秀的解题代码,可以参考和学习。还有相关的讨论区,可以与其他程序员交流和讨论,共同进步。 最后,要坚持并保持思考。做题不是单纯为了刷数量,更重要的是学会思考和总结。遇到难题时,要有耐心,多思考,多尝试不同的解法。即使不能一次性解出来,也要学会思考和分析解题过程,以及可能出现的错误和优化。 总之,bzoj做题计划的关键在于合理安排时间、制定目标、利用资源、坚持思考。通过有计划的刷题,可以提高算法和编程能力,并培养解决问题的思维习惯,在计算机竞赛中取得更好的成绩。 ### 回答2: bzoj做题计划是指在bzoj这个在线测评系统上制定一套学习和刷题的计划,并且将计划记录在excel表格中。该计划主要包括以下几个方面的内容。 首先是学习目标的设定。通过分析自己的水平和知识缺口,可以设定一个合理的目标,比如每天解决一定数量的题目或者提高特定的算法掌握程度。 其次是题目选择的策略。在excel表格中可以记录下自己选择的题目编号、题目类型和难度等信息。可以根据题目的类型和难度来安排每天的刷题计划,确保自己可以逐步提高技巧和解题能力。 然后是学习进度的记录和管理。将每天的完成情况记录在excel表格中,可以清晰地看到自己的学习进度和任务完成情况。可以使用图表等功能来对学习进度进行可视化展示,更好地管理自己的学习计划。 同时,可以在excel表格的备注栏中记录下每道题目的解题思路、关键点和需要复习的知识点等信息。这样可以方便自己回顾和总结,巩固所学的知识。 最后,可以将excel表格与其他相关资料进行整合,比如算法教材、题目解析和学习笔记等。这样可以形成一个完整的学习档案,方便自己进行系统的学习和复习。 总之,bzoj做题计划excel的制定和记录可以帮助我们更加有条理和高效地进行学习和刷题。通过合理安排学习目标和题目选择策略,记录学习进度和思路,并整合其他学习资料,我们可以提高自己的解题能力,并在bzoj上取得更好的成绩。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值