P2016 战略游戏
  • 板块P2016 战略游戏
  • 楼主kanm7
  • 当前回复4
  • 已保存回复4
  • 发布时间2022/6/2 23:21
  • 上次更新2023/10/28 00:03:45
查看原帖
P2016 战略游戏
675438
kanm7楼主2022/6/2 23:21

求解,这样做思路哪里不对

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1510;
int h[N * 2], e[N * 2], ne[N * 2], idx;
int n;
bool vis[N];
int f[N][2];
void add(int a, int b)
{
    e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
void dfs(int u) // 计算出 f[u][0] 和 f[u][1]
{
    f[u][1] = 1;
    vis[u] = true;

    for (int i = h[u]; i != -1; i = ne[i])
    {
        int j = e[i];
        if (!vis[j])
        {
            cout << u << ' ' << j << endl;
            dfs(j);
        }
        else
            continue;
        f[u][0] += f[j][1];
        f[u][1] += min(f[j][1], f[j][0]);
    }
    return;
}
int main()
{
    // ios::sync_with_stdio(false);
    // cin.tie(0);
    while (cin >> n)
    {
        memset(h, -1, sizeof h);
        memset(vis, 0, sizeof vis);
        idx = 0;
        for (int i = 0; i < n; i++)
        {
            int id, k, x;
            cin >> id >> k;
            while (k--)
            {
                cin >> x;
                add(id, x);
                add(x, id);
            }
        }
        dfs(0);
        int t = min(f[0][0], f[0][1]);
        cout << t << endl;
    }
    return 0;
}
2022/6/2 23:21
加载中...