2020 年 “游族杯” 全国高校程序设计网络挑战赛


传送门

Solved A B C D E F G H I
8 / 9 O Ø Ø Ø Ø O - Ø O
  • O 在比赛中通过
  • Ø 赛后通过
  • ! 尝试了但是失败了
  • - 没有尝试

A. Amateur Chess Players

简单博弈,显然每次每个人只会选择一个。

Code
/*
 * Author:  heyuhhh
 * Created Time:  2020/5/23 13:16:27
 */
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
  #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  void err() { std::cout << std::endl; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
  template <template<typename...> class T, typename t, typename... A> 
  void err(const T <t> &arg, const A&... args) {
  for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
  #define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 1e5 + 5;

void run() {
    int n, m;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        string s; cin >> s;
    }
    cin >> m;
    for (int i = 1; i <= m; i++) {
        string s; cin >> s;
    }
    if (n <= m) cout << "Quber CC" << '\n';
    else cout << "Cuber QQ" << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    run();
    return 0;
}

B. Binary String

题意:
这是个交互题,现有一个长度为\(n,n\leq 1000\)\(01\)串。
现在可以最多询问\(1023\)次,每次询问一个长度为\(\displaystyle\lfloor\frac{n}{2}\rfloor+1\)\(01\)串,回答该\(01\)串是否为某个子串(不一定连续)。
最后要找到隐藏的串。

思路:
十分巧妙、精致的一道题。
主要在于长度为\(\displaystyle\lfloor\frac{n}{2}\rfloor+1\)的限制。我们先考虑没有这个限制怎么做。
首先求出\(0\)的个数,不妨\(0\)的个数小于\(1\)。然后我们考虑求出连续的\(1\)的个数,之后放\(0\),也就是以\(0\)作为界限,从前往后依次确定。注意到\(0\)个数是递增的,\(1\)个数也是递增的。我们以\(1\)的长度作为状态,那么每个长度至多会被遍历\(a_i\)次,并且\(\sum a_i-1=cnt_0\),所以总的询问次数为\(cnt_0+cnt_1=n\)次,那么我们通过\(n\)次询问即可找出原串。
关于询问次数的详细分析就是这个题目巧妙的第一点,\(a_i\)减去\(1\)是因为除开第一次遍历到此长度的那一次。那么其余时候重复地进行遍历都是因为\(0\),每个\(0\)最多也会重复遍历\(1\)次。
接下来把限制长度为\(\displaystyle \lfloor\frac{n}{2}\rfloor+1\)考虑进来。我们观察之前询问的串类似于\(\underbrace{111}_y\underbrace{111}_i\underbrace{000}_{m-x}\)这样的形式,如果这样长度大于\(k,k\)即是最大询问长度就不能这样询问。我们假设前面有\(x\)\(0\)\(y\)个1,也就是说\(y+i+m-x>k,m\)为串中\(0\)的个数。我们注意到\(n-k<k\)的,所以考虑转化一下询问的形式为:\(\underbrace{000..0}_x01111\),注意下两者长度的差别,第二种形式的长度为\(\displaystyle n-k+1\leq k\),那么也就是说此时这种情况是合法的。
因为我们要保证询问次数的合法性,所以先\(check\)第一种情况,第一种情况没产生结果时考虑第二种情况,但是中间的\(1\)的个数要不小于之前枚举的\(1\)的个数\(i\),不然会重复枚举!也就是说第二种情况形式是这样的:\(\underbrace{000..0}_x\underbrace{111}_i0\underbrace{111..1}_{n-m-y-i}\),之后接着上面的询问来就行。
可能还有点细节,但我感觉我说得听清楚了= =
反正就是非常巧妙,上面的每一段落都是很巧妙的一个思想。
细节见代码:

Code
/*
 * Author:  heyuhhh
 * Created Time:  2020/5/24 18:33:37
 */
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
  #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  void err() { std::cout << std::endl; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
  template <template<typename...> class T, typename t, typename... A> 
  void err(const T <t> &arg, const A&... args) {
  for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
  #define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head

int qry(string s) {
    cout << "? " << s << endl;
    int x; cin >> x;
    return x;   
}
int f;
int query(string s) {
    for (auto& it : s) it ^= f;
    return qry(s);
}

void run() {
    int n; cin >> n;
    int k = n / 2 + 1;
    f = query(string(k, '0'));
    int l = 1, r = k + 1, mid;
    while (l < r) {
        mid = (l + r) >> 1;
        if (query(string(mid, '0'))) l = mid + 1;
        else r = mid;
    }
    int m = l - 1;
    auto solve = [&] (int x, int y) {
        for (int i = y + 1; i + m - x <= k && i <= n - m; i++) {
            if (!query(string(i, '1') + string(m - x, '0'))) return i - 1;
        }
        for (int i = max(y, n - m - k + x + 1); i <= n - m; i++) {
            if (query(string(x + 1, '0') + string(n - m - i, '1'))) return i;
        }
    };
    string res = "";
    int i, j;
    for (i = 0, j = 0; i < m; i++) {
        int k = solve(i, j);
        res += string(k - j, '1');
        res += '0';
        j = k;
    }
    if (j < n - m) {
    	res += string(n - m - j, '1');
	}
    for (auto& it : res) it ^= f;
    cout << "! " << res << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    run();
    return 0;
}

C. Coronavirus Battle

题意:
随机在三维空间中给定\(n\)个点,现在有病毒进行攻击,每次病毒会攻击当前没有受保护的点。
定义结点\(i\)保护结点\(j\)为:

  • \(x_i\leq x_j,y_i\leq y_j,z_i\leq z_j\)且两点不重合。

问病毒攻击几轮能消灭所有的点,并且输出每个点存活的轮数。

思路:
因为是随机数据,所以我们可以直接通过模拟上述过程来做。
模拟的思想就是我们对于每一轮攻击,选出一个最小的点,然后找到所有不能保护的点,他们都会在这一轮攻击中被消灭;然后进行下一轮攻击。
具体实现我们可以直接枚举或者用树状数组维护\(z\)的最小值都可。反正数据随机可以不用管复杂度= =
还有一种复杂度比较稳定的做法就是cdq分治,用cdq分治维护每个结点的拓扑序即可。注意一下这种写法我们要按照拓扑序进行转移,也就是说在cdq分治中,先处理左边的,然后处理左边对右边的影响,最后再处理右边。这样能够保证拓扑序从小到大转移。
详见代码:

Code
/*
 * Author:  heyuhhh
 * Created Time:  2020/5/23 14:57:55
 */
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
  #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  void err() { std::cout << std::endl; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
  template <template<typename...> class T, typename t, typename... A> 
  void err(const T <t> &arg, const A&... args) {
  for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
  #define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 1e5 + 5;

unsigned long long k1, k2;
unsigned long long CoronavirusBeats() {
    unsigned long long k3 = k1, k4 = k2;
    k1 = k4;
    k3 ^= k3 << 23;
    k2 = k3 ^ k4 ^ (k3 >> 17) ^ (k4 >> 26);
    return k2 + k4;
}
unsigned long long x[N], y[N], z[N];

struct Point {
    unsigned long long x, y, z;
    int id;   
}a[N], b[N];

int n;
int ans[N];

void Hash(unsigned long long* a) {
    sort(a + 1, a + n + 1);
    a[0] = unique(a + 1, a + n + 1) - a - 1;
}

int find(unsigned long long* a, unsigned long long x) {
    return lower_bound(a + 1, a + a[0] + 1, x) - a;
}

int c[N];

int lowbit(int x) {
    return x & (-x);
}
void del(int x) {
    for (int i = x; i < N; i += lowbit(i)) c[i] = 0;
}
void add(int x, int v) {
    for(int i = x; i < N; i += lowbit(i)) c[i] = max(c[i], v);
}
int query(int x) {
    int ans = 0;
    for(int i = x; i; i -= lowbit(i)) ans = max(ans, c[i]);
    return ans;
}
void cdq(int l, int r) {
    if(l == r) return ;
    int mid = (l + r) >> 1;
    cdq(l, mid);
    sort(a + l, a + mid + 1, [&](Point A, Point B) {
        return A.y < B.y;
    });
    sort(a + mid + 1, a + r + 1, [&](Point A, Point B) {
        return A.y < B.y;
    });
    int t1 = l, t2 = mid + 1;
    for(int i = l; i <= r; i++) {
        if((t1 <= mid && a[t1].y <= a[t2].y) || t2 > r) {
            add(a[t1].z, ans[a[t1].id]);
            b[i] = a[t1++];
        } else {
            ans[a[t2].id] = max(ans[a[t2].id], query(a[t2].z) + 1);
            b[i] = a[t2++];
        }
    }
    for(int i = l; i <= mid; i++) del(a[i].z);
    sort(a + mid + 1, a + r + 1, [&](Point A, Point B) {
        return A.x < B.x;
    });
    cdq(mid + 1, r);
}

void run() {
    cin >> n >> k1 >> k2;
    for (int i = 1; i <= n; ++i) {
        x[i] = CoronavirusBeats();
        y[i] = CoronavirusBeats();
        z[i] = CoronavirusBeats();
        a[i] = Point{x[i], y[i], z[i], i};
    }
    Hash(x), Hash(y), Hash(z);
    for (int i = 1; i <= n; i++) {
        a[i].x = find(x, a[i].x);
        a[i].y = find(y, a[i].y);
        a[i].z = find(z, a[i].z);
    }   
    sort(a + 1, a + n + 1, [&](Point A, Point B) {
        return A.x < B.x;
    });
    for (int i = 1; i <= n; i++) {
        ans[i] = 1;
    }
    cdq(1, n);
    int res = 0;
    for (int i = 1; i <= n; i++) {
        res = max(res, ans[i]);
        --ans[i];
    }
    cout << res << '\n';
    for (int i = 1; i <= n; i++) {
        cout << ans[i] << " \n"[i == n];
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    run();
    return 0;
}

D. Decay of Signals

题意:
给定一颗带点权的树,定义一条路径的价值为\(\displaystyle \frac{a_{p_1}\cdot a_{p_2}\cdots a_{p_m}}{m}\)
现在问路径价值的最小值为多少,输出\("x/y"\)

思路:
分几种清空考虑:

  • 存在\(a_i=0\),那么答案显然;
  • 所有\(a_i\geq 2\),答案即为\(min\{a_i\}\)
  • 找到最长连续的\(1\),设长度为\(m\),那么答案为\(\frac{1}{m}\)。考虑最终答案为\(111x111...\)这样的形式,我们容易发现当\(x=2\)并且左右两边都有\(m\)\(1\)时会出现更优解。所以找一下这种情况就行。

思路就是上面这样,具体的证明应该也比较好证,就拿第三种情况证明一下:
首先答案为\(\frac{1}{m}\),假设现在有\(t\)段连续的\(1\)长度为\(m\),那么答案为\(\displaystyle\frac{b_1\cdots b_{t-1}}{tm+t-1}\),因为\(b_i\geq 2\),我们就取\(b_i=2\),将其与\(\frac{1}{m}\)做差得\(\displaystyle m(2^{t-1}-t)-t+1\)。容易发现当\(t=2\)时,差为负数;当\(t>2\)时,差为非负数;若\(b_i\geq 3\),值为非负数。也就是说只有我们上述所说情况会出现最优解。
证明大概就是这个样子,可能细节会有点问题。。
思路是这样,代码写起来还是挺麻烦的,需要换下根。
详见代码:

Code
/*
 * Author:  heyuhhh
 * Created Time:  2020/5/23 20:42:25
 */
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#include <bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
  #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  void err() { std::cout << std::endl; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
  template <template<typename...> class T, typename t, typename... A> 
  void err(const T <t> &arg, const A&... args) {
  for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
  #define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 1e6 + 5;

int n;
int a[N];
struct Edge {
    int u, v;
}e[N];

vector <int> G[N];
bool vis[N];
int f[N][2], g[N];

void run() {
    cin >> n;
    for (int i = 1; i < n; i++) {
        int u, v; cin >> u >> v;
        e[i] = Edge {u, v};
    }
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    pii ans(INF, INF);
    for (int i = 1; i <= n; i++) {
        if (a[i] == 0) {
            cout << "0/1" << '\n';
            return;
        }
        ans = min(ans, MP(a[i], 1));
    }
    if (ans.fi > 1) {
        cout << ans.fi << '/' << ans.se << '\n';
        return;
    }
    for (int i = 1; i < n; i++) {
        int u = e[i].u, v = e[i].v;
        if (a[u] == 1 && a[v] == 1) {
            G[u].push_back(v);
            G[v].push_back(u);
        }
    }
    
    int m = 0, rt;
    function <void(int, int, int)> dfs;
    dfs = [&](int u, int fa, int d) -> void {
        if (d > m) {
            m = d, rt = u;   
        }
        vis[u] = 1;
        for (auto v : G[u]) if (v != fa) {
            dfs(v, u, d + 1);
        }
    };
    
    function <void(int, int)> dfs2;
    dfs2 = [&](int u, int fa) -> void {
        f[u][0] = 1, f[u][1] = -1;
        for (auto v : G[u]) if (v != fa) {
            dfs2(v, u);
            if (f[v][0] + 1 > f[u][0]) {
                f[u][1] = f[u][0];
                f[u][0] = f[v][0] + 1;
            } else if(f[v][0] + 1 > f[u][1]) {
                f[u][1] = f[v][0] + 1;   
            }
        }       
    };
    
    function <void(int, int, int)> dfs3;
    dfs3 = [&](int u, int fa, int h) -> void {
        for (auto v : G[u]) if (v != fa) {
            g[v] = f[v][0];
            if (f[v][0] + 1 == f[u][0]) {
                g[v] = max(g[v], max(h, f[u][1]) + 1);
                dfs3(v, u, max(h, f[u][1]) + 1);
            } else {
                g[v] = max(g[v], max(h, f[u][0]) + 1);
                dfs3(v, u, max(h, f[u][0]) + 1);
            }
        }
    };
    
    int MAX = 0;
    for (int i = 1; i <= n; i++) if (a[i] == 1 && !vis[i]) {
        rt = i, m = 0;
        dfs(rt, 0, 1), dfs(rt, 0, 1);
        MAX = max(m, MAX);
        dfs2(i, 0);
        g[i] = f[i][0];
        dfs3(i, 0, 0);
    }
    ans = MP(1, MAX);
    vector <int> d(n + 1);
    for (int i = 1; i < n; i++) {
        int u = e[i].u, v = e[i].v;
        if (a[u] > a[v]) swap(u, v);
        if (a[u] == 1 && a[v] == 2) {
            if (g[u] == MAX) ++d[v];
            if (d[v] >= 2) {
                ans = MP(2, 2 * MAX + 1);
                break;
            }
        }
    }
    cout << ans.fi << '/' << ans.se << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    run();
    return 0;
}

E. Even Degree

题意:
给定一个无向图,保证每个点的度数为偶数。
现在要去掉尽可能多的边,去掉一条边必须满足两个点的度数都不同时为奇数。
输出删边的序列。

思路:
所有点度数为偶数我们很容易想到欧拉回路,那么我们就考虑沿着欧拉回路走来删边。
我们容易发现,对于一条路径或者一个环,我们可以留下一条边把其它的都删掉。
照这样来说,如果两个环在一起,有重合一个点,我们也可以留下一条边,但是直接按照欧拉路径来删边是行不通的。这里手画一下其实很好找到删边方法使得最后只留下一条边,就是我们到一个环时先不删最后一条边(也删不了),先删去另外一条出边(如果有的话,没有就留下这条边作为最后一条边),再来删这条边就行。
细节见代码:

Code
/*
 * Author:  heyuhhh
 * Created Time:  2020/5/24 10:38:47
 */
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#include <functional>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
  #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  void err() { std::cout << std::endl; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
  template <template<typename...> class T, typename t, typename... A> 
  void err(const T <t> &arg, const A&... args) {
  for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
  #define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 5e5 + 5;

int n, m;

struct Edge {
    int u, v;   
} e[N];

vector <pii> G[N];
bool vis[N], del[N];

void run() {
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int u, v; cin >> u >> v;
        G[u].push_back(MP(v, i));
        G[v].push_back(MP(u, i));
        e[i] = Edge {u, v};
    }
    vector <int> path;
    function <void(int)> dfs = [&] (int u) -> void {
        vis[u] = true;
        while (sz(G[u])) {
            pii s = G[u][sz(G[u]) - 1]; G[u].pop_back();
            int v = s.fi, id = s.se;
            if (del[id]) continue;
            del[id] = true;
            dfs(v);
            path.push_back(id);
        }
    };
    auto get = [&] (int u, int id) {
        return u ^ e[id].u ^ e[id].v;
    };
    vector <int> ans;
    for (int i = 1; i <= n; i++) if (!vis[i]) {
        path.clear();
        dfs(i);
        reverse(all(path));
        if (sz(path) == 0) continue;
        int st = i, ed = get(st, path[0]);
        ans.push_back(path[0]);
        for (int j = 1; j < sz(path) - 1; j++) {
            if (get(ed, path[j]) == st) {
                ed = get(st, path[j + 1]);
                ans.push_back(path[j + 1]);
                ans.push_back(path[j++]);
            } else {
                ans.push_back(path[j]);
                ed = get(ed, path[j]);
            }
        }
    }
    cout << sz(ans) << '\n';
    for (auto it : ans) cout << it << ' ';
    cout << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    run();
    return 0;
}

F. Find / -type f -or -type d

排序过后判断是否为某一个的前缀就行,我的时间复杂度貌似有点炸,但依旧跑得飞快。
正解的话就直接上字典树吧。

Code

/*
 * Author:  heyuhhh
 * Created Time:  2020/5/23 13:35:27
 */
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
  #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  void err() { std::cout << std::endl; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
  template <template<typename...> class T, typename t, typename... A> 
  void err(const T <t> &arg, const A&... args) {
  for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
  #define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 1e6 + 5;

void run() {
    int n; cin >> n;
    vector <string> s(n);
    for (int i = 0; i < n; i++) {
        cin >> s[i];
    }
    sort(all(s));
    int ans = 0;
    auto chk = [&] (string& str) {
        int len = str.length();
        if (len > 3 && str.substr(len - 4) == ".eoj") return 1;
        return 0;        
    };
    for (int i = 0, j; i < n; i++) {
        if (i == n - 1) {
            ans += chk(s[i]);
        } else {
            int l1 = s[i].length(), l2 = s[i + 1].length();
            if (l1 > l2) {
                ans += chk(s[i]);
            } else if (s[i + 1].substr(0, l1) != s[i]) {
                ans += chk(s[i]);
            }
        }
    }
    cout << ans << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    run();
    return 0;
}

H. Heat Pipes

题意:
给定一个无向图,现在要给这张图染上范围在\([a,b]\)中的颜色,每个颜色必须用一次,并且要求任意相邻两点颜色的差值为\(1\)
最后输出染色方案,否则这种情况不合法。

思路:
这个题和之前cf的一个题很像,那个题相当于染色范围为\([1,3]\)
这里我们可以借鉴类似的思路,但是那个题是二分图,这里显然不满足二分图的条件,我们类似地\(dfs\)肯定也不行。
这里要用到\(bfs\),因为\(bfs\)树有一个很棒的性质:非树边只会出现在同一层或者相邻层。这样就很方便我们进行染色。
怎么判断最后方案合法呢?首先不会出现染不了色的情况,其次必须每种颜色都用到。显然一颗树深度越大染色数量越多,那么我们就要求出图的直径,即深度尽可能大的合法的树。如果对于一个连通块怎么染色都不合法,那么总的就不合法。
那么整个\(bfs\)的过程为\(O(n^2)\)的,我们需要通过枚举每个点为起点进行\(bfs\),找到深度最大的合法\(bfs\)树,这样我们染色能够尽可能多。
有个细节就是如果我们染色超过\(b\)了,那么我们就可以直接\(b,b-1,b,b-1...\)这样给其它结点染色就行,不影响最后方案的正确性。
细节见代码:

Code
/*
 * Author:  heyuhhh
 * Created Time:  2020/5/24 14:40:58
 */
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#include <functional>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
  #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  void err() { std::cout << std::endl; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
  template <template<typename...> class T, typename t, typename... A> 
  void err(const T <t> &arg, const A&... args) {
  for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
  #define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head

void run() {
    int n, m, A, B;
    cin >> n >> m >> A >> B;
    int k = B - A + 1;
    vector <vector <int>> G(n + 1);
    for (int i = 1; i <= m; i++) {
        int u, v; cin >> u >> v;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    if (k == 1) {
        if (m) {
            cout << "No" << '\n';
        } else {
            cout << "Yes" << '\n';
            for (int i = 1; i <= n; i++) {
                cout << A << " \n"[i == n];
            }              
        }
        return;   
    }
    
    vector <vector<int>> part;
    vector <int> t, bel(n + 1, -1);
    function <void(int)> dfs = [&] (int u) {
        bel[u] = sz(part);
        t.push_back(u);
        for (auto v : G[u]) {
            if (bel[v] == -1) {
                dfs(v);
            }
        }
    };
    
    for (int i = 1; i <= n; i++) if (bel[i] == -1) {
        t.clear();
        dfs(i);
        part.push_back(t);
    }
    
    vector <int> col(n + 1);
    auto bfs = [&] (int u) {
        int res = 0;
        col[u] = 1;
        queue <int> q;
        q.push(u);
        while (!q.empty()) {
            int u = q.front(); q.pop();
            res = max(res, col[u]);
            for (auto v : G[u]) {
                if (!col[v]) {
                    col[v] = col[u] + 1;
                    q.push(v);
                } else if (col[u] == col[v]) {
                    return -1;
                }
            }
        }
        return res;
    };
    
    int sum = 0;   
    for (int i = 0; i < sz(part); i++) {
        int node, dis = 0;
        for (auto u : part[i]) {
            for (auto it : part[i]) col[it] = 0;
            int tmp = bfs(u);
            if (tmp == -1) continue;
            if (tmp > dis) {
                dis = tmp;
                node = u;
            }
        }
        if (dis == 0) {
            cout << "No" << '\n';
            return;
        }
        for (auto it : part[i]) col[it] = 0;
        bfs(node);
        for (auto u : part[i]) {
            if (sum + col[u] > k) {
                col[u] = k - ((sum + col[u] - k) & 1);
            } else col[u] += sum;
        }
        sum += dis;
    }
    if (sum < k) {
        cout << "No" << '\n';
        return;
    }
    for (int i = 1; i <= n; i++) {
        col[i] += A - 1;
    }
    cout << "Yes" << '\n';
    for (int i = 1; i <= n; i++) {
        cout << col[i] << " \n"[i == n];
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    int T; cin >> T; while(T--)
    run();
    return 0;
}

I. Idiotic Suffix Array

随便构造一下就行。

Code
/*
 * Author:  heyuhhh
 * Created Time:  2020/5/23 13:23:08
 */
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
  #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
  void err() { std::cout << std::endl; }
  template<typename T, typename...Args>
  void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
  template <template<typename...> class T, typename t, typename... A> 
  void err(const T <t> &arg, const A&... args) {
  for (auto &v : arg) std::cout << v << ' '; err(args...); }
#else
  #define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 1e5 + 5;

void run() {
    int n, k; cin >> n >> k;
    string ans = "";
    for (int i = 1; i < k; i++) ans += "a";
    for (int i = 1; i <= n - k; i++) ans += "c";
    ans += "b";
    reverse(all(ans));
    cout << ans << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
    run();
    return 0;
}
posted @ 2020-05-23 23:45  heyuhhh  阅读(471)  评论(0编辑  收藏  举报