#include<bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ld long double
using namespace std;
const ll maxn=505, maxm=100005, maxq=1000005;
pair<ll, pll> e[maxm];
vector <pair<pll, ll>> edge;
ll n, m, x[maxq], ans[maxq], cnt[maxq];
struct DSU
{
vector <ll> pa, Rank;
DSU(ll n)
{
pa.assign(n+1, 0);
for (ll i=1; i<=n; i++)
pa[i]=i;
Rank.assign(n+1, 0);
}
ll Find(ll i)
{
if (pa[i]!=i)
pa[i]=Find(pa[i]);
return pa[i];
}
bool Union(ll a, ll b)
{
ll Pa=Find(a), Pb=Find(b);
if (Pa==Pb) return 0;
if (Rank[Pa]<Rank[Pb]) swap(Pa, Pb);
if (Rank[Pa]==Rank[Pb]) Rank[Pa]++;
pa[Pb]=Pa; return 1;
}
};
void addedge(ll u, ll v, ll id)
{
DSU A(n);
vector <pair<pll, ll>> edgen;
edgen.pb({{u, v}, id}); A.Union(u, v);
for (pair<pll, ll> e:edge)
if (A.Union(e.fi.fi, e.fi.se))
edgen.pb(e);
swap(edge, edgen);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for (ll i=1; i<=m; i++)
cin >> e[i].se.fi >> e[i].se.se >> e[i].fi;
sort(e+1, e+m+1);
ll q; cin >> q;
for (ll i=1; i<=q; i++)
cin >> x[i];
for (ll i=1; i<=m; i++)
{
DSU A(n);
ll u=e[i].se.fi, v=e[i].se.se, w=e[i].fi, ok=0;
for (pair<pll, ll> j:edge)
{
A.Union(j.fi.fi, j.fi.se);
if (A.Find(u)==A.Find(v))
{ok=e[j.se].fi; break;}
}
ll p=upper_bound(x+1, x+q+1, w)-x;
if (!ok) ans[1]+=w, ans[p]-=w, cnt[1]--, cnt[p]++;
else
{
ll L=(ok+w)/2+1, p1=lower_bound(x+1, x+q+1, L)-x;
ans[p1]+=w, ans[p]-=w, cnt[p1]--, cnt[p]++;
}
addedge(u, v, i);
}
edge.clear();
for (ll i=m; i>=1; i--)
{
DSU A(n);
ll u=e[i].se.fi, v=e[i].se.se, w=e[i].fi, ok=0;
for (pair<pll, ll> j:edge)
{
A.Union(j.fi.fi, j.fi.se);
if (A.Find(u)==A.Find(v))
{ok=e[j.se].fi; break;}
}
ll p=upper_bound(x+1, x+q+1, w)-x;
if (!ok) ans[p]-=w, cnt[p]++;
else
{
ll R=(ok+w)/2, p1=upper_bound(x+1, x+q+1, R)-x;
ans[p]-=w, ans[p1]+=w, cnt[p]++, cnt[p1]--;
}
addedge(u, v, i);
}
for (ll i=1; i<=q; i++)
{
ans[i]+=ans[i-1], cnt[i]+=cnt[i-1];
cout << ans[i]+cnt[i]*x[i] << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6744 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
2 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6864 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6488 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
2 ms |
6492 KB |
Output is correct |
15 |
Correct |
2 ms |
6556 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
2 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6744 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
2 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6864 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6488 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
2 ms |
6492 KB |
Output is correct |
15 |
Correct |
2 ms |
6556 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
2 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
2121 ms |
9396 KB |
Output is correct |
21 |
Correct |
1405 ms |
9552 KB |
Output is correct |
22 |
Correct |
1462 ms |
9400 KB |
Output is correct |
23 |
Correct |
1532 ms |
9556 KB |
Output is correct |
24 |
Correct |
1855 ms |
9400 KB |
Output is correct |
25 |
Correct |
2060 ms |
9408 KB |
Output is correct |
26 |
Correct |
2108 ms |
9400 KB |
Output is correct |
27 |
Correct |
2095 ms |
9356 KB |
Output is correct |
28 |
Correct |
2111 ms |
9404 KB |
Output is correct |
29 |
Correct |
1195 ms |
9612 KB |
Output is correct |
30 |
Correct |
2156 ms |
9400 KB |
Output is correct |
31 |
Correct |
2179 ms |
9404 KB |
Output is correct |
32 |
Correct |
2112 ms |
9296 KB |
Output is correct |
33 |
Correct |
2102 ms |
9404 KB |
Output is correct |
34 |
Correct |
1040 ms |
9712 KB |
Output is correct |
35 |
Correct |
2120 ms |
9404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1655 ms |
48564 KB |
Output is correct |
5 |
Correct |
1714 ms |
48704 KB |
Output is correct |
6 |
Correct |
1726 ms |
48696 KB |
Output is correct |
7 |
Correct |
998 ms |
50844 KB |
Output is correct |
8 |
Correct |
904 ms |
50516 KB |
Output is correct |
9 |
Correct |
821 ms |
50744 KB |
Output is correct |
10 |
Correct |
1640 ms |
48876 KB |
Output is correct |
11 |
Correct |
794 ms |
50880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6744 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
2 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6864 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6488 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
2 ms |
6492 KB |
Output is correct |
15 |
Correct |
2 ms |
6556 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
2 ms |
6492 KB |
Output is correct |
19 |
Correct |
2 ms |
6492 KB |
Output is correct |
20 |
Correct |
161 ms |
47048 KB |
Output is correct |
21 |
Correct |
159 ms |
47188 KB |
Output is correct |
22 |
Correct |
158 ms |
47188 KB |
Output is correct |
23 |
Correct |
168 ms |
47184 KB |
Output is correct |
24 |
Correct |
157 ms |
47184 KB |
Output is correct |
25 |
Correct |
159 ms |
47188 KB |
Output is correct |
26 |
Correct |
162 ms |
47040 KB |
Output is correct |
27 |
Correct |
157 ms |
47184 KB |
Output is correct |
28 |
Correct |
173 ms |
47440 KB |
Output is correct |
29 |
Correct |
157 ms |
47184 KB |
Output is correct |
30 |
Correct |
166 ms |
47224 KB |
Output is correct |
31 |
Correct |
158 ms |
46928 KB |
Output is correct |
32 |
Correct |
158 ms |
47700 KB |
Output is correct |
33 |
Correct |
166 ms |
47264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6744 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
2 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6864 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6488 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
2 ms |
6492 KB |
Output is correct |
15 |
Correct |
2 ms |
6556 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
2 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
2121 ms |
9396 KB |
Output is correct |
21 |
Correct |
1405 ms |
9552 KB |
Output is correct |
22 |
Correct |
1462 ms |
9400 KB |
Output is correct |
23 |
Correct |
1532 ms |
9556 KB |
Output is correct |
24 |
Correct |
1855 ms |
9400 KB |
Output is correct |
25 |
Correct |
2060 ms |
9408 KB |
Output is correct |
26 |
Correct |
2108 ms |
9400 KB |
Output is correct |
27 |
Correct |
2095 ms |
9356 KB |
Output is correct |
28 |
Correct |
2111 ms |
9404 KB |
Output is correct |
29 |
Correct |
1195 ms |
9612 KB |
Output is correct |
30 |
Correct |
2156 ms |
9400 KB |
Output is correct |
31 |
Correct |
2179 ms |
9404 KB |
Output is correct |
32 |
Correct |
2112 ms |
9296 KB |
Output is correct |
33 |
Correct |
2102 ms |
9404 KB |
Output is correct |
34 |
Correct |
1040 ms |
9712 KB |
Output is correct |
35 |
Correct |
2120 ms |
9404 KB |
Output is correct |
36 |
Correct |
2124 ms |
9860 KB |
Output is correct |
37 |
Correct |
1417 ms |
10068 KB |
Output is correct |
38 |
Correct |
1521 ms |
9964 KB |
Output is correct |
39 |
Correct |
1571 ms |
9748 KB |
Output is correct |
40 |
Correct |
1832 ms |
9748 KB |
Output is correct |
41 |
Correct |
2083 ms |
9836 KB |
Output is correct |
42 |
Correct |
2123 ms |
9984 KB |
Output is correct |
43 |
Correct |
2261 ms |
9908 KB |
Output is correct |
44 |
Correct |
2238 ms |
9792 KB |
Output is correct |
45 |
Correct |
1226 ms |
9932 KB |
Output is correct |
46 |
Correct |
2187 ms |
9904 KB |
Output is correct |
47 |
Correct |
2275 ms |
10156 KB |
Output is correct |
48 |
Correct |
2130 ms |
9760 KB |
Output is correct |
49 |
Correct |
2174 ms |
10172 KB |
Output is correct |
50 |
Correct |
999 ms |
9956 KB |
Output is correct |
51 |
Correct |
2124 ms |
9884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6744 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
2 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6864 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6488 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
2 ms |
6492 KB |
Output is correct |
15 |
Correct |
2 ms |
6556 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
2 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
2121 ms |
9396 KB |
Output is correct |
21 |
Correct |
1405 ms |
9552 KB |
Output is correct |
22 |
Correct |
1462 ms |
9400 KB |
Output is correct |
23 |
Correct |
1532 ms |
9556 KB |
Output is correct |
24 |
Correct |
1855 ms |
9400 KB |
Output is correct |
25 |
Correct |
2060 ms |
9408 KB |
Output is correct |
26 |
Correct |
2108 ms |
9400 KB |
Output is correct |
27 |
Correct |
2095 ms |
9356 KB |
Output is correct |
28 |
Correct |
2111 ms |
9404 KB |
Output is correct |
29 |
Correct |
1195 ms |
9612 KB |
Output is correct |
30 |
Correct |
2156 ms |
9400 KB |
Output is correct |
31 |
Correct |
2179 ms |
9404 KB |
Output is correct |
32 |
Correct |
2112 ms |
9296 KB |
Output is correct |
33 |
Correct |
2102 ms |
9404 KB |
Output is correct |
34 |
Correct |
1040 ms |
9712 KB |
Output is correct |
35 |
Correct |
2120 ms |
9404 KB |
Output is correct |
36 |
Correct |
2 ms |
6488 KB |
Output is correct |
37 |
Correct |
2 ms |
6492 KB |
Output is correct |
38 |
Correct |
1 ms |
6492 KB |
Output is correct |
39 |
Correct |
1655 ms |
48564 KB |
Output is correct |
40 |
Correct |
1714 ms |
48704 KB |
Output is correct |
41 |
Correct |
1726 ms |
48696 KB |
Output is correct |
42 |
Correct |
998 ms |
50844 KB |
Output is correct |
43 |
Correct |
904 ms |
50516 KB |
Output is correct |
44 |
Correct |
821 ms |
50744 KB |
Output is correct |
45 |
Correct |
1640 ms |
48876 KB |
Output is correct |
46 |
Correct |
794 ms |
50880 KB |
Output is correct |
47 |
Correct |
2 ms |
6492 KB |
Output is correct |
48 |
Correct |
161 ms |
47048 KB |
Output is correct |
49 |
Correct |
159 ms |
47188 KB |
Output is correct |
50 |
Correct |
158 ms |
47188 KB |
Output is correct |
51 |
Correct |
168 ms |
47184 KB |
Output is correct |
52 |
Correct |
157 ms |
47184 KB |
Output is correct |
53 |
Correct |
159 ms |
47188 KB |
Output is correct |
54 |
Correct |
162 ms |
47040 KB |
Output is correct |
55 |
Correct |
157 ms |
47184 KB |
Output is correct |
56 |
Correct |
173 ms |
47440 KB |
Output is correct |
57 |
Correct |
157 ms |
47184 KB |
Output is correct |
58 |
Correct |
166 ms |
47224 KB |
Output is correct |
59 |
Correct |
158 ms |
46928 KB |
Output is correct |
60 |
Correct |
158 ms |
47700 KB |
Output is correct |
61 |
Correct |
166 ms |
47264 KB |
Output is correct |
62 |
Correct |
2124 ms |
9860 KB |
Output is correct |
63 |
Correct |
1417 ms |
10068 KB |
Output is correct |
64 |
Correct |
1521 ms |
9964 KB |
Output is correct |
65 |
Correct |
1571 ms |
9748 KB |
Output is correct |
66 |
Correct |
1832 ms |
9748 KB |
Output is correct |
67 |
Correct |
2083 ms |
9836 KB |
Output is correct |
68 |
Correct |
2123 ms |
9984 KB |
Output is correct |
69 |
Correct |
2261 ms |
9908 KB |
Output is correct |
70 |
Correct |
2238 ms |
9792 KB |
Output is correct |
71 |
Correct |
1226 ms |
9932 KB |
Output is correct |
72 |
Correct |
2187 ms |
9904 KB |
Output is correct |
73 |
Correct |
2275 ms |
10156 KB |
Output is correct |
74 |
Correct |
2130 ms |
9760 KB |
Output is correct |
75 |
Correct |
2174 ms |
10172 KB |
Output is correct |
76 |
Correct |
999 ms |
9956 KB |
Output is correct |
77 |
Correct |
2124 ms |
9884 KB |
Output is correct |
78 |
Correct |
2344 ms |
47608 KB |
Output is correct |
79 |
Correct |
1599 ms |
49892 KB |
Output is correct |
80 |
Correct |
1646 ms |
48704 KB |
Output is correct |
81 |
Correct |
1741 ms |
48696 KB |
Output is correct |
82 |
Correct |
2009 ms |
47684 KB |
Output is correct |
83 |
Correct |
2320 ms |
47680 KB |
Output is correct |
84 |
Correct |
2344 ms |
47696 KB |
Output is correct |
85 |
Correct |
2336 ms |
47720 KB |
Output is correct |
86 |
Correct |
2267 ms |
47664 KB |
Output is correct |
87 |
Correct |
1348 ms |
49332 KB |
Output is correct |
88 |
Correct |
2328 ms |
47676 KB |
Output is correct |
89 |
Correct |
2531 ms |
47676 KB |
Output is correct |
90 |
Correct |
2326 ms |
47848 KB |
Output is correct |
91 |
Correct |
2456 ms |
47568 KB |
Output is correct |
92 |
Correct |
1277 ms |
50660 KB |
Output is correct |
93 |
Correct |
2345 ms |
48656 KB |
Output is correct |