博客
关于我
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/

    你可能感兴趣的文章
    npm错误Error: Cannot find module ‘postcss-loader‘
    查看>>
    npm,yarn,cnpm 的区别
    查看>>
    NPOI之Excel——合并单元格、设置样式、输入公式
    查看>>
    NPOI初级教程
    查看>>
    NPOI利用多任务模式分批写入多个Excel
    查看>>
    NPOI在Excel中插入图片
    查看>>
    NPOI将某个程序段耗时插入Excel
    查看>>
    NPOI格式设置
    查看>>
    NPOI设置单元格格式
    查看>>
    Npp删除选中行的Macro录制方式
    查看>>
    NR,NF,FNR
    查看>>
    nrf24l01+arduino
    查看>>
    nrf开发笔记一开发软件
    查看>>
    nrm —— 快速切换 NPM 源 (附带测速功能)
    查看>>
    nrm报错 [ERR_INVALID_ARG_TYPE]
    查看>>
    NS3 IP首部校验和
    查看>>
    NSDateFormatter的替代方法
    查看>>
    NSError 的使用方法
    查看>>
    nsis 安装脚本示例(转)
    查看>>
    NSJSON的用法(oc系统自带的解析方法)
    查看>>