Submission #973418

# Submission time Handle Problem Language Result Execution time Memory
973418 2024-05-02T01:37:43 Z hotboy2703 Reconstruction Project (JOI22_reconstruction) C++14
100 / 100
2456 ms 51044 KB
#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 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 2 ms 6492 KB Output is correct
5 Correct 1 ms 6608 KB Output is correct
6 Correct 1 ms 6488 KB Output is correct
7 Correct 1 ms 6488 KB Output is correct
8 Correct 1 ms 6488 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6564 KB Output is correct
11 Correct 1 ms 6488 KB Output is correct
12 Correct 1 ms 6488 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 2 ms 6492 KB Output is correct
5 Correct 1 ms 6608 KB Output is correct
6 Correct 1 ms 6488 KB Output is correct
7 Correct 1 ms 6488 KB Output is correct
8 Correct 1 ms 6488 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6564 KB Output is correct
11 Correct 1 ms 6488 KB Output is correct
12 Correct 1 ms 6488 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6492 KB Output is correct
19 Correct 1 ms 6744 KB Output is correct
20 Correct 2318 ms 7700 KB Output is correct
21 Correct 1467 ms 9408 KB Output is correct
22 Correct 1526 ms 9404 KB Output is correct
23 Correct 1624 ms 9404 KB Output is correct
24 Correct 1884 ms 9408 KB Output is correct
25 Correct 2172 ms 9408 KB Output is correct
26 Correct 2243 ms 9408 KB Output is correct
27 Correct 2217 ms 9300 KB Output is correct
28 Correct 2205 ms 9404 KB Output is correct
29 Correct 1279 ms 9412 KB Output is correct
30 Correct 2244 ms 9404 KB Output is correct
31 Correct 2337 ms 9404 KB Output is correct
32 Correct 2288 ms 9412 KB Output is correct
33 Correct 2240 ms 9408 KB Output is correct
34 Correct 1024 ms 9512 KB Output is correct
35 Correct 2259 ms 9408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 6488 KB Output is correct
2 Correct 1 ms 6488 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1782 ms 37336 KB Output is correct
5 Correct 1760 ms 48724 KB Output is correct
6 Correct 1747 ms 48716 KB Output is correct
7 Correct 976 ms 50492 KB Output is correct
8 Correct 821 ms 51036 KB Output is correct
9 Correct 811 ms 50820 KB Output is correct
10 Correct 1723 ms 48804 KB Output is correct
11 Correct 808 ms 51044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 2 ms 6492 KB Output is correct
5 Correct 1 ms 6608 KB Output is correct
6 Correct 1 ms 6488 KB Output is correct
7 Correct 1 ms 6488 KB Output is correct
8 Correct 1 ms 6488 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6564 KB Output is correct
11 Correct 1 ms 6488 KB Output is correct
12 Correct 1 ms 6488 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6492 KB Output is correct
19 Correct 1 ms 6492 KB Output is correct
20 Correct 161 ms 37332 KB Output is correct
21 Correct 157 ms 47188 KB Output is correct
22 Correct 163 ms 47088 KB Output is correct
23 Correct 165 ms 47312 KB Output is correct
24 Correct 164 ms 47188 KB Output is correct
25 Correct 165 ms 47184 KB Output is correct
26 Correct 165 ms 46932 KB Output is correct
27 Correct 170 ms 47324 KB Output is correct
28 Correct 163 ms 47188 KB Output is correct
29 Correct 156 ms 47188 KB Output is correct
30 Correct 167 ms 47380 KB Output is correct
31 Correct 163 ms 46924 KB Output is correct
32 Correct 159 ms 47772 KB Output is correct
33 Correct 158 ms 47184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 2 ms 6492 KB Output is correct
5 Correct 1 ms 6608 KB Output is correct
6 Correct 1 ms 6488 KB Output is correct
7 Correct 1 ms 6488 KB Output is correct
8 Correct 1 ms 6488 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6564 KB Output is correct
11 Correct 1 ms 6488 KB Output is correct
12 Correct 1 ms 6488 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6492 KB Output is correct
19 Correct 1 ms 6744 KB Output is correct
20 Correct 2318 ms 7700 KB Output is correct
21 Correct 1467 ms 9408 KB Output is correct
22 Correct 1526 ms 9404 KB Output is correct
23 Correct 1624 ms 9404 KB Output is correct
24 Correct 1884 ms 9408 KB Output is correct
25 Correct 2172 ms 9408 KB Output is correct
26 Correct 2243 ms 9408 KB Output is correct
27 Correct 2217 ms 9300 KB Output is correct
28 Correct 2205 ms 9404 KB Output is correct
29 Correct 1279 ms 9412 KB Output is correct
30 Correct 2244 ms 9404 KB Output is correct
31 Correct 2337 ms 9404 KB Output is correct
32 Correct 2288 ms 9412 KB Output is correct
33 Correct 2240 ms 9408 KB Output is correct
34 Correct 1024 ms 9512 KB Output is correct
35 Correct 2259 ms 9408 KB Output is correct
36 Correct 2222 ms 9884 KB Output is correct
37 Correct 1523 ms 9788 KB Output is correct
38 Correct 1525 ms 10128 KB Output is correct
39 Correct 1699 ms 9868 KB Output is correct
40 Correct 1905 ms 10164 KB Output is correct
41 Correct 2180 ms 9956 KB Output is correct
42 Correct 2258 ms 9908 KB Output is correct
43 Correct 2258 ms 9908 KB Output is correct
44 Correct 2246 ms 9860 KB Output is correct
45 Correct 1290 ms 10168 KB Output is correct
46 Correct 2234 ms 9968 KB Output is correct
47 Correct 2238 ms 10068 KB Output is correct
48 Correct 2252 ms 9944 KB Output is correct
49 Correct 2226 ms 9748 KB Output is correct
50 Correct 1034 ms 9972 KB Output is correct
51 Correct 2254 ms 9888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 2 ms 6492 KB Output is correct
5 Correct 1 ms 6608 KB Output is correct
6 Correct 1 ms 6488 KB Output is correct
7 Correct 1 ms 6488 KB Output is correct
8 Correct 1 ms 6488 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6564 KB Output is correct
11 Correct 1 ms 6488 KB Output is correct
12 Correct 1 ms 6488 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6492 KB Output is correct
19 Correct 1 ms 6744 KB Output is correct
20 Correct 2318 ms 7700 KB Output is correct
21 Correct 1467 ms 9408 KB Output is correct
22 Correct 1526 ms 9404 KB Output is correct
23 Correct 1624 ms 9404 KB Output is correct
24 Correct 1884 ms 9408 KB Output is correct
25 Correct 2172 ms 9408 KB Output is correct
26 Correct 2243 ms 9408 KB Output is correct
27 Correct 2217 ms 9300 KB Output is correct
28 Correct 2205 ms 9404 KB Output is correct
29 Correct 1279 ms 9412 KB Output is correct
30 Correct 2244 ms 9404 KB Output is correct
31 Correct 2337 ms 9404 KB Output is correct
32 Correct 2288 ms 9412 KB Output is correct
33 Correct 2240 ms 9408 KB Output is correct
34 Correct 1024 ms 9512 KB Output is correct
35 Correct 2259 ms 9408 KB Output is correct
36 Correct 2 ms 6488 KB Output is correct
37 Correct 1 ms 6488 KB Output is correct
38 Correct 1 ms 6492 KB Output is correct
39 Correct 1782 ms 37336 KB Output is correct
40 Correct 1760 ms 48724 KB Output is correct
41 Correct 1747 ms 48716 KB Output is correct
42 Correct 976 ms 50492 KB Output is correct
43 Correct 821 ms 51036 KB Output is correct
44 Correct 811 ms 50820 KB Output is correct
45 Correct 1723 ms 48804 KB Output is correct
46 Correct 808 ms 51044 KB Output is correct
47 Correct 1 ms 6492 KB Output is correct
48 Correct 161 ms 37332 KB Output is correct
49 Correct 157 ms 47188 KB Output is correct
50 Correct 163 ms 47088 KB Output is correct
51 Correct 165 ms 47312 KB Output is correct
52 Correct 164 ms 47188 KB Output is correct
53 Correct 165 ms 47184 KB Output is correct
54 Correct 165 ms 46932 KB Output is correct
55 Correct 170 ms 47324 KB Output is correct
56 Correct 163 ms 47188 KB Output is correct
57 Correct 156 ms 47188 KB Output is correct
58 Correct 167 ms 47380 KB Output is correct
59 Correct 163 ms 46924 KB Output is correct
60 Correct 159 ms 47772 KB Output is correct
61 Correct 158 ms 47184 KB Output is correct
62 Correct 2222 ms 9884 KB Output is correct
63 Correct 1523 ms 9788 KB Output is correct
64 Correct 1525 ms 10128 KB Output is correct
65 Correct 1699 ms 9868 KB Output is correct
66 Correct 1905 ms 10164 KB Output is correct
67 Correct 2180 ms 9956 KB Output is correct
68 Correct 2258 ms 9908 KB Output is correct
69 Correct 2258 ms 9908 KB Output is correct
70 Correct 2246 ms 9860 KB Output is correct
71 Correct 1290 ms 10168 KB Output is correct
72 Correct 2234 ms 9968 KB Output is correct
73 Correct 2238 ms 10068 KB Output is correct
74 Correct 2252 ms 9944 KB Output is correct
75 Correct 2226 ms 9748 KB Output is correct
76 Correct 1034 ms 9972 KB Output is correct
77 Correct 2254 ms 9888 KB Output is correct
78 Correct 2456 ms 47960 KB Output is correct
79 Correct 1653 ms 49788 KB Output is correct
80 Correct 1753 ms 48728 KB Output is correct
81 Correct 1842 ms 48732 KB Output is correct
82 Correct 2111 ms 47704 KB Output is correct
83 Correct 2347 ms 47708 KB Output is correct
84 Correct 2428 ms 47700 KB Output is correct
85 Correct 2424 ms 47708 KB Output is correct
86 Correct 2403 ms 47692 KB Output is correct
87 Correct 1502 ms 49252 KB Output is correct
88 Correct 2445 ms 47720 KB Output is correct
89 Correct 2443 ms 47704 KB Output is correct
90 Correct 2400 ms 47880 KB Output is correct
91 Correct 2410 ms 47480 KB Output is correct
92 Correct 1241 ms 50436 KB Output is correct
93 Correct 2394 ms 48908 KB Output is correct