HDOJ1150(最小点集覆盖)

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
76
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
 
using namespace std;
 
#define N 101
 
int match[N];
bool vis[N];
vector <int> e[N];
int n, m, k;
 
void InitRead();
 
void DataProcess();
 
bool Dfs(int x);
 
int main()
{
    while (~scanf("%d", &n))
    {
        if (n == 0) break;
        InitRead();
        DataProcess();
    }
    return 0;
}
 
void InitRead()
{
    scanf("%d %d", &m, &k);
    memset(match, -1, sizeof(match));
    for (int i=0; i<N; ++i) e[i].clear();
    int a, b;
    for (int i=0; i<k; ++i)
    {
        scanf("%*d %d %d", &a, &b);
        if (a == 0 || b == 0) continue;     //可以用模式0解决的任务不建边
        e[a].push_back(b);
    }
    return;
}
 
void DataProcess()
{
    int ans = 0;
    for (int i=0; i<n; ++i)
    {
        memset(vis, false, sizeof(vis));
        if (Dfs(i)) ans++;
    }
    printf("%d\n", ans);
    return;
}
 
bool Dfs(int x)
{
    int size = e[x].size();
    for (int i=0; i<size; ++i)
    {
        if (!vis[e[x][i]])
        {
            vis[e[x][i]] = true;
            if (match[e[x][i]] == -1 || Dfs(match[e[x][i]]))
            {
                match[e[x][i]] = x;
                return true;
            }
        }
    }
    return false;
}

 

posted on   vCoders  阅读(184)  评论(0编辑  收藏  举报

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示