QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#602602#9230. Routing K-CodesJDScript0117WA 67ms16416kbC++174.2kb2024-10-01 11:07:412024-10-01 11:07:42

Judging History

你现在查看的是最新测评结果

  • [2024-10-01 11:07:42]
  • 评测
  • 测评结果:WA
  • 用时:67ms
  • 内存:16416kb
  • [2024-10-01 11:07:41]
  • 提交

answer

#include <cstdio>
#include <algorithm>
using namespace std;

struct Func {
    long long k, b;
};

int n, m, head[200000], to[400000], nxt[400000], out[400000], vst[400000], u, v;
int rt, flag, root_depth[200000], depth[200000], tree_depth[200000], sons[200000][2], l[200000];
Func tree_func[200000];
long long ans = 1e18;

Func make(long long k, long long b) {
    Func nw;
    nw.k = k;
    nw.b = b;
    return nw;
}

long long getValue(Func f, long long x) {
    return f.k * x + f.b;
}

Func grower(Func a) {
    return make((a.k << 1) + 1, a.b);
}

Func combine(Func a, Func b) {
    return make((a.k + b.k << 1) + 1, a.b + b.b + min(getValue(a, 1), getValue(b, 1)));
}

void add(int id, int u, int v) {
    to[id] = v;
    nxt[id] = head[u];
    head[u] = id;
    out[u] ++;
    return;
}

void init() {
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; i ++) {
        head[i] = -1;
    }
    for (int i = 0; i < m; i ++) {
        scanf("%d %d", &u, &v);
        add(i << 1, u - 1, v - 1);
        add(i << 1 | 1, v - 1, u - 1);
    }
    return;
}

void buildTree(int id, int fa) {
    l[id] = 0;
    for (int i = head[id]; i >= 0; i = nxt[i]) {
        if (to[i] ^ fa) {
            sons[id][l[id] ++] = to[i];
            buildTree(to[i], id);
        }
    }
    return;
}

void initDepth(int id) {
    tree_depth[id] = depth[id];
    for (int i = 0; i < l[id]; i ++) {
        depth[sons[id][i]] = depth[id] + 1;
        initDepth(sons[id][i]);
        tree_depth[id] = max(tree_depth[sons[id][i]], tree_depth[id]);
    }
    return;
}

void initRootDepth(int id, int up) {
    root_depth[id] = max(tree_depth[id], up);
    if (l[id] == 1) {
        initRootDepth(sons[id][0], up + 1);
    } else if (l[id] == 2) {
        initRootDepth(sons[id][0], max(up, tree_depth[sons[id][1]] - depth[id]) + 1);
        initRootDepth(sons[id][1], max(up, tree_depth[sons[id][0]] - depth[id]) + 1);
    }
    return;
}

void initFunc(int id) {
    Func f[2];
    for (int i = 0; i < l[id]; i ++) {
        initFunc(sons[id][i]);
        f[i] = tree_func[sons[id][i]];
    }
    if (l[id] == 0) {
        tree_func[id] = make(1, 0);
    } else if (l[id] == 1) {
        tree_func[id] = grower(f[0]);
    } else if (l[id] == 2) {
        tree_func[id] = combine(f[0], f[1]);
    }
    return;
}

void build() {
    if (m ^ n - 1) {
        flag = 1;
        return;
    }
    for (int i = 0; i < n; i ++) {
        if (out[i] > 3) {
            flag = 1;
            return;
        }
    }
    for (rt = 0; rt < n; rt ++) {
        if (out[rt] ^ 3) {
            break;
        }
    }
    buildTree(rt, rt);
    initDepth(rt);
    initRootDepth(rt, 0);
    for (rt = 0; rt < n; rt ++) {
        if (out[rt] + root_depth[rt] <= 33 && (out[rt] ^ 3)) {
            break;
        }
    }
    if (rt == n) {
        flag = 1;
        return;
    }
    buildTree(rt, rt);
    initDepth(rt);
    initFunc(rt);
    return;
}

void calculatetMin(int id, Func f) {
    if (out[id] ^ 3) {
        if (out[id] + root_depth[id] > 33) {
            return;
        }
        if (id == rt) {
            if (l[id] == 1) {
                ans = min(ans, getValue(tree_func[sons[id][0]], 1));
            } else if (l[id] == 2) {
                ans = min(ans, getValue(tree_func[id], 1));
            }
        } else {
            if (l[id] == 0) {
                ans = min(ans, getValue(f, 1));
            } else if (l[id] == 1) {
                ans = min(ans, getValue(combine(tree_func[sons[id][0]], f), 1));
            }
        }
    }
    if (l[id] == 1) {
        calculatetMin(sons[id][0], grower(f));
    } else if (l[id] == 2) {
        calculatetMin(sons[id][0], combine(tree_func[sons[id][1]], f));
        calculatetMin(sons[id][1], combine(tree_func[sons[id][0]], f));
    }
    return;
}

void calculate() {
    if (flag) {
        printf("NIE\n");
    } else {
        calculatetMin(rt, make(0, 0));
        printf("%lld\n", ans);
    }
    return;
}

void solve() {
    init();
    build();
    calculate();
    return;
}

int main() {
    solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 11848kb

input:

4 3
1 2
1 3
1 4

output:

6

result:

ok single line: '6'

Test #2:

score: 0
Accepted
time: 1ms
memory: 7688kb

input:

4 6
1 2
2 3
3 4
4 1
1 3
2 4

output:

NIE

result:

ok single line: 'NIE'

Test #3:

score: 0
Accepted
time: 0ms
memory: 9720kb

input:

10 10
9 3
5 10
1 4
10 8
8 3
7 3
9 6
8 6
7 2
4 5

output:

NIE

result:

ok single line: 'NIE'

Test #4:

score: 0
Accepted
time: 56ms
memory: 14768kb

input:

200000 199999
172774 188052
195063 155577
38023 148303
30605 155047
170238 19344
46835 58255
154268 109062
197059 116041
136424 12058
42062 149807
109545 115380
132322 51106
16706 162612
62234 31319
195735 80435
173898 84051
19876 102910
186924 9136
123094 117097
145054 173049
137364 152731
82662 18...

output:

NIE

result:

ok single line: 'NIE'

Test #5:

score: -100
Wrong Answer
time: 67ms
memory: 16416kb

input:

200000 199999
168192 30733
164413 46673
175210 2329
69389 67102
33991 152553
91061 55265
166724 117726
90148 157176
34831 12213
60756 105488
121891 155192
82202 155666
102083 156661
38968 75200
190004 107321
72436 92732
64314 65004
39210 91106
70455 173568
179742 29844
126232 19552
133509 110602
131...

output:

20996806570330

result:

wrong answer 1st lines differ - expected: '20980030044349', found: '20996806570330'