2019/10/24 CSP-S Analog

T1 tone

Meaning of the questions:

Untitled .png

Consideration must belong \ (A \) in cook, belonging \ (B \) in cook, find this connection \ (A \) and \ (B \) sides, respectively, and then press \ (DFS \) sequence staining to
note belonging \ (a \) communicates or blocks belonging \ (B \) communication block may \ (DFS \) trees are not embodied as a complete sub-tree, it is necessary to have determined a bit

#include<bits/stdc++.h>
#define N (200000 + 10)
using namespace std;
inline int read() {
    int cnt = 0, f = 1; char c = getchar();
    while (!isdigit(c)) {if (c == '-') f = -f; c = getchar();}
    while (isdigit(c)) {cnt = (cnt << 3) + (cnt << 1) + (c ^ 48); c = getchar();}
    return cnt * f;
}
int n, a, b, x, y, fa[N], siz[N], id[N], val;
int first[N], to[N], nxt[N], tot;
void add (int x, int y) {nxt[++tot] = first[x], first[x] = tot, to[tot] = y;}
void get_siz(int x, int father) {
    siz[x] = 1; fa[x] = father;
    for (register int i = first[x]; i; i = nxt[i]) {
        int v = to[i];
        if (v == father) continue;
        get_siz(v, x), siz[x] += siz[v];
    }
}
void print(int x, int fa, int d) {
    for (register int i = first[x]; i; i = nxt[i]) {
        int v = to[i];
        if (v == fa) continue;
        print(v, x, d);
    }
    val += d;
    id[x] = val; 
}
void work () {
    bool ok = 0;
    get_siz(1, 0);
    for (register int i = 1; i <= n; ++i) 
        if (siz[i] == a) {
            val = 0;
            print(i, fa[i], 1);
            val = 0;
            print(fa[i], i, -1);
            ok = 1;
        } else if (siz[i] == b) {
            val = 0;
            print(i, fa[i], -1);
            val = 0;
            print(fa[i], i, 1);
            ok = 1;
        }
    if (!ok) puts("-1");
    else for (register int i = 1; i <= n; ++i) printf("%d ", id[i]);
    putchar('\n');
}
int main() {
    n = read(), a = read(), b = read();
    for (register int i = 1; i <= n - 1; ++i) {
        x = read(), y = read();
        add(x, y), add(y, x);
    }
    work();
    return 0;
}

T2 Jerry

First find a property bracket structure nested up to two
if there are three such nesting can simplify
((()))

(() ())
symbols and three anti-anti-equivalent layer
disposed \ (F [i] [0 ~ 2] \) represents the current to the second process \ (I \) bits, preceded by \ (0/1/2 \) unpaired left parenthesis
in the current number \ (> 0 \) state consider transferred up the right parenthesis \ (<0 \) considering the first force in the "-" after a fill opening parenthesis (if the answer is not preferred, naturally removed in a later update :-( a) + b), then transferred
DETAILED codes see equation

#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read() {
    int cnt = 0, f= 1;char c = getchar();
    while (!isdigit(c)) {if (c == '-') f = -f; c = getchar();}
    while (isdigit(c)) {cnt = (cnt << 3) + (cnt << 1) + (c ^ 48); c = getchar();}
    return cnt * f;
}
int T, n, a[500010], dp[500010][3];
signed main() {
    T = read();
    while (T--) {
        n = read();
        for (register int i = 1; i <= n; ++i) a[i] = read();
        dp[0][0] = 0, dp[0][1] = dp[0][2] = -1e18;
        for (register int i = 1; i <= n; ++i) 
            if (a[i] > 0) {
                dp[i][0] = max(max(dp[i - 1][0] + a[i], dp[i - 1][1] + a[i]), dp[i - 1][2] + a[i]);
                dp[i][1] = max(dp[i - 1][1] - a[i], dp[i - 1][2] - a[i]);
                dp[i][2] = dp[i - 1][2] + a[i];
            } else {
                dp[i][0] = -1e18;
                dp[i][1] = max(max(dp[i - 1][0] + a[i], dp[i - 1][1] + a[i]), dp[i - 1][2] + a[i]);
                dp[i][2] = max(dp[i - 1][1] - a[i], dp[i - 1][2] - a[i]);
            }
        printf("%lld\n", max(max(dp[n][0], dp[n][1]), dp[n][2]));
    }
    return 0;
}

T3 do not want to write too much trouble, first the goo

Guess you like

Origin www.cnblogs.com/kma093/p/11735159.html