QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#410594#6662. 기지 간소화nguyentunglam5 47ms18012kbC++171.9kb2024-05-14 10:14:162024-05-14 10:14:17

Judging History

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

  • [2024-05-14 10:14:17]
  • 评测
  • 测评结果:5
  • 用时:47ms
  • 内存:18012kb
  • [2024-05-14 10:14:16]
  • 提交

answer

#include<bits/stdc++.h>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;

const int N = 3e5 + 10, mod = 1e9 + 7;

int st[N], ed[N], rev_st[N], dd[N], cnt[2], sz[N], cost[N];

vector<pair<int, int> > adj[N];

int timer, n;

long long ans;

void prep (int u, int p) {
//  cout << u << endl;
  st[u] = ++timer;
  rev_st[timer] = u;
  sz[u] = 1;
  for(auto &[v, c] : adj[u]) if (v != p) {
    cost[v] = c;
    prep(v, u);
    sz[u] += sz[v];
  }
  ed[u] = timer;
}

void dfs(int u, int p) {
  int heavy = 0;
//  cout << u << endl;
  for(auto &[v, w] : adj[u]) if (v != p && sz[v] > sz[heavy]) heavy = v;
  for(auto &[v, w] : adj[u]) if (v != p && v != heavy) {
    dfs(v, u);
    for(int j = st[v]; j <= ed[v]; j++) {
      dd[rev_st[j]] = 0;
//      if (u == 2) cout << rev_st[j] << endl;
    }
  }
  if (heavy) dfs(heavy, u);
  dd[u] = 1;
  for(auto &[v, w] : adj[u]) if (v != p && v != heavy) {
    for(int j = st[v]; j <= ed[v]; j++) dd[rev_st[j]] = 1;
  }
//  if (u == 2) {
//    for(int i = 0; i < n; i++) cout << dd[i] << " "; cout << endl;
//    cout << u << endl;
////  }
  for(int l = 0; l < n; l++) {
    cnt[0] = cnt[1] = 0;
    for(int r = l; r < n; r++) {
      cnt[dd[r]] = 1;
      if (cnt[0] && cnt[1]) {
        ans += cost[u];
        ans %= mod;
//        cout << l << " " << r << " " << u << " " << cost[u] << endl;
      }
    }
  }
}

int maintenance_costs_sum (vector<int> U, vector<int> V, vector<int> W) {
  n = U.size() + 1;
  for(int i = 0; i < n - 1; i++) {
    adj[U[i]].emplace_back(V[i], W[i]);
    adj[V[i]].emplace_back(U[i], W[i]);
  }

  prep(0, 0);

  dfs(0, 0);

  return ans;
}

#ifdef ngu
int main() {

  freopen ("task.inp", "r", stdin);
  freopen ("task.out", "w", stdout);

  int tot = maintenance_costs_sum({0, 2, 2, 0}, {2, 1, 4, 3}, {2, 1, 1, 1});
  cout << tot << endl;
}
#endif // ngu


详细

Subtask #1:

score: 5
Accepted

Test #1:

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

input:

2
1 0 1

output:

1

result:

ok single line: '1'

Test #2:

score: 0
Accepted
time: 47ms
memory: 17188kb

input:

299
72 263 662908099
170 230 583287081
181 87 461245480
117 116 41098264
282 218 300936390
238 128 561969086
175 105 305200697
152 28 927649982
211 58 290232523
233 188 304617152
246 74 61325892
283 120 724838352
207 94 123021801
138 241 893659708
171 283 82846115
104 250 142703714
111 63 547249068
...

output:

93964028

result:

ok single line: '93964028'

Test #3:

score: 0
Accepted
time: 47ms
memory: 15884kb

input:

300
116 249 999999948
23 182 999999956
174 147 999999978
21 178 999999981
138 238 999999941
215 6 999999955
190 256 999999990
107 203 999999992
142 221 999999943
129 236 999999957
55 154 999999992
183 35 999999979
200 284 999999958
36 222 999999988
88 62 999999960
219 222 999999950
95 38 999999960
2...

output:

534442086

result:

ok single line: '534442086'

Test #4:

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

input:

5
0 2 2
2 1 3
2 4 6
0 3 5

output:

98

result:

ok single line: '98'

Test #5:

score: 0
Accepted
time: 3ms
memory: 18012kb

input:

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

output:

1625

result:

ok single line: '1625'

Test #6:

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

input:

10
2 5 242840396
4 7 425058513
7 9 975802175
7 1 647540462
3 7 239781206
2 0 873867121
4 0 132645393
6 4 862552539
0 8 542726039

output:

711424766

result:

ok single line: '711424766'

Test #7:

score: 0
Accepted
time: 32ms
memory: 16200kb

input:

299
107 270 74
187 221 79
142 129 40
77 272 27
94 152 1
117 171 74
64 79 87
263 36 91
257 176 1
183 191 82
109 67 23
91 90 30
116 228 76
276 152 15
59 165 40
160 235 71
235 116 82
102 44 88
130 289 72
241 150 14
62 176 23
277 50 83
206 38 11
66 250 72
84 120 66
137 259 54
46 287 82
209 263 57
151 24...

output:

399821685

result:

ok single line: '399821685'

Test #8:

score: 0
Accepted
time: 30ms
memory: 17236kb

input:

300
111 245 844422933
103 173 980761620
210 18 509066965
36 51 134507716
264 28 523343821
249 88 748193505
30 46 832843261
286 33 489900279
125 288 575144383
106 87 191375234
259 126 882094013
10 57 842095866
73 65 254207339
175 280 353530327
21 145 883665290
130 110 818300025
137 48 259121083
0 102...

output:

156555229

result:

ok single line: '156555229'

Test #9:

score: 0
Accepted
time: 32ms
memory: 17636kb

input:

292
37 89 397871374
184 205 437399619
14 234 93902029
40 155 84065200
12 60 367994608
143 35 623301168
125 0 307800516
68 281 653696611
146 260 978361615
177 79 385187016
251 192 369734594
245 159 471356654
109 53 96376804
248 103 681417305
85 288 837771859
25 110 145156090
129 46 874442695
252 249 ...

output:

702681209

result:

ok single line: '702681209'

Test #10:

score: 0
Accepted
time: 37ms
memory: 17260kb

input:

285
93 162 959772147
32 172 751202610
173 257 284177666
199 112 94146918
80 117 514639361
245 265 282507802
135 109 829039102
53 38 832201899
166 245 984506327
247 224 843352696
4 98 415992963
189 33 989751728
117 153 801360684
106 213 848238622
119 77 300949830
177 29 951903134
110 27 991808126
102...

output:

281677588

result:

ok single line: '281677588'

Test #11:

score: 0
Accepted
time: 44ms
memory: 15924kb

input:

300
238 117 820410457
230 235 506467445
200 74 653603782
62 76 485420791
105 275 392365065
20 121 28250604
203 87 723682049
109 151 342880635
133 79 302983289
47 297 671031835
257 299 444969934
255 116 860542983
91 92 332672418
205 280 290258390
103 160 769766288
106 137 789215359
159 204 831106721
...

output:

144479203

result:

ok single line: '144479203'

Test #12:

score: 0
Accepted
time: 43ms
memory: 17240kb

input:

298
157 32 579243060
277 30 236601044
95 45 746850490
123 94 873131928
185 95 376156675
66 150 606904039
262 124 953899592
219 279 373193513
152 99 546195756
113 100 936194271
216 240 22619249
38 110 395200927
89 55 159263634
265 77 151240149
90 232 529211969
88 177 115952104
43 208 657967542
196 45...

output:

75917669

result:

ok single line: '75917669'

Test #13:

score: 0
Accepted
time: 26ms
memory: 17108kb

input:

300
86 72 35281851
86 65 772642113
86 85 267144561
68 86 237759697
225 86 275794715
188 86 708990293
86 224 212292280
61 86 139649599
141 86 852035124
131 86 522883805
62 86 374119946
86 107 661877081
218 86 365176681
14 86 676939669
86 254 678988772
86 189 741173114
86 175 101646645
86 10 776575255...

output:

970519677

result:

ok single line: '970519677'

Test #14:

score: 0
Accepted
time: 26ms
memory: 16400kb

input:

296
167 280 308339229
42 120 44980047
172 173 31087131
280 172 346584469
68 172 765593132
172 11 55592625
172 60 294200085
172 177 829489003
172 26 368114672
172 6 906646562
239 172 311081319
199 172 89488635
264 172 782569720
172 53 614220967
172 192 111994073
70 172 348033132
172 37 5375089
10 108...

output:

685676493

result:

ok single line: '685676493'

Test #15:

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

input:

299
219 189 428591428
214 280 645468138
239 124 44092787
278 109 973174819
229 28 708387042
118 105 231265201
106 23 115324206
127 163 182711436
61 68 171248721
67 218 585337538
198 30 576082156
147 49 179613791
109 244 356639356
122 205 714546250
179 207 831521336
292 188 754667151
91 55 962847082
...

output:

442304238

result:

ok single line: '442304238'

Test #16:

score: 0
Accepted
time: 35ms
memory: 17432kb

input:

300
72 138 240810351
80 111 534427405
105 137 731389559
86 95 740641088
86 126 67440531
151 147 975723723
84 98 889126297
60 37 235430076
98 236 462897489
183 146 446541268
42 135 730090491
92 259 6083997
189 161 972025910
3 29 546634976
53 270 249844443
28 122 103308828
105 275 560940747
121 124 94...

output:

51045998

result:

ok single line: '51045998'

Test #17:

score: 0
Accepted
time: 34ms
memory: 17440kb

input:

298
15 284 446739499
274 218 9244552
64 69 913186698
79 63 70124148
253 46 192851114
165 30 798777080
30 123 824050137
199 104 363328962
23 70 471441
170 54 206153629
18 49 203147626
83 136 841021519
158 184 342247212
72 139 144579819
237 6 976320498
235 224 141859373
169 39 844270142
231 273 615291...

output:

874031476

result:

ok single line: '874031476'

Test #18:

score: 0
Accepted
time: 42ms
memory: 16720kb

input:

300
211 34 999999974
255 27 999999997
50 98 999999974
16 21 999999946
207 243 999999999
39 64 999999983
279 79 999999975
95 248 999999960
69 276 999999977
94 6 999999960
137 281 999999969
127 49 999999945
122 72 999999958
124 63 999999991
125 52 999999963
152 92 999999994
26 120 999999989
29 275 999...

output:

558463020

result:

ok single line: '558463020'

Subtask #2:

score: 0
Time Limit Exceeded

Dependency #1:

100%
Accepted

Test #19:

score: 0
Time Limit Exceeded

input:

3987
1834 2601 895910969
1701 1884 820508615
3406 1155 439833658
2977 3131 370614789
617 2254 829077229
1648 1576 977756493
597 2928 624126121
1165 3147 11258087
3888 2644 341340941
3007 2676 281856885
3971 957 644422867
3791 1181 166275464
3089 2273 36236019
1918 1080 747406617
3032 2602 102478217
...

output:

Unauthorized output

result:


Subtask #3:

score: 0
Time Limit Exceeded

Test #36:

score: 0
Time Limit Exceeded

input:

249494
137621 137622 1
198127 198128 1
117358 117359 1
155777 155778 1
112742 112743 1
235668 235669 1
20632 20622 1
41333 41334 1
170699 170692 1
130269 130268 1
137208 137207 1
37791 37767 1
130187 130178 1
89795 89817 1
91408 91359 1
243301 243574 1
22017 22018 1
116466 116467 1
160094 160095 1
4...

output:

Unauthorized output

result:


Subtask #4:

score: 0
Time Limit Exceeded

Test #48:

score: 0
Time Limit Exceeded

input:

249876
133760 107716 545485826
57898 35556 542825636
159559 78814 588304799
9037 105623 735470824
242676 240955 258616989
58653 143501 194781066
36591 97835 699376285
95049 123298 35300031
91751 203920 865003045
53908 18382 376452723
211462 200538 638209824
48693 89388 132147210
238496 151742 322726...

output:

Unauthorized output

result:


Subtask #5:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

0%