QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#602851#9230. Routing K-CodesJDScript0117WA 72ms16472kbC++174.2kb2024-10-01 13:08:582024-10-01 13:09:19

Judging History

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

  • [2024-10-01 13:09:19]
  • 评测
  • 测评结果:WA
  • 用时:72ms
  • 内存:16472kb
  • [2024-10-01 13:08:58]
  • 提交

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(a.k, b.k));
}

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] - 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: 13836kb

input:

4 3
1 2
1 3
1 4

output:

6

result:

ok single line: '6'

Test #2:

score: 0
Accepted
time: 0ms
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: 9680kb

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: 47ms
memory: 14472kb

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: 0
Accepted
time: 72ms
memory: 16448kb

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:

20980030044349

result:

ok single line: '20980030044349'

Test #6:

score: 0
Accepted
time: 2ms
memory: 11800kb

input:

4 3
1 2
1 3
1 4

output:

6

result:

ok single line: '6'

Test #7:

score: 0
Accepted
time: 45ms
memory: 13764kb

input:

199021 199020
189105 111001
147300 187047
67395 102319
25317 152109
56495 115535
11656 19974
119178 6528
84571 159100
198873 156684
21161 163040
91720 165273
168305 140152
104884 119802
131 177991
35930 74934
27528 278
177640 162738
118439 69472
20365 85043
184995 54207
64542 188847
97881 167717
188...

output:

NIE

result:

ok single line: 'NIE'

Test #8:

score: 0
Accepted
time: 52ms
memory: 16472kb

input:

200000 199999
145608 31176
94180 155303
112924 85699
196865 176102
34049 30841
84924 191665
164317 97582
10102 125492
114493 20622
127660 194591
183851 21461
167689 53839
59189 126425
135853 79950
173910 4196
8134 182272
42157 63799
5109 182005
197391 154014
93467 130380
1508 66644
129681 199910
677...

output:

25146839515965

result:

ok single line: '25146839515965'

Test #9:

score: 0
Accepted
time: 58ms
memory: 16464kb

input:

200000 199999
160386 189041
24452 73297
75992 87415
87012 116413
18645 2219
151007 100916
87623 77520
51217 45179
51633 67938
183428 99891
74684 129965
186795 70345
28743 22633
107782 28087
117185 78477
46846 176763
18968 80952
118201 35872
123906 140127
65784 66684
24802 134847
42591 150517
27123 1...

output:

9894117829832

result:

ok single line: '9894117829832'

Test #10:

score: 0
Accepted
time: 65ms
memory: 16452kb

input:

200000 199999
42958 26294
73743 94861
161036 525
22581 6649
152064 106483
126795 178801
147721 107972
43433 197974
75281 163319
170596 167054
180443 168322
1443 163261
197722 164837
144573 16585
6005 143774
195029 188032
112864 105465
108330 154314
138435 103667
66734 146178
15416 123293
22322 10216...

output:

28756428378750

result:

ok single line: '28756428378750'

Test #11:

score: 0
Accepted
time: 29ms
memory: 10472kb

input:

199000 199099
149311 147296
89797 115798
124184 88539
170531 79689
134688 98182
53272 56612
68205 106798
156914 76119
158177 131
29557 179794
35380 78679
72756 116830
122836 23339
33434 147647
193860 131424
52240 110141
166223 94852
47072 83029
156709 75295
184679 174874
52901 95437
137870 130531
58...

output:

NIE

result:

ok single line: 'NIE'

Test #12:

score: 0
Accepted
time: 49ms
memory: 14628kb

input:

199999 199998
27735 110383
157138 117109
21107 95911
27141 58936
1514 195135
106403 63473
66162 42639
21681 114598
53954 191710
110249 143780
40636 40124
11560 51678
2778 182082
60435 105451
198676 123935
67639 115704
176195 189982
52257 8366
99743 141742
147381 28925
190341 131308
159677 5040
6138 ...

output:

NIE

result:

ok single line: 'NIE'

Test #13:

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

input:

64 63
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 52
52 53...

output:

NIE

result:

ok single line: 'NIE'

Test #14:

score: 0
Accepted
time: 5ms
memory: 13132kb

input:

40030 40029
27049 14668
38975 18442
35811 5302
17535 12299
15998 29692
2477 9551
30589 13238
21558 20519
14120 37711
3010 28910
20472 29630
20470 30673
20684 12059
34197 17012
11419 29902
27976 6142
17829 29836
17033 1563
28681 11276
15975 4662
35715 1653
4390 29700
20484 19601
35245 22179
35694 105...

output:

7876944195050

result:

ok single line: '7876944195050'

Test #15:

score: 0
Accepted
time: 65ms
memory: 16416kb

input:

200000 199999
194326 144191
182448 172442
33303 58743
195157 142391
124650 181319
48631 75861
194887 75085
871 75209
128403 22324
1911 96598
172868 55033
158652 188139
96224 69860
69359 69490
184520 11792
191959 42157
131482 19144
107935 150093
67117 41377
9963 116441
65600 112397
64802 13426
37419 ...

output:

23606567643085

result:

ok single line: '23606567643085'

Test #16:

score: 0
Accepted
time: 58ms
memory: 16444kb

input:

200000 199999
52436 11641
79984 114804
111668 33543
137247 48090
9645 65795
184747 39611
11610 159313
22638 145697
63222 136367
32014 151563
93890 110893
116729 104704
156341 117458
98118 56463
18222 199014
16706 14372
187937 105132
117178 156199
38056 104887
72658 192287
171805 72070
114790 84610
1...

output:

23902024445030

result:

ok single line: '23902024445030'

Test #17:

score: 0
Accepted
time: 31ms
memory: 8800kb

input:

199000 199000
56063 29441
95944 92411
160503 142093
170757 96332
25224 46198
58983 137544
22176 91206
112508 87842
147759 138236
64654 75242
89467 126313
169923 41227
76585 85194
105242 18476
155335 62578
90527 108821
4035 170280
185455 189112
132037 25888
54529 101925
157500 26166
108961 86259
9757...

output:

NIE

result:

ok single line: 'NIE'

Test #18:

score: -100
Wrong Answer
time: 1ms
memory: 5696kb

input:

1 0

output:

1000000000000000000

result:

wrong answer 1st lines differ - expected: '0', found: '1000000000000000000'