#include <bits/stdc++.h>
using namespace std;
int p[505];
int rt(int a){return p[a]==a?a:p[a]=rt(p[a]);}
int A[100005];
int B[100005];
int W[100005];
int n;
struct node{
int s,e;
vector<int> usefulmin,usefulmax;
node *l,*r;
node (int _s, int _e){
s = _s; e = _e;
if (s!=e){
l = new node(s,(s+e)/2);
r = new node((s+e)/2+1,e);
}
}
void construct(){
for (int x = 1; x<=n; x++){
p[x] = x;
}
for (int x = s; x<=e; x++){
if (rt(A[x])!=rt(B[x])){
usefulmin.push_back(x);
p[rt(A[x])] = rt(B[x]);
}
}
for (int x = 1; x<=n; x++){
p[x] = x;
}
for (int x = e; x>=s; x--){
if (rt(A[x])!=rt(B[x])){
usefulmax.push_back(x);
p[rt(A[x])] = rt(B[x]);
}
}
if (s==e) return;
l->construct();
r->construct();
}
void qu(vector<int> &low, vector<int> &high, int val){
//printf("qu %d-%d, %d, %d %d\n",s,e,val,W[s],W[e]);
if (W[s]>val){
for (auto x : usefulmin){
//printf("high add %d\n",x);
high.push_back(x);
}
return;
}
if (W[e]<=val){
for (auto x : usefulmax){
//printf("low add %d\n",x);
low.push_back(x);
}
return;
}
if (W[(s+e)/2]>=val){
l->qu(low,high,val);
r->qu(low,high,val);
}
else{
r->qu(low,high,val);
l->qu(low,high,val);
}
}
}*root;
int curval;
bool cmp1(int a, int b){
return abs(W[a]-curval)<abs(W[b]-curval);
}
main(){
scanf("%d",&n);
int m;
scanf("%d",&m);
vector<pair<int,pair<int,int>>> edgel;
for (int x = 0; x<m; x++){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
edgel.push_back({c,{a,b}});
}
sort(edgel.begin(),edgel.end());
for (int x = 0; x<m; x++){
A[x] = edgel[x].second.first;
B[x] = edgel[x].second.second;
W[x] = edgel[x].first;
}
root = new node(0,m-1);
root->construct();
int q;
scanf("%d",&q);
while (q--){
int v;
scanf("%d",&v);
vector<int> v1,v2;
root->qu(v1,v2,v);
//printf("qu fin\n");
curval = v;
sort(v1.begin(),v1.end(),cmp1);
sort(v2.begin(),v2.end(),cmp1);
int c1 = 0;
int c2 = 0;
long long ans = 0;
for (int x = 1; x<=n; x++) p[x] = x;
while (c1<v1.size() || c2<v2.size()){
//printf("%d %d\n",c1,c2);
if (c2==v2.size() || c1<v1.size() && v-W[v1[c1]]<W[v2[c2]]-v){
if (rt(A[v1[c1]])!=rt(B[v1[c1]])){
ans += v-W[v1[c1]];
p[rt(A[v1[c1]])] = rt(B[v1[c1]]);
}
c1++;
}
else{
if (rt(A[v2[c2]])!=rt(B[v2[c2]])){
ans += W[v2[c2]]-v;
p[rt(A[v2[c2]])] = rt(B[v2[c2]]);
}
c2++;
}
}
printf("%lld\n",ans);
}
}
Compilation message
reconstruction.cpp:78:2: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
78 | main(){
| ^~~~
reconstruction.cpp: In function 'int main()':
reconstruction.cpp:111:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
111 | while (c1<v1.size() || c2<v2.size()){
| ~~^~~~~~~~~~
reconstruction.cpp:111:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
111 | while (c1<v1.size() || c2<v2.size()){
| ~~^~~~~~~~~~
reconstruction.cpp:113:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
113 | if (c2==v2.size() || c1<v1.size() && v-W[v1[c1]]<W[v2[c2]]-v){
| ~~^~~~~~~~~~~
reconstruction.cpp:113:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
113 | if (c2==v2.size() || c1<v1.size() && v-W[v1[c1]]<W[v2[c2]]-v){
| ~~^~~~~~~~~~
reconstruction.cpp:113:47: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
113 | if (c2==v2.size() || c1<v1.size() && v-W[v1[c1]]<W[v2[c2]]-v){
| ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
reconstruction.cpp:79:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
79 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
reconstruction.cpp:81:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
81 | scanf("%d",&m);
| ~~~~~^~~~~~~~~
reconstruction.cpp:85:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
85 | scanf("%d%d%d",&a,&b,&c);
| ~~~~~^~~~~~~~~~~~~~~~~~~
reconstruction.cpp:97:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
97 | scanf("%d",&q);
| ~~~~~^~~~~~~~~
reconstruction.cpp:100:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
100 | scanf("%d",&v);
| ~~~~~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
214 ms |
38008 KB |
Output is correct |
21 |
Correct |
204 ms |
38204 KB |
Output is correct |
22 |
Correct |
254 ms |
38248 KB |
Output is correct |
23 |
Correct |
223 ms |
38216 KB |
Output is correct |
24 |
Correct |
227 ms |
38224 KB |
Output is correct |
25 |
Correct |
236 ms |
38292 KB |
Output is correct |
26 |
Correct |
222 ms |
38284 KB |
Output is correct |
27 |
Correct |
310 ms |
38240 KB |
Output is correct |
28 |
Correct |
271 ms |
38280 KB |
Output is correct |
29 |
Correct |
269 ms |
37932 KB |
Output is correct |
30 |
Correct |
229 ms |
38308 KB |
Output is correct |
31 |
Correct |
225 ms |
38304 KB |
Output is correct |
32 |
Correct |
232 ms |
38224 KB |
Output is correct |
33 |
Correct |
217 ms |
38304 KB |
Output is correct |
34 |
Correct |
226 ms |
37876 KB |
Output is correct |
35 |
Correct |
223 ms |
38312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Execution timed out |
5010 ms |
38508 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Execution timed out |
5079 ms |
2052 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
214 ms |
38008 KB |
Output is correct |
21 |
Correct |
204 ms |
38204 KB |
Output is correct |
22 |
Correct |
254 ms |
38248 KB |
Output is correct |
23 |
Correct |
223 ms |
38216 KB |
Output is correct |
24 |
Correct |
227 ms |
38224 KB |
Output is correct |
25 |
Correct |
236 ms |
38292 KB |
Output is correct |
26 |
Correct |
222 ms |
38284 KB |
Output is correct |
27 |
Correct |
310 ms |
38240 KB |
Output is correct |
28 |
Correct |
271 ms |
38280 KB |
Output is correct |
29 |
Correct |
269 ms |
37932 KB |
Output is correct |
30 |
Correct |
229 ms |
38308 KB |
Output is correct |
31 |
Correct |
225 ms |
38304 KB |
Output is correct |
32 |
Correct |
232 ms |
38224 KB |
Output is correct |
33 |
Correct |
217 ms |
38304 KB |
Output is correct |
34 |
Correct |
226 ms |
37876 KB |
Output is correct |
35 |
Correct |
223 ms |
38312 KB |
Output is correct |
36 |
Execution timed out |
5005 ms |
38500 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
214 ms |
38008 KB |
Output is correct |
21 |
Correct |
204 ms |
38204 KB |
Output is correct |
22 |
Correct |
254 ms |
38248 KB |
Output is correct |
23 |
Correct |
223 ms |
38216 KB |
Output is correct |
24 |
Correct |
227 ms |
38224 KB |
Output is correct |
25 |
Correct |
236 ms |
38292 KB |
Output is correct |
26 |
Correct |
222 ms |
38284 KB |
Output is correct |
27 |
Correct |
310 ms |
38240 KB |
Output is correct |
28 |
Correct |
271 ms |
38280 KB |
Output is correct |
29 |
Correct |
269 ms |
37932 KB |
Output is correct |
30 |
Correct |
229 ms |
38308 KB |
Output is correct |
31 |
Correct |
225 ms |
38304 KB |
Output is correct |
32 |
Correct |
232 ms |
38224 KB |
Output is correct |
33 |
Correct |
217 ms |
38304 KB |
Output is correct |
34 |
Correct |
226 ms |
37876 KB |
Output is correct |
35 |
Correct |
223 ms |
38312 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Execution timed out |
5010 ms |
38508 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |