#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#define dbg(...) printf(__VA_ARGS__);
#define getchar_unlocked getchar
#else
#define dbg(...)
#endif
#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)x.size()
#define mnto(x,y) x=min(x,(__typeof__(x))y)
#define mxto(x,y) x=max(x,(__typeof__(x))y)
#define INF 1023456789
#define LINF 1023456789123456789
#define all(x) x.begin(), x.end()
#define disc(x) sort(all(x));x.resize(unique(all(x))-x.begin());
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<pll> vll;
mt19937 rng(time(0));
#define mod 1000000007
inline int add(int a,int b){
int r=a+b;
while(r>=mod)r-=mod;
while(r<0)r+=mod;
return r;
}
inline int mult(int a,int b){
return (int)(((ll)(a*b))%mod);
}
inline int rd(){
int x=0;
char ch=getchar_unlocked();
while(!(ch&16))ch=getchar();//keep reading while current character is whitespace
while(ch&16){//this will break when ‘\n’ or ‘ ‘ is encountered
x=(x<<3)+(x<<1)+(ch&15);
ch=getchar_unlocked();
}
return x;
}
int n,m,q,par[505],rnk[505];
vector<pair<int,ii>> EL;
void init(){
for(int i=1;i<=n;++i)rnk[i]=1,par[i]=i;
}
int fp(int i){
return (par[i]==i)?i:par[i]=fp(par[i]);
}
void join(int x,int y){
x=fp(x),y=fp(y);
if(x==y)return;
if(rnk[x]<rnk[y])par[x]=y;
else par[y]=x;
if(rnk[x]==rnk[y])++rnk[x];
}
int main(){
sf("%d%d",&n,&m);
for(int i=0;i<m;++i){
int a,b,w;sf("%d%d%d",&a,&b,&w);
EL.pb({w,{a,b}});
}
sort(all(EL));
int cur=0;
sf("%d",&q);
for(int i=0;i<q;++i){
int x;sf("%d",&x);
while(cur!=m-1&&abs(EL[cur].fi-x)>=abs(EL[cur+1].fi-x))++cur;
init();
int l=cur,r=cur,take=0;
ll ans=0;
while(take!=n-1){
int w,a,b;
if(l==-1||(r!=m&&abs(EL[l].fi-x)>abs(EL[r].fi-x))){
w=EL[r].fi;
tie(a,b)=EL[r++].se;
}
else{
w=EL[l].fi;
tie(a,b)=EL[l--].se;
}
if(fp(a)==fp(b))continue;
take++;
join(a,b);
ans+=abs(w-x);
}
dbg("ans: ");
pf("%lld\n",ans);
}
}
Compilation message
reconstruction.cpp: In function 'int main()':
reconstruction.cpp:77:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | sf("%d%d",&n,&m);
| ^
reconstruction.cpp:79:15: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
79 | int a,b,w;sf("%d%d%d",&a,&b,&w);
| ^
reconstruction.cpp:84:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
84 | sf("%d",&q);
| ^
reconstruction.cpp:86:11: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
86 | int x;sf("%d",&x);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
1 ms |
304 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
1 ms |
304 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
34 ms |
3224 KB |
Output is correct |
21 |
Correct |
41 ms |
3212 KB |
Output is correct |
22 |
Correct |
37 ms |
3248 KB |
Output is correct |
23 |
Correct |
37 ms |
3260 KB |
Output is correct |
24 |
Correct |
36 ms |
3264 KB |
Output is correct |
25 |
Correct |
37 ms |
3264 KB |
Output is correct |
26 |
Correct |
35 ms |
3276 KB |
Output is correct |
27 |
Correct |
38 ms |
3224 KB |
Output is correct |
28 |
Correct |
37 ms |
3216 KB |
Output is correct |
29 |
Correct |
39 ms |
3240 KB |
Output is correct |
30 |
Correct |
35 ms |
3200 KB |
Output is correct |
31 |
Correct |
37 ms |
3232 KB |
Output is correct |
32 |
Correct |
35 ms |
3232 KB |
Output is correct |
33 |
Correct |
38 ms |
3268 KB |
Output is correct |
34 |
Correct |
45 ms |
3332 KB |
Output is correct |
35 |
Correct |
34 ms |
3244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
308 KB |
Output is correct |
4 |
Execution timed out |
5038 ms |
5336 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
1 ms |
304 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
20 |
Execution timed out |
5047 ms |
10564 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
1 ms |
304 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
34 ms |
3224 KB |
Output is correct |
21 |
Correct |
41 ms |
3212 KB |
Output is correct |
22 |
Correct |
37 ms |
3248 KB |
Output is correct |
23 |
Correct |
37 ms |
3260 KB |
Output is correct |
24 |
Correct |
36 ms |
3264 KB |
Output is correct |
25 |
Correct |
37 ms |
3264 KB |
Output is correct |
26 |
Correct |
35 ms |
3276 KB |
Output is correct |
27 |
Correct |
38 ms |
3224 KB |
Output is correct |
28 |
Correct |
37 ms |
3216 KB |
Output is correct |
29 |
Correct |
39 ms |
3240 KB |
Output is correct |
30 |
Correct |
35 ms |
3200 KB |
Output is correct |
31 |
Correct |
37 ms |
3232 KB |
Output is correct |
32 |
Correct |
35 ms |
3232 KB |
Output is correct |
33 |
Correct |
38 ms |
3268 KB |
Output is correct |
34 |
Correct |
45 ms |
3332 KB |
Output is correct |
35 |
Correct |
34 ms |
3244 KB |
Output is correct |
36 |
Correct |
645 ms |
3680 KB |
Output is correct |
37 |
Execution timed out |
5044 ms |
3680 KB |
Time limit exceeded |
38 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
1 ms |
304 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
34 ms |
3224 KB |
Output is correct |
21 |
Correct |
41 ms |
3212 KB |
Output is correct |
22 |
Correct |
37 ms |
3248 KB |
Output is correct |
23 |
Correct |
37 ms |
3260 KB |
Output is correct |
24 |
Correct |
36 ms |
3264 KB |
Output is correct |
25 |
Correct |
37 ms |
3264 KB |
Output is correct |
26 |
Correct |
35 ms |
3276 KB |
Output is correct |
27 |
Correct |
38 ms |
3224 KB |
Output is correct |
28 |
Correct |
37 ms |
3216 KB |
Output is correct |
29 |
Correct |
39 ms |
3240 KB |
Output is correct |
30 |
Correct |
35 ms |
3200 KB |
Output is correct |
31 |
Correct |
37 ms |
3232 KB |
Output is correct |
32 |
Correct |
35 ms |
3232 KB |
Output is correct |
33 |
Correct |
38 ms |
3268 KB |
Output is correct |
34 |
Correct |
45 ms |
3332 KB |
Output is correct |
35 |
Correct |
34 ms |
3244 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
0 ms |
308 KB |
Output is correct |
39 |
Execution timed out |
5038 ms |
5336 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |