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

    你可能感兴趣的文章
    Object Oriented Programming in JavaScript
    查看>>
    object references an unsaved transient instance - save the transient instance before flushing
    查看>>
    Object 类的常见方法有哪些?
    查看>>
    Object-c动态特性
    查看>>
    Object.assign用法
    查看>>
    Object.create
    查看>>
    Object.defineProperty详解
    查看>>
    Object.keys()的详解和用法
    查看>>
    objectForKey与valueForKey在NSDictionary中的差异
    查看>>
    Objective - C 小谈:消息机制的原理与使用
    查看>>
    OBJECTIVE C (XCODE) 绘图功能简介(转载)
    查看>>
    Objective-C ---JSON 解析 和 KVC
    查看>>
    Objective-C 编码规范
    查看>>
    Objective-Cfor循环实现Factorial阶乘算法 (附完整源码)
    查看>>
    Objective-C——判断对象等同性
    查看>>
    objective-c中的内存管理
    查看>>
    Objective-C之成魔之路【7-类、对象和方法】
    查看>>
    Objective-C享元模式(Flyweight)
    查看>>
    Objective-C以递归的方式实现二叉搜索树算法(附完整源码)
    查看>>
    Objective-C内存管理教程和原理剖析(三)
    查看>>