博客
关于我
Codeforces Round #592 (Div. 2) D. Paint the Tree(构造)
阅读量:387 次
发布时间:2019-03-05

本文共 3378 字,大约阅读时间需要 11 分钟。

??????????????????????????????????????????????a[i], b[i], c[i]????????????????

????

?????????????????????????????????????????????????

  • ???????????????????????????????????????????????????
  • ?????????????????????????????????????????????????
  • ???????????????????????????????????????????????????6?????????????????
  • ????????????????????????????????????????
  • ????

    #include 
    #include
    using namespace std;const int maxn = 1e5 + 5;vector
    g[maxn];ll a[maxn], b[maxn], c[maxn];ll ans[7][maxn];ll minn = 2e18;ll temp;void dfs(int x, int fa, int prev_color, int current_color, int deep, int id) { if (deep % 3 == 1) { ans[id][x] = a[x]; } else if (deep % 3 == 2) { ans[id][x] = b[x]; } else { ans[id][x] = c[x]; } for (int to : g[x]) { if (to == fa) continue; dfs(to, x, current_color, prev_color, deep + 1, id); }}int main() { int n, k, x, y; scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%lld", &a[i]); scanf("%lld", &b[i]); scanf("%lld", &c[i]); } for (int i = 1; i <= n; ++i) { scanf("%d %d", &x, &y); g[x].push_back(y); g[y].push_back(x); } if (g[1].size() != n - 1) { puts("-1"); return 0; } vector
    > order(n); for (int i = 1; i <= n; ++i) { order[i - 1] = make_pair(g[i][0], g[i][1]); } int root = 1; for (int i = 1; i <= n; ++i) { if (g[i].size() != 1) { puts("-1"); return 0; } } for (int init1 = 1; init1 <= 3; ++init1) { for (int init2 = 1; init2 <= 3; ++init2) { if (init1 == init2) continue; for (int init3 = 1; init3 <= 3; ++init3) { if (init1 != init3 && init2 != init3) { int id = 1; for (int i = 1; i <= n; ++i) { if (order[i - 1].first == i) { continue; } if (order[i - 1].second != i) { puts("-1"); return 0; } } dfs(root, -1, -1, init1, 1, 1); dfs(root, -1, init1, init2, 1, 2); dfs(root, -1, init1, init3, 1, 3); dfs(root, -1, init2, init1, 1, 4); dfs(root, -1, init2, init3, 1, 5); dfs(root, -1, init3, init1, 1, 6); for (int i = 1; i <= 6; ++i) { temp = 0; for (int j = 1; j <= n; ++j) { if (ans[i][j] == 1) { temp += a[j]; } else if (ans[i][j] == 2) { temp += b[j]; } else { temp += c[j]; } } if (temp < minn) { minn = temp; } } } } } } cout << minn << endl; return 0;}

    ????

  • ?????????????????????????
  • ????????????????????????????????
  • ?????dfs????????????????????????????????
  • ???????????????????????????????????????
  • ???????????????????????????
  • ????????????????????????????????????????

    转载地址:http://trewz.baihongyu.com/

    你可能感兴趣的文章
    python | aiofiles,一个超酷的 Python 库!
    查看>>
    python | akshare,一个超强的 开源Python 金融数据接口库!
    查看>>
    python | alabaster,一个强大的 关于alabaster 主题 Python 库!
    查看>>
    python | algorithms,一个超赞的 集合常用算法的Python 库!
    查看>>
    python调用jpype 报错:OSError JVM is already started和JVM cannot be restarted
    查看>>
    python | authlib,一个强大的 Python 库!
    查看>>
    python | awswrangler,一个高效的 Python 库!
    查看>>
    python | bashplotlib,一个有趣的Python库!
    查看>>
    python | bentoml,一个超级厉害的 模型部署 Python 库!
    查看>>
    python调用jar包的模块_python调用jar包
    查看>>
    python | black,一个神奇的 代码格式化工具 Python 库!
    查看>>
    python | bleach,一个超强的 Python 库!
    查看>>
    python | cartopy,一个有趣的 Python 库!
    查看>>
    python | cloud-init,一个实用的 云计算 Python 库!
    查看>>
    python | code2flow,一个神奇的 Python 库!
    查看>>
    python | cudf,一个超实用的 Python 库!
    查看>>
    python | daphne,一个非常nice的 Python 库!
    查看>>
    python调用halcon
    查看>>
    python | doit,一个非常实用的 Python 库!
    查看>>
    python | easyocr,一个超厉害的 关于OCR的 Python 库!
    查看>>