#include <bits/stdc++.h>
using namespace std;
#define ll int
#define pll pair<ll,ll>
#define ff first
#define ss second
#define pb push_back
#define endl "\n"
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
const ll maxn=503;
const ll maxn1=1e5+10;
const ll mod=1e9+7 ;
const ll base=1e9;
/// i believe myself
/// goal 2/7
ll par[maxn];
ll find_par(ll u)
{
if (par[u]<0)
return u;
return par[u]=find_par(par[u]);
}
bool dsu(ll x,ll y)
{
x=find_par(x);
y=find_par(y);
if (x==y)
return false;
if (par[x]>par[y])
swap(x,y);
par[x]+=par[y];
par[y]=x;
return true;
}
vector<pair<ll,pll>> gr[maxn1];
vector<pll> adjq[maxn1];
long long res[maxn1*10];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
if (fopen("t.inp","r"))
{
freopen("test.inp","r",stdin);
freopen("test.out","w",stdout);
}
ll n, m;
cin>> n>> m;
vector<pair<ll,pll>> vt;
vector<ll> vt1;
for (int i=1; i<=m; i++)
{
ll x, y, w;
cin>> x>> y>> w;
vt.pb(make_pair(w,make_pair(x,y)));
vt1.pb(w);
}
sort(vt.begin(),vt.end());
sort(vt1.begin(),vt1.end());
vt1.pb(base);
vt1.resize(unique(vt1.begin(),vt1.end())-vt1.begin());
ll q;
cin>> q;
for (int i=1; i<=q; i++)
{
ll x;
cin>> x;
auto p=lower_bound(vt1.begin(),vt1.end(),x)-vt1.begin();
adjq[p].pb(make_pair(x,i));
}
vector<pair<ll,pll>> vt2;
vector<pair<ll,pll>> vtp;
ll id=0;
for (int i=0; i<vt1.size(); i++)
{
memset(par,-1,sizeof(par));
vtp.clear();
while (id<vt.size()&&vt[id].ff==vt1[i])
{
auto p=vt[id].ss;
if (dsu(p.ff,p.ss))
{
vtp.pb(vt[id]);
}
id++;
}
for (auto to:vt2)
{
if (dsu(to.ss.ff,to.ss.ss))
vtp.pb(to);
}
gr[i]=vtp;
swap(vtp,vt2);
}
vt2.clear();
id=vt.size()-1;
for (int i=vt1.size()-1; i>=0; i--)
{
memset(par,-1,sizeof(par));
vtp.clear();
while (id>=0&&vt[id].ff==vt1[i])
{
auto p=vt[id].ss;
if (dsu(p.ff,p.ss))
{
vtp.pb(vt[id]);
}
id--;
}
for (auto to:vt2)
{
if (dsu(to.ss.ff,to.ss.ss))
vtp.pb(to);
}
for (auto p:adjq[i])
{
memset(par,-1,sizeof(par));
ll idr=0;
ll idl=0;
ll cnt=0;
ll x=p.ff;
ll id=p.ss;
long long ans=0;
while (cnt!=n-1)
{
ll val=base+3;
if (idr<vtp.size()) val=vtp[idr].ff-x;
ll val1=base+3;
if (i&&idl<gr[i-1].size()) val1=x-gr[i-1][idl].ff;
if (val<val1)
{
ll h=dsu(vtp[idr].ss.ff,vtp[idr].ss.ss);
cnt+=h;
ans+=(h*val);
idr++;
}
else
{
ll h=dsu(gr[i-1][idl].ss.ff,gr[i-1][idl].ss.ss);
cnt+=h;
ans+=(h*val1);
idl++;
}
}
res[id]=ans;
}
swap(vtp,vt2);
}
for (int i=1;i<=q;i++) cout <<res[i]<<endl;
}
Compilation message
reconstruction.cpp: In function 'int main()':
reconstruction.cpp:81:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | for (int i=0; i<vt1.size(); i++)
| ~^~~~~~~~~~~
reconstruction.cpp:85:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
85 | while (id<vt.size()&&vt[id].ff==vt1[i])
| ~~^~~~~~~~~~
reconstruction.cpp:134:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
134 | if (idr<vtp.size()) val=vtp[idr].ff-x;
| ~~~^~~~~~~~~~~
reconstruction.cpp:136:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
136 | if (i&&idl<gr[i-1].size()) val1=x-gr[i-1][idl].ff;
| ~~~^~~~~~~~~~~~~~~
reconstruction.cpp:51:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | freopen("test.inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
reconstruction.cpp:52:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
52 | freopen("test.out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4932 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5040 KB |
Output is correct |
6 |
Correct |
3 ms |
5032 KB |
Output is correct |
7 |
Correct |
4 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4960 KB |
Output is correct |
11 |
Correct |
4 ms |
4968 KB |
Output is correct |
12 |
Correct |
4 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
4 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
4 ms |
5028 KB |
Output is correct |
18 |
Correct |
3 ms |
5032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4932 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5040 KB |
Output is correct |
6 |
Correct |
3 ms |
5032 KB |
Output is correct |
7 |
Correct |
4 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4960 KB |
Output is correct |
11 |
Correct |
4 ms |
4968 KB |
Output is correct |
12 |
Correct |
4 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
4 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
4 ms |
5028 KB |
Output is correct |
18 |
Correct |
3 ms |
5032 KB |
Output is correct |
19 |
Correct |
3 ms |
5040 KB |
Output is correct |
20 |
Correct |
1811 ms |
593524 KB |
Output is correct |
21 |
Correct |
1472 ms |
560128 KB |
Output is correct |
22 |
Correct |
1593 ms |
584040 KB |
Output is correct |
23 |
Correct |
1615 ms |
590316 KB |
Output is correct |
24 |
Correct |
1637 ms |
591468 KB |
Output is correct |
25 |
Correct |
1696 ms |
591724 KB |
Output is correct |
26 |
Correct |
1750 ms |
593520 KB |
Output is correct |
27 |
Correct |
1633 ms |
565372 KB |
Output is correct |
28 |
Correct |
70 ms |
14028 KB |
Output is correct |
29 |
Correct |
46 ms |
8792 KB |
Output is correct |
30 |
Correct |
1725 ms |
593560 KB |
Output is correct |
31 |
Correct |
1694 ms |
593472 KB |
Output is correct |
32 |
Correct |
1716 ms |
593532 KB |
Output is correct |
33 |
Correct |
1712 ms |
593436 KB |
Output is correct |
34 |
Correct |
44 ms |
8832 KB |
Output is correct |
35 |
Correct |
1714 ms |
593480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Execution timed out |
5106 ms |
604720 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4932 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5040 KB |
Output is correct |
6 |
Correct |
3 ms |
5032 KB |
Output is correct |
7 |
Correct |
4 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4960 KB |
Output is correct |
11 |
Correct |
4 ms |
4968 KB |
Output is correct |
12 |
Correct |
4 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
4 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
4 ms |
5028 KB |
Output is correct |
18 |
Correct |
3 ms |
5032 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Execution timed out |
5090 ms |
24028 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4932 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5040 KB |
Output is correct |
6 |
Correct |
3 ms |
5032 KB |
Output is correct |
7 |
Correct |
4 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4960 KB |
Output is correct |
11 |
Correct |
4 ms |
4968 KB |
Output is correct |
12 |
Correct |
4 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
4 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
4 ms |
5028 KB |
Output is correct |
18 |
Correct |
3 ms |
5032 KB |
Output is correct |
19 |
Correct |
3 ms |
5040 KB |
Output is correct |
20 |
Correct |
1811 ms |
593524 KB |
Output is correct |
21 |
Correct |
1472 ms |
560128 KB |
Output is correct |
22 |
Correct |
1593 ms |
584040 KB |
Output is correct |
23 |
Correct |
1615 ms |
590316 KB |
Output is correct |
24 |
Correct |
1637 ms |
591468 KB |
Output is correct |
25 |
Correct |
1696 ms |
591724 KB |
Output is correct |
26 |
Correct |
1750 ms |
593520 KB |
Output is correct |
27 |
Correct |
1633 ms |
565372 KB |
Output is correct |
28 |
Correct |
70 ms |
14028 KB |
Output is correct |
29 |
Correct |
46 ms |
8792 KB |
Output is correct |
30 |
Correct |
1725 ms |
593560 KB |
Output is correct |
31 |
Correct |
1694 ms |
593472 KB |
Output is correct |
32 |
Correct |
1716 ms |
593532 KB |
Output is correct |
33 |
Correct |
1712 ms |
593436 KB |
Output is correct |
34 |
Correct |
44 ms |
8832 KB |
Output is correct |
35 |
Correct |
1714 ms |
593480 KB |
Output is correct |
36 |
Correct |
2108 ms |
594700 KB |
Output is correct |
37 |
Correct |
1809 ms |
560388 KB |
Output is correct |
38 |
Correct |
1911 ms |
587456 KB |
Output is correct |
39 |
Correct |
1999 ms |
593292 KB |
Output is correct |
40 |
Correct |
2090 ms |
593872 KB |
Output is correct |
41 |
Correct |
2091 ms |
594640 KB |
Output is correct |
42 |
Correct |
2106 ms |
594388 KB |
Output is correct |
43 |
Correct |
2020 ms |
566500 KB |
Output is correct |
44 |
Correct |
346 ms |
14904 KB |
Output is correct |
45 |
Correct |
152 ms |
9280 KB |
Output is correct |
46 |
Correct |
2113 ms |
594856 KB |
Output is correct |
47 |
Correct |
2096 ms |
594636 KB |
Output is correct |
48 |
Correct |
2094 ms |
594640 KB |
Output is correct |
49 |
Correct |
2206 ms |
594580 KB |
Output is correct |
50 |
Correct |
145 ms |
9180 KB |
Output is correct |
51 |
Correct |
1906 ms |
594176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4932 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5040 KB |
Output is correct |
6 |
Correct |
3 ms |
5032 KB |
Output is correct |
7 |
Correct |
4 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4960 KB |
Output is correct |
11 |
Correct |
4 ms |
4968 KB |
Output is correct |
12 |
Correct |
4 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
4 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
4 ms |
5028 KB |
Output is correct |
18 |
Correct |
3 ms |
5032 KB |
Output is correct |
19 |
Correct |
3 ms |
5040 KB |
Output is correct |
20 |
Correct |
1811 ms |
593524 KB |
Output is correct |
21 |
Correct |
1472 ms |
560128 KB |
Output is correct |
22 |
Correct |
1593 ms |
584040 KB |
Output is correct |
23 |
Correct |
1615 ms |
590316 KB |
Output is correct |
24 |
Correct |
1637 ms |
591468 KB |
Output is correct |
25 |
Correct |
1696 ms |
591724 KB |
Output is correct |
26 |
Correct |
1750 ms |
593520 KB |
Output is correct |
27 |
Correct |
1633 ms |
565372 KB |
Output is correct |
28 |
Correct |
70 ms |
14028 KB |
Output is correct |
29 |
Correct |
46 ms |
8792 KB |
Output is correct |
30 |
Correct |
1725 ms |
593560 KB |
Output is correct |
31 |
Correct |
1694 ms |
593472 KB |
Output is correct |
32 |
Correct |
1716 ms |
593532 KB |
Output is correct |
33 |
Correct |
1712 ms |
593436 KB |
Output is correct |
34 |
Correct |
44 ms |
8832 KB |
Output is correct |
35 |
Correct |
1714 ms |
593480 KB |
Output is correct |
36 |
Correct |
3 ms |
4948 KB |
Output is correct |
37 |
Correct |
3 ms |
4948 KB |
Output is correct |
38 |
Correct |
2 ms |
4948 KB |
Output is correct |
39 |
Execution timed out |
5106 ms |
604720 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |