求助

UVA1292 Strategic game

通过的代码 ```cpp #include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <vector> using namespace std; int n, ans; vector<int> map[3010]; int color[3010]; int match[3010]; bool book[3010]; void dfs1(int id){ for (int i = 0; i < map[id].size(); i++){ int temp = map[id][i]; if (color[temp]) continue; color[temp] = 3 - color[id]; dfs1(temp); } } bool dfs2(int id){ for (int i = 0; i < map[id].size(); i++){ int temp = map[id][i]; if (!book[temp]){ book[temp] = 1; if (!match[temp] || dfs2(match[temp])){ match[temp] = id; return true; } } } return false; } int main(int argc, char** argv){ freopen("testdata.txt", "r", stdin); //freopen("ans2.txt", "w", stdout); while (scanf("%d", &n) != EOF){ ans = 0; for (int i = 0; i <= n; i++) map[i].clear(); memset(color, 0, sizeof(color)); memset(match, 0, sizeof(match)); for (int i = 1; i <= n; i++){ int temp, ct; scanf("%d:(%d)", &temp, &ct); temp++; for (int k = 1; k <= ct; k++){ int u; scanf("%d", &u); u++; map[u].push_back(temp); map[temp].push_back(u); } } color[1] = 1; dfs1(1); for (int i = 1; i <= n; i++){ //cout << color[i] << endl; if (color[i] == 1){ memset(book, 0, sizeof(book)); if (dfs2(i)){ ans++; } } } cout << ans << endl; // for (int i = 0; i < n; i++) // cout << (match[i] ? match[i] + 1 : match[i]) << endl; } return 0; } ```
by 于昭琛 @ 2019-01-05 16:21:00


好了我知道了 ```cpp match[temp] = id; ``` 可能被赋值为0 然后 ```CPP if (!match[temp] || dfs2(match[temp])) ``` 判断就会有问题
by 于昭琛 @ 2019-01-05 16:33:48


|