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

    你可能感兴趣的文章
    Objective-C实现getline函数功能(附完整源码)
    查看>>
    Objective-C实现gnome sortt侏儒排序算法(附完整源码)
    查看>>
    Objective-C实现GraphVertex图顶点算法(附完整源码)
    查看>>
    Objective-C实现greatest common divisor最大公约数算法(附完整源码)
    查看>>
    Objective-C实现greedy coin change贪心硬币找零算法(附完整源码)
    查看>>
    Objective-C实现half adder半加器算法(附完整源码)
    查看>>
    Objective-C实现hamiltonianCycle哈密尔顿图算法(附完整源码)
    查看>>
    Objective-C实现hamming code汉明码算法(附完整源码)
    查看>>
    Objective-C实现hamming numbers汉明数算法(附完整源码)
    查看>>
    Objective-C实现hammingDistance汉明距离算法(附完整源码)
    查看>>
    Objective-C实现hanning 窗(附完整源码)
    查看>>
    Objective-C实现hanoiTower汉诺塔算法(附完整源码)
    查看>>
    Objective-C实现hardy ramanujana定理算法(附完整源码)
    查看>>
    Objective-C实现harmonic series调和级数算法(附完整源码)
    查看>>
    Objective-C实现harris算法(附完整源码)
    查看>>
    Objective-C实现HashTable哈希表算法(附完整源码)
    查看>>
    Objective-C实现haversine distance斜距算法(附完整源码)
    查看>>
    Objective-C实现heap sort堆排序算法(附完整源码)
    查看>>
    Objective-C实现heaps algorithm堆算法(附完整源码)
    查看>>
    Objective-C实现heap堆算法(附完整源码)
    查看>>