2017 Wuhan University Programming Contest (Online Round) 划水
Doveccl

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

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

(还是太弱了

One car comes and one car goes

image
image

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

Color

image
image
image

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#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;
}

Lost in WHU

image
image
image

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

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#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;
}
 评论
评论插件加载失败
正在加载评论插件
由 Hexo 驱动 & 主题 Keep
总字数 24.2k 访客数 访问量