Codeforces Round #592 (Div. 2)

D. Paint the Tree

题意:
给出一棵树,有三种颜色,要求任意链上的三点的颜色是不一样的,每个点图3种颜色都有自己的花费值,问最小的花费
Solution:
显然一个点度超过2,那么这3个点颜色都不同,无解
所以度都为1,2。树退化成链。
确定了第1,2个,第3个点也唯一确定了(不和1,2的一样),那第4个也出来了
所以两个点就能确定所有点。枚举前两个点颜色就行

/**/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <queue>

typedef long long LL;
typedef unsigned long long ULL;
using namespace std;

bool Sqrt(LL n) { return (LL)sqrt(n) * sqrt(n) == n; }
const double PI = acos(-1.0), ESP = 1e-10;
const LL INF = 99999999999999;
const int inf = 999999999, N = 1e5 + 5;
int n, u, v, rt, a[] = {1, 2, 3}, ans[N], cur[N], c[4][N], deg[N];
LL res = 1e18, cost;
vector<int> G[N];

void dfs(int u, int h, int p = 0)
{
    cost += c[a[h]][u];
    cur[u] = h;
    for(auto v : G[u]) if(v != p)
        dfs(v, (h + 1) % 3, u);
}

void solve()
{
    cost = 0;
    dfs(rt, 0);
    if(cost < res) {
        res = cost;
        for(int i = 1; i <= n; i++) ans[i] = a[cur[i]];
    }
}

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    scanf("%d", &n);
    for(int i = 1; i <= 3; i++) for(int j = 1; j <= n; j++) scanf("%d", &c[i][j]);
    for(int i = 1; i < n; i++) {
        scanf("%d%d", &u, &v);
        G[u].push_back(v); G[v].push_back(u);
        deg[v]++; deg[u]++;
    }
    rt = -1;
    for(int i = 1; i <= n; i++) {
        if(deg[i] > 2) { puts("-1"); exit(0);}
        if(deg[i] == 1 && rt == -1) rt=  i;
    }
    do {
        solve();
    }while(next_permutation(a, a + 3));
    printf("%lld\n", res);
    for(int i = 1; i < n; i++) printf("%d ", ans[i]);
    printf("%d\n", ans[n]);

    return 0;
}
/*
    input:
    output:
    modeling:
    methods:
    complexity:
    summary:
*/

C. The Football Season
简单数学,打的时候没有发现限制条件x < w,T了;后来狂想exgcd,emmmmm

LL n, p, w, d;

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    while(scanf("%lld%lld%lld%lld", &n, &p, &w, &d) == 4) {
        bool flag = 1;
        for(LL x = 0, y; x < w; x++) {
            LL k = p - d * x;
            if(k % w == 0) { 
                y = k / w;
                if(y >= 0 && x + y <= n) { flag = 0; printf("%lld %lld %lld\n", y, x, n - x - y); break;}
            }
        }
        if(flag) puts("-1");
    }

    return 0;
}
上一篇:Codeforces Round #592 (Div. 2)


下一篇:Codeforces Round #592 (Div. 2)