2017 Wuhan University Programming Contest (Online Round) 划水

半年不碰 C++ 实力明显变弱

个人贡献三题,零罚时比较让人欣慰,至少手还算稳

(还是太弱了

A. One car comes and one car goes

听说是小学奥数题,没啥好说的

B. Color

裸的树形 DP ,穷举一下每个点选各种 type 的种类数就好,剩下的就是乘法原理吧

#include <bits/stdc++.h>

#define MOD 10000009
#define N 50000
#define M 30

using namespace std;

int n, m, s, t;

bool can[N][M];
vector<int> a[N];
long long f[N][M];

inline long long nis(int c, int i) {
    static int ret;
    ret = f[c][0] - f[c][i];
    return ret < 0 ? ret + MOD : ret;
}

void dfs(int t, int p) {
    int len = a[t].size(), c;
    for (int i = 0; i < len; i++)
        if (a[t][i] != p) {
            dfs(a[t][i], t);
        }
    for (int i = 1; i <= m; i++) {
        if (can[t][i])
            f[t][i] = 1;
        else continue ;
        for (int j = 0; j < len; j++) {
            if ((c = a[t][j]) == p)
                continue ;
            f[t][i] *= nis(c, i);
            f[t][i] %= MOD;
        }
    }
    for (int i = 1; i <= m; i++) {
        f[t][0] += f[t][i];
        f[t][0] %= MOD;
    }
}

int main() {
    srand(time(0));
    ios::sync_with_stdio(0);

    while (cin >> n >> m) {
        memset(f, 0, sizeof(f));
        for (int i = 1; i <= n; i++)
            a[i].clear();
        for (int i = 1; i < n; i++) {
            cin >> s >> t;
            a[s].push_back(t);
            a[t].push_back(s);
        }
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++) {
                cin >> t;
                can[i][j] = t;
            }

        dfs(s = rand() % n + 1, 0);
        cout << f[s][0] << endl;
    }

    return 0;
}

E. Lost in WHU

给一个无向图,问在 T 步之内有多少条路线从 1 走到 n

结论题,直接建立图的邻接矩阵,然后求矩阵 T 次幂就好

由于题目要求到了 n 就不能走出去,所以需要删掉 n 所有出边,然后 n 自环一下就好

但是由于太弱并不知道还有自环这种操作,一股脑删光了所有射出边,给邻接矩阵多加了一层,用一种十分不优雅的方式侥幸过了 XD

#include <bits/stdc++.h>

#define N 100
#define MOD 1000000007LL

using namespace std;

typedef long long LL;
typedef vector<vector<LL>> mat;

int n, m;

inline void init(mat &x, bool I) {
    x.resize(n + 1);
    for (int i = 0; i <= n; i++)
        x[i].resize(n + 1);
    for (int i = 0; i <= n; i++)
        for (int j = 0; j <= n; j++)
            x[i][j] = 0;
    if (I) for (int i = 0; i <= n; i++)
        x[i][i] = 1;
}

inline mat mul(const mat &a, const mat &b) {
    mat c; init(c, false);
    for (int i = 0; i <= n; i++)
        for (int j = 0; j <= n; j++) {
            for (int k = 0; k <= n; k++) {
                c[i][j] += (a[i][k] * b[k][j]) % MOD;
                c[i][j] %= MOD;
            }
        }
    return c;
}

inline mat quick_mod(mat a, int q) {
    mat c; init(c, true);
    while (q) {
        if (q & 1)
            c = mul(a, c);
        a = mul(a, a);
        q >>= 1;
    } 
    return c;
}

inline void print(const mat &a) {
    for (int i = 0; i <= n; i++, cout << endl)
        for (int j = 0; j <= n; j++)
            cout << a[i][j] << " ";
}

int main() {
    int x, y, t;
    cin >> n >> m;
    mat a; init(a, false);
    while (m--) {
        cin >> x >> y;
        x--, y--;
        a[x][y] = 1;
        a[y][x] = 1;
    }
    cin >> t;

    for (int i = 0; i < n; i++)
        a[n - 1][i] = 0;
    a[n - 1][n] = 1;
    a[n][n] = 1;

    a = quick_mod(a, t);
    a[0][n] += a[0][n - 1];
    cout << a[0][n] % MOD << endl;

    return 0;
}

标签: whu, 矩阵快速幂, 树形 dp

添加新评论