日度归档:16 8 月, 2017

洛谷 奶牛家谱 Cow Pedigrees

毫不夸张的说:USACO里2.3节最难的动规、、、
基本是照着题解一个字一个字对的。。。。
题号:1472

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
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#define MOD 9901
using namespace std;
int n, k;
long long f[210][110],dp[210][110]; //必须是long long
int main(){
    cin >> n >> k;
    //if (n % 2 == 0){
    //  cout << 0 << endl;
    //  return 0;
    //}
    dp[1][1] = 1; f[0][0] = 1;
    for (int i = 1; i <= k; i++)f[1][i] = f[0][i] = 1;
    for (int i = 3; i <= n; i += 2){ //i:Node Num;j:depth
        int j = 1; while ((1 << j) - 1 < i)j++;
        for (; j <= k; j++){
            for (int l = 1; l <= i - 1; l += 2){
                dp[i][j] += dp[l][j - 1] * f[i - l - 1][j - 2];
                dp[i][j] += f[l][j - 2] * dp[i - l - 1][j - 1];
                dp[i][j] += dp[l][j - 1] * dp[i - l - 1][j - 1];
            }
            dp[i][j] %= MOD;
        }
        for (int j = 1; j <= k; j++)f[i][j] = (f[i][j - 1] + dp[i][j]) % MOD;
    }
    cout << dp[n][k] << endl;
}

USACO Section 2.3 Controlling Companies

STDIN/STDOUT

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
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<utility>
#include<algorithm>
#include<map>
#include<vector>
#include<list>
#include<functional>
#include<string>
using namespace std;
int u[10005], v[10005], w[10005], first[105], nxt[10005];
int n, m;
bool control[105],visited[105];
int percentage[105];
void dfs(int node) {
    if (visited[node])return;
    visited[node] = true;
    for (int e = first[node]; e != -1; e = nxt[e]) {
        percentage[v[e]] += w[e];
        if (percentage[v[e]] > 50) {
            control[v[e]] = true;
            dfs(v[e]);
        }
    }
}
int main() {
    memset(first, -1, sizeof(first));
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> u[i] >> v[i] >> w[i];
        nxt[i] = first[u[i]];
        first[u[i]] = i;
        m = max(m, max(u[i], v[i]));
    }
    for (int i = 1; i <= m; i++) {
        memset(visited, false, sizeof(visited));
        memset(control, false, sizeof(control));
        memset(percentage, 0, sizeof(percentage));
        dfs(i);
        for (int j = 1; j <= m; j++) {
            if (i != j&&control[j])cout << i << " " << j << endl;
        }
    }
}