#pragma GCC optimize("Ofast","O3","unroll-loops")
#pragma GCC target("avx","avx2")
#include <bits/stdc++.h>
using namespace std;
#define LCBorz ios_base::sync_with_stdio(false); cin.tie(0);
#define ll long long
#define all(x) x.begin(), x.end()
#define endl '\n'
const int N=505;
const int M=1000005;
const ll INF=1e9;
struct DSU{
vector<int> pa,sz;
DSU(int n){
pa.resize(n);
sz.resize(n,1);
iota(all(pa),0);
}
int Find(int k){
return (k==pa[k]?k:pa[k]=Find(pa[k]));
}
void onion(int a,int b){
a=Find(a),b=Find(b);
if(a==b)return;
if(sz[a]>sz[b]){
swap(a,b);
}
pa[a]=b;
sz[b]+=sz[a];
}
int same(int a,int b){
return Find(a)==Find(b);
}
};
vector<int> e(M);
set<int> adj[N];
int get_mn(int k,int pa,int goal){
if(k==goal){
return INF;
}
for(int j:adj[k]){
int to=k^e[j];
if(to==pa)continue;
int tmp=get_mn(to,k,goal);
if(tmp!=-1){
return min(tmp,j);
}
}
return -1;
}
int get_mx(int k,int pa,int goal){
if(k==goal){
return -INF;
}
for(int j:adj[k]){
int to=k^e[j];
if(to==pa)continue;
int tmp=get_mx(to,k,goal);
if(tmp!=-1){
return max(tmp,j);
}
}
return -1;
}
int32_t main() {
LCBorz;
int n,m;cin>>n>>m;
vector<array<int,3>> v(m);
for(int i=0;i<m;i++){
cin>>v[i][1]>>v[i][2]>>v[i][0];
}
sort(all(v));
for(int i=0;i<m;i++){
e[i]=v[i][1]^v[i][2];
}
vector<int> l[m],r[m];
vector<int> t;
for(int i=0;i<m;i++){
int id=get_mn(v[i][1],v[i][1],v[i][2]);
if(id!=-1){
adj[v[id][1]].erase(id);
adj[v[id][2]].erase(id);
for(auto it=t.begin();it!=t.end();it++){
if(*it==id){
t.erase(it);
break;
}
}
}
t.push_back(i);
adj[v[i][1]].insert(i);
adj[v[i][2]].insert(i);
l[i]=t;
sort(all(l[i]),greater<int>());
}
t.clear();
for(int i=1;i<=n;i++){
adj[i].clear();
}
for(int i=m-1;i>=0;i--){
int id=get_mx(v[i][1],v[i][1],v[i][2]);
if(id!=-1){
adj[v[id][1]].erase(id);
adj[v[id][2]].erase(id);
for(auto it=t.begin();it!=t.end();it++){
if(*it==id){
t.erase(it);
break;
}
}
}
t.push_back(i);
adj[v[i][1]].insert(i);
adj[v[i][2]].insert(i);
r[i]=t;
sort(all(r[i]));
}
int ptr=0;
int q;cin>>q;
ll ta=0,tb=0;
for(int q1=0;q1<q;q1++){
int x;cin>>x;
while(ptr<m-1&&v[ptr][0]<=x){
ptr++;
}
DSU dsu(n+1);
ll ans=0;
int l1=0,r1=0,cnt=0;
int ptr1=max(0,ptr-1);
while(l1<l[ptr1].size()&&r1<r[ptr].size()&&cnt<n-1){
if(abs(x-v[l[ptr1][l1]][0])<abs(x-v[r[ptr][r1]][0])){
auto [a1,b1,c1]=v[l[ptr1][l1]];
if(!dsu.same(b1,c1)){
dsu.onion(b1,c1);
ans+=abs(x-a1);
cnt++;
}
l1++;
}
else{
auto [a1,b1,c1]=v[r[ptr][r1]];
if(!dsu.same(b1,c1)){
dsu.onion(b1,c1);
ans+=abs(x-a1);
cnt++;
}
r1++;
}
}
while(cnt<n-1&&l1<l[ptr1].size()){
auto [a1,b1,c1]=v[l[ptr1][l1]];
if(!dsu.same(b1,c1)){
dsu.onion(b1,c1);
ans+=abs(x-a1);
cnt++;
}
l1++;
}
while(cnt<n-1&&r1<r[ptr].size()){
auto [a1,b1,c1]=v[r[ptr][r1]];
if(!dsu.same(b1,c1)){
dsu.onion(b1,c1);
ans+=abs(x-a1);
cnt++;
}
r1++;
}
cout<<ans<<endl;
}
return 0;
}
Compilation message
reconstruction.cpp: In function 'int32_t main()':
reconstruction.cpp:130:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
130 | while(l1<l[ptr1].size()&&r1<r[ptr].size()&&cnt<n-1){
| ~~^~~~~~~~~~~~~~~
reconstruction.cpp:130:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
130 | while(l1<l[ptr1].size()&&r1<r[ptr].size()&&cnt<n-1){
| ~~^~~~~~~~~~~~~~
reconstruction.cpp:150:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
150 | while(cnt<n-1&&l1<l[ptr1].size()){
| ~~^~~~~~~~~~~~~~~
reconstruction.cpp:159:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
159 | while(cnt<n-1&&r1<r[ptr].size()){
| ~~^~~~~~~~~~~~~~
reconstruction.cpp:120:8: warning: unused variable 'ta' [-Wunused-variable]
120 | ll ta=0,tb=0;
| ^~
reconstruction.cpp:120:13: warning: unused variable 'tb' [-Wunused-variable]
120 | ll ta=0,tb=0;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4188 KB |
Output is correct |
2 |
Correct |
1 ms |
4188 KB |
Output is correct |
3 |
Correct |
1 ms |
4188 KB |
Output is correct |
4 |
Correct |
1 ms |
4188 KB |
Output is correct |
5 |
Correct |
2 ms |
4236 KB |
Output is correct |
6 |
Correct |
1 ms |
4184 KB |
Output is correct |
7 |
Correct |
2 ms |
4188 KB |
Output is correct |
8 |
Correct |
1 ms |
4188 KB |
Output is correct |
9 |
Correct |
1 ms |
4188 KB |
Output is correct |
10 |
Correct |
1 ms |
4188 KB |
Output is correct |
11 |
Correct |
2 ms |
4404 KB |
Output is correct |
12 |
Correct |
1 ms |
4188 KB |
Output is correct |
13 |
Correct |
1 ms |
4192 KB |
Output is correct |
14 |
Correct |
1 ms |
4200 KB |
Output is correct |
15 |
Correct |
1 ms |
4396 KB |
Output is correct |
16 |
Correct |
1 ms |
4200 KB |
Output is correct |
17 |
Correct |
1 ms |
4200 KB |
Output is correct |
18 |
Correct |
2 ms |
4200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4188 KB |
Output is correct |
2 |
Correct |
1 ms |
4188 KB |
Output is correct |
3 |
Correct |
1 ms |
4188 KB |
Output is correct |
4 |
Correct |
1 ms |
4188 KB |
Output is correct |
5 |
Correct |
2 ms |
4236 KB |
Output is correct |
6 |
Correct |
1 ms |
4184 KB |
Output is correct |
7 |
Correct |
2 ms |
4188 KB |
Output is correct |
8 |
Correct |
1 ms |
4188 KB |
Output is correct |
9 |
Correct |
1 ms |
4188 KB |
Output is correct |
10 |
Correct |
1 ms |
4188 KB |
Output is correct |
11 |
Correct |
2 ms |
4404 KB |
Output is correct |
12 |
Correct |
1 ms |
4188 KB |
Output is correct |
13 |
Correct |
1 ms |
4192 KB |
Output is correct |
14 |
Correct |
1 ms |
4200 KB |
Output is correct |
15 |
Correct |
1 ms |
4396 KB |
Output is correct |
16 |
Correct |
1 ms |
4200 KB |
Output is correct |
17 |
Correct |
1 ms |
4200 KB |
Output is correct |
18 |
Correct |
2 ms |
4200 KB |
Output is correct |
19 |
Correct |
1 ms |
4200 KB |
Output is correct |
20 |
Correct |
2174 ms |
405576 KB |
Output is correct |
21 |
Correct |
1238 ms |
383512 KB |
Output is correct |
22 |
Correct |
1534 ms |
400136 KB |
Output is correct |
23 |
Correct |
1690 ms |
404272 KB |
Output is correct |
24 |
Correct |
1771 ms |
405020 KB |
Output is correct |
25 |
Correct |
1997 ms |
405104 KB |
Output is correct |
26 |
Correct |
2044 ms |
405180 KB |
Output is correct |
27 |
Correct |
2009 ms |
405332 KB |
Output is correct |
28 |
Correct |
2106 ms |
405152 KB |
Output is correct |
29 |
Correct |
1564 ms |
398384 KB |
Output is correct |
30 |
Correct |
2106 ms |
405100 KB |
Output is correct |
31 |
Correct |
2084 ms |
405080 KB |
Output is correct |
32 |
Correct |
2036 ms |
405332 KB |
Output is correct |
33 |
Correct |
2103 ms |
405316 KB |
Output is correct |
34 |
Correct |
1062 ms |
340308 KB |
Output is correct |
35 |
Correct |
2045 ms |
405176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4188 KB |
Output is correct |
2 |
Correct |
1 ms |
4188 KB |
Output is correct |
3 |
Correct |
2 ms |
4188 KB |
Output is correct |
4 |
Execution timed out |
5110 ms |
409896 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4188 KB |
Output is correct |
2 |
Correct |
1 ms |
4188 KB |
Output is correct |
3 |
Correct |
1 ms |
4188 KB |
Output is correct |
4 |
Correct |
1 ms |
4188 KB |
Output is correct |
5 |
Correct |
2 ms |
4236 KB |
Output is correct |
6 |
Correct |
1 ms |
4184 KB |
Output is correct |
7 |
Correct |
2 ms |
4188 KB |
Output is correct |
8 |
Correct |
1 ms |
4188 KB |
Output is correct |
9 |
Correct |
1 ms |
4188 KB |
Output is correct |
10 |
Correct |
1 ms |
4188 KB |
Output is correct |
11 |
Correct |
2 ms |
4404 KB |
Output is correct |
12 |
Correct |
1 ms |
4188 KB |
Output is correct |
13 |
Correct |
1 ms |
4192 KB |
Output is correct |
14 |
Correct |
1 ms |
4200 KB |
Output is correct |
15 |
Correct |
1 ms |
4396 KB |
Output is correct |
16 |
Correct |
1 ms |
4200 KB |
Output is correct |
17 |
Correct |
1 ms |
4200 KB |
Output is correct |
18 |
Correct |
2 ms |
4200 KB |
Output is correct |
19 |
Correct |
1 ms |
4188 KB |
Output is correct |
20 |
Execution timed out |
5023 ms |
23780 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4188 KB |
Output is correct |
2 |
Correct |
1 ms |
4188 KB |
Output is correct |
3 |
Correct |
1 ms |
4188 KB |
Output is correct |
4 |
Correct |
1 ms |
4188 KB |
Output is correct |
5 |
Correct |
2 ms |
4236 KB |
Output is correct |
6 |
Correct |
1 ms |
4184 KB |
Output is correct |
7 |
Correct |
2 ms |
4188 KB |
Output is correct |
8 |
Correct |
1 ms |
4188 KB |
Output is correct |
9 |
Correct |
1 ms |
4188 KB |
Output is correct |
10 |
Correct |
1 ms |
4188 KB |
Output is correct |
11 |
Correct |
2 ms |
4404 KB |
Output is correct |
12 |
Correct |
1 ms |
4188 KB |
Output is correct |
13 |
Correct |
1 ms |
4192 KB |
Output is correct |
14 |
Correct |
1 ms |
4200 KB |
Output is correct |
15 |
Correct |
1 ms |
4396 KB |
Output is correct |
16 |
Correct |
1 ms |
4200 KB |
Output is correct |
17 |
Correct |
1 ms |
4200 KB |
Output is correct |
18 |
Correct |
2 ms |
4200 KB |
Output is correct |
19 |
Correct |
1 ms |
4200 KB |
Output is correct |
20 |
Correct |
2174 ms |
405576 KB |
Output is correct |
21 |
Correct |
1238 ms |
383512 KB |
Output is correct |
22 |
Correct |
1534 ms |
400136 KB |
Output is correct |
23 |
Correct |
1690 ms |
404272 KB |
Output is correct |
24 |
Correct |
1771 ms |
405020 KB |
Output is correct |
25 |
Correct |
1997 ms |
405104 KB |
Output is correct |
26 |
Correct |
2044 ms |
405180 KB |
Output is correct |
27 |
Correct |
2009 ms |
405332 KB |
Output is correct |
28 |
Correct |
2106 ms |
405152 KB |
Output is correct |
29 |
Correct |
1564 ms |
398384 KB |
Output is correct |
30 |
Correct |
2106 ms |
405100 KB |
Output is correct |
31 |
Correct |
2084 ms |
405080 KB |
Output is correct |
32 |
Correct |
2036 ms |
405332 KB |
Output is correct |
33 |
Correct |
2103 ms |
405316 KB |
Output is correct |
34 |
Correct |
1062 ms |
340308 KB |
Output is correct |
35 |
Correct |
2045 ms |
405176 KB |
Output is correct |
36 |
Correct |
2483 ms |
405496 KB |
Output is correct |
37 |
Correct |
1612 ms |
384288 KB |
Output is correct |
38 |
Correct |
1974 ms |
400660 KB |
Output is correct |
39 |
Correct |
2099 ms |
404620 KB |
Output is correct |
40 |
Correct |
2221 ms |
405856 KB |
Output is correct |
41 |
Correct |
2475 ms |
405504 KB |
Output is correct |
42 |
Correct |
2504 ms |
405684 KB |
Output is correct |
43 |
Correct |
2593 ms |
405796 KB |
Output is correct |
44 |
Correct |
2224 ms |
405560 KB |
Output is correct |
45 |
Correct |
1514 ms |
398948 KB |
Output is correct |
46 |
Correct |
2455 ms |
405720 KB |
Output is correct |
47 |
Correct |
2491 ms |
405724 KB |
Output is correct |
48 |
Correct |
2538 ms |
405912 KB |
Output is correct |
49 |
Correct |
2540 ms |
405608 KB |
Output is correct |
50 |
Correct |
1150 ms |
341060 KB |
Output is correct |
51 |
Correct |
2227 ms |
405940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4188 KB |
Output is correct |
2 |
Correct |
1 ms |
4188 KB |
Output is correct |
3 |
Correct |
1 ms |
4188 KB |
Output is correct |
4 |
Correct |
1 ms |
4188 KB |
Output is correct |
5 |
Correct |
2 ms |
4236 KB |
Output is correct |
6 |
Correct |
1 ms |
4184 KB |
Output is correct |
7 |
Correct |
2 ms |
4188 KB |
Output is correct |
8 |
Correct |
1 ms |
4188 KB |
Output is correct |
9 |
Correct |
1 ms |
4188 KB |
Output is correct |
10 |
Correct |
1 ms |
4188 KB |
Output is correct |
11 |
Correct |
2 ms |
4404 KB |
Output is correct |
12 |
Correct |
1 ms |
4188 KB |
Output is correct |
13 |
Correct |
1 ms |
4192 KB |
Output is correct |
14 |
Correct |
1 ms |
4200 KB |
Output is correct |
15 |
Correct |
1 ms |
4396 KB |
Output is correct |
16 |
Correct |
1 ms |
4200 KB |
Output is correct |
17 |
Correct |
1 ms |
4200 KB |
Output is correct |
18 |
Correct |
2 ms |
4200 KB |
Output is correct |
19 |
Correct |
1 ms |
4200 KB |
Output is correct |
20 |
Correct |
2174 ms |
405576 KB |
Output is correct |
21 |
Correct |
1238 ms |
383512 KB |
Output is correct |
22 |
Correct |
1534 ms |
400136 KB |
Output is correct |
23 |
Correct |
1690 ms |
404272 KB |
Output is correct |
24 |
Correct |
1771 ms |
405020 KB |
Output is correct |
25 |
Correct |
1997 ms |
405104 KB |
Output is correct |
26 |
Correct |
2044 ms |
405180 KB |
Output is correct |
27 |
Correct |
2009 ms |
405332 KB |
Output is correct |
28 |
Correct |
2106 ms |
405152 KB |
Output is correct |
29 |
Correct |
1564 ms |
398384 KB |
Output is correct |
30 |
Correct |
2106 ms |
405100 KB |
Output is correct |
31 |
Correct |
2084 ms |
405080 KB |
Output is correct |
32 |
Correct |
2036 ms |
405332 KB |
Output is correct |
33 |
Correct |
2103 ms |
405316 KB |
Output is correct |
34 |
Correct |
1062 ms |
340308 KB |
Output is correct |
35 |
Correct |
2045 ms |
405176 KB |
Output is correct |
36 |
Correct |
1 ms |
4188 KB |
Output is correct |
37 |
Correct |
1 ms |
4188 KB |
Output is correct |
38 |
Correct |
2 ms |
4188 KB |
Output is correct |
39 |
Execution timed out |
5110 ms |
409896 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |