Submission #370919

# Submission time Handle Problem Language Result Execution time Memory
370919 2021-02-25T04:24:22 Z azberjibiou Designated Cities (JOI19_designated_cities) C++17
100 / 100
881 ms 76884 KB
#include <bits/stdc++.h>
#define fir first
#define sec second
#define ll long long
#define pll pair<ll, ll>
using namespace std;
const int mxN=200020;
const ll INF=1000000000000000001;
typedef struct line{
    int s, e;
    ll val;
}line;
int N, Q;
map <int, ll> v[mxN];
vector <pll> rv[mxN];
ll ans[mxN];
ll tot, rtot;
ll dep[mxN], rdep[mxN];
void dfs(int now, int pre)
{
    for(auto iter=v[now].begin();iter!=v[now].end();iter++)
    {
        if(iter->fir==pre)  continue;
        int nxt=iter->fir;
        dep[nxt]=dep[now]+iter->sec;
        tot+=iter->sec;
        dfs(nxt, now);
    }
}
void rdfs(int now, int pre)
{
    for(pll ele : rv[now])
    {
        if(ele.fir==pre)    continue;
        rdep[ele.fir]=rdep[now]+ele.sec;
        rdfs(ele.fir, now);
    }
}
void solv1()
{
    dfs(1, -1);
    rdfs(1, -1);
    for(int i=1;i<=N;i++)
    {
        ans[1]=max(ans[1], tot-dep[i]+rdep[i]);
    }
    ans[1]=rtot-ans[1];
}
typedef struct cmp1{
    bool operator()(const pll a, const pll b) const
    {
        if(a.sec!=b.sec)    return a.sec<b.sec;
        return a.fir<b.fir;
    }
}cmp1;
multiset <pll, cmp1> pq;
void solv2()
{
    for(int i=1;i<=N;i++)
    {
        if(v[i].size()==2)
        {
            ll a=-1, b=-1, ia, ib, ai, bi;
            for(auto iter=v[i].begin();iter!=v[i].end();iter++)
            {
                if(a==-1)   a=iter->fir, ia=iter->sec;
                else    b=iter->fir, ib=iter->sec;
            }
            auto itera=v[a].find(i);
            ai=itera->sec;
            v[a].erase(itera);
            auto iterb=v[b].find(i);
            bi=iterb->sec;
            v[b].erase(iterb);
            v[a].insert({b, ai+ib});
            v[b].insert({a, bi+ia});
        }
    }
    for(int i=1;i<=N;i++)
    {
        if(v[i].size()==1)
        {
            auto iter=v[i].begin();
            pq.insert({i, iter->sec});
        }
    }
    int pqs=pq.size();
    for(int i=pqs;i<=N;i++)   ans[i]=0;
    for(int i=pqs-1;i>=2;i--)
    {
        pll now=*pq.begin();
        pq.erase(pq.begin());
        ans[i]=ans[i+1]+now.sec;
        auto iter=v[now.fir].begin();
        int nxt=iter->fir;
        auto itern=v[nxt].find(now.fir);
        v[nxt].erase(itern);
        if(i==2)    break;
        if(v[nxt].size()==2)
        {
            ll a=-1, b=-1, ia, ib, ai, bi;
            for(auto iter=v[nxt].begin();iter!=v[nxt].end();iter++)
            {
                if(a==-1)   a=iter->fir, ia=iter->sec;
                else        b=iter->fir, ib=iter->sec;
            }
            if(a==-1 || b==-1)  return;
            if(v[a].size()==1)
            {
                ll nval=v[a].begin()->sec;
                pq.erase({a, nval});
            }
            if(v[b].size()==1)
            {
                ll nval=v[b].begin()->sec;
                pq.erase({b, nval});
            }
            auto itera=v[a].find(nxt);
            auto iterb=v[b].find(nxt);
            ai=itera->sec, bi=iterb->sec;
            v[a].erase(itera);  v[b].erase(iterb);
            v[a].insert({b, ai+ib});
            v[b].insert({a, bi+ia});
            if(v[a].size()==1)  pq.insert({a, v[a].begin()->sec});
            if(v[b].size()==1)  pq.insert({b, v[b].begin()->sec});
        }
    }
}
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> N;
    for(int i=0;i<N-1;i++)
    {
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        v[a].insert({b, d}); v[b].insert({a, c});
        rv[a].push_back({b, c});    rv[b].push_back({a, d});
        rtot+=c;    rtot+=d;
    }
    solv1();
    solv2();
    cin >> Q;
    while(Q--)
    {
        int a;
        cin >> a;
        cout << ans[a] << '\n';
    }
}

Compilation message

designated_cities.cpp: In function 'void solv2()':
designated_cities.cpp:122:31: warning: 'ib' may be used uninitialized in this function [-Wmaybe-uninitialized]
  122 |             v[a].insert({b, ai+ib});
      |                             ~~^~~
designated_cities.cpp:75:31: warning: 'ib' may be used uninitialized in this function [-Wmaybe-uninitialized]
   75 |             v[a].insert({b, ai+ib});
      |                             ~~^~~
designated_cities.cpp:76:31: warning: 'ia' may be used uninitialized in this function [-Wmaybe-uninitialized]
   76 |             v[b].insert({a, bi+ia});
      |                             ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14444 KB Output is correct
2 Correct 10 ms 14464 KB Output is correct
3 Correct 10 ms 14444 KB Output is correct
4 Correct 10 ms 14444 KB Output is correct
5 Correct 10 ms 14444 KB Output is correct
6 Correct 10 ms 14444 KB Output is correct
7 Correct 10 ms 14444 KB Output is correct
8 Correct 10 ms 14444 KB Output is correct
9 Correct 10 ms 14444 KB Output is correct
10 Correct 10 ms 14444 KB Output is correct
11 Correct 10 ms 14444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14444 KB Output is correct
2 Correct 692 ms 59500 KB Output is correct
3 Correct 650 ms 64832 KB Output is correct
4 Correct 628 ms 59568 KB Output is correct
5 Correct 774 ms 60128 KB Output is correct
6 Correct 691 ms 61292 KB Output is correct
7 Correct 862 ms 61788 KB Output is correct
8 Correct 674 ms 65748 KB Output is correct
9 Correct 879 ms 66248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14444 KB Output is correct
2 Correct 694 ms 59500 KB Output is correct
3 Correct 660 ms 72812 KB Output is correct
4 Correct 639 ms 64620 KB Output is correct
5 Correct 780 ms 66656 KB Output is correct
6 Correct 690 ms 67692 KB Output is correct
7 Correct 870 ms 71380 KB Output is correct
8 Correct 675 ms 71276 KB Output is correct
9 Correct 855 ms 72376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14444 KB Output is correct
2 Correct 10 ms 14464 KB Output is correct
3 Correct 10 ms 14444 KB Output is correct
4 Correct 10 ms 14444 KB Output is correct
5 Correct 10 ms 14444 KB Output is correct
6 Correct 10 ms 14444 KB Output is correct
7 Correct 10 ms 14444 KB Output is correct
8 Correct 10 ms 14444 KB Output is correct
9 Correct 10 ms 14444 KB Output is correct
10 Correct 10 ms 14444 KB Output is correct
11 Correct 10 ms 14444 KB Output is correct
12 Correct 10 ms 14444 KB Output is correct
13 Correct 13 ms 14956 KB Output is correct
14 Correct 13 ms 14956 KB Output is correct
15 Correct 16 ms 14956 KB Output is correct
16 Correct 13 ms 14956 KB Output is correct
17 Correct 15 ms 14956 KB Output is correct
18 Correct 13 ms 14956 KB Output is correct
19 Correct 13 ms 14956 KB Output is correct
20 Correct 14 ms 14956 KB Output is correct
21 Correct 13 ms 14956 KB Output is correct
22 Correct 13 ms 14956 KB Output is correct
23 Correct 13 ms 14956 KB Output is correct
24 Correct 13 ms 15084 KB Output is correct
25 Correct 15 ms 15084 KB Output is correct
26 Correct 13 ms 15084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14444 KB Output is correct
2 Correct 692 ms 59500 KB Output is correct
3 Correct 650 ms 64832 KB Output is correct
4 Correct 628 ms 59568 KB Output is correct
5 Correct 774 ms 60128 KB Output is correct
6 Correct 691 ms 61292 KB Output is correct
7 Correct 862 ms 61788 KB Output is correct
8 Correct 674 ms 65748 KB Output is correct
9 Correct 879 ms 66248 KB Output is correct
10 Correct 10 ms 14444 KB Output is correct
11 Correct 694 ms 59500 KB Output is correct
12 Correct 660 ms 72812 KB Output is correct
13 Correct 639 ms 64620 KB Output is correct
14 Correct 780 ms 66656 KB Output is correct
15 Correct 690 ms 67692 KB Output is correct
16 Correct 870 ms 71380 KB Output is correct
17 Correct 675 ms 71276 KB Output is correct
18 Correct 855 ms 72376 KB Output is correct
19 Correct 9 ms 14444 KB Output is correct
20 Correct 709 ms 65908 KB Output is correct
21 Correct 661 ms 72940 KB Output is correct
22 Correct 636 ms 64492 KB Output is correct
23 Correct 696 ms 66284 KB Output is correct
24 Correct 692 ms 65260 KB Output is correct
25 Correct 707 ms 66336 KB Output is correct
26 Correct 719 ms 65144 KB Output is correct
27 Correct 780 ms 66824 KB Output is correct
28 Correct 711 ms 67564 KB Output is correct
29 Correct 700 ms 65900 KB Output is correct
30 Correct 712 ms 66144 KB Output is correct
31 Correct 855 ms 69228 KB Output is correct
32 Correct 681 ms 71568 KB Output is correct
33 Correct 857 ms 73048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14444 KB Output is correct
2 Correct 10 ms 14464 KB Output is correct
3 Correct 10 ms 14444 KB Output is correct
4 Correct 10 ms 14444 KB Output is correct
5 Correct 10 ms 14444 KB Output is correct
6 Correct 10 ms 14444 KB Output is correct
7 Correct 10 ms 14444 KB Output is correct
8 Correct 10 ms 14444 KB Output is correct
9 Correct 10 ms 14444 KB Output is correct
10 Correct 10 ms 14444 KB Output is correct
11 Correct 10 ms 14444 KB Output is correct
12 Correct 9 ms 14444 KB Output is correct
13 Correct 692 ms 59500 KB Output is correct
14 Correct 650 ms 64832 KB Output is correct
15 Correct 628 ms 59568 KB Output is correct
16 Correct 774 ms 60128 KB Output is correct
17 Correct 691 ms 61292 KB Output is correct
18 Correct 862 ms 61788 KB Output is correct
19 Correct 674 ms 65748 KB Output is correct
20 Correct 879 ms 66248 KB Output is correct
21 Correct 10 ms 14444 KB Output is correct
22 Correct 694 ms 59500 KB Output is correct
23 Correct 660 ms 72812 KB Output is correct
24 Correct 639 ms 64620 KB Output is correct
25 Correct 780 ms 66656 KB Output is correct
26 Correct 690 ms 67692 KB Output is correct
27 Correct 870 ms 71380 KB Output is correct
28 Correct 675 ms 71276 KB Output is correct
29 Correct 855 ms 72376 KB Output is correct
30 Correct 10 ms 14444 KB Output is correct
31 Correct 13 ms 14956 KB Output is correct
32 Correct 13 ms 14956 KB Output is correct
33 Correct 16 ms 14956 KB Output is correct
34 Correct 13 ms 14956 KB Output is correct
35 Correct 15 ms 14956 KB Output is correct
36 Correct 13 ms 14956 KB Output is correct
37 Correct 13 ms 14956 KB Output is correct
38 Correct 14 ms 14956 KB Output is correct
39 Correct 13 ms 14956 KB Output is correct
40 Correct 13 ms 14956 KB Output is correct
41 Correct 13 ms 14956 KB Output is correct
42 Correct 13 ms 15084 KB Output is correct
43 Correct 15 ms 15084 KB Output is correct
44 Correct 13 ms 15084 KB Output is correct
45 Correct 9 ms 14444 KB Output is correct
46 Correct 709 ms 65908 KB Output is correct
47 Correct 661 ms 72940 KB Output is correct
48 Correct 636 ms 64492 KB Output is correct
49 Correct 696 ms 66284 KB Output is correct
50 Correct 692 ms 65260 KB Output is correct
51 Correct 707 ms 66336 KB Output is correct
52 Correct 719 ms 65144 KB Output is correct
53 Correct 780 ms 66824 KB Output is correct
54 Correct 711 ms 67564 KB Output is correct
55 Correct 700 ms 65900 KB Output is correct
56 Correct 712 ms 66144 KB Output is correct
57 Correct 855 ms 69228 KB Output is correct
58 Correct 681 ms 71568 KB Output is correct
59 Correct 857 ms 73048 KB Output is correct
60 Correct 10 ms 14444 KB Output is correct
61 Correct 723 ms 68632 KB Output is correct
62 Correct 691 ms 72908 KB Output is correct
63 Correct 683 ms 67180 KB Output is correct
64 Correct 730 ms 69100 KB Output is correct
65 Correct 721 ms 67564 KB Output is correct
66 Correct 730 ms 68904 KB Output is correct
67 Correct 736 ms 67564 KB Output is correct
68 Correct 789 ms 69240 KB Output is correct
69 Correct 719 ms 70124 KB Output is correct
70 Correct 707 ms 68332 KB Output is correct
71 Correct 732 ms 69088 KB Output is correct
72 Correct 870 ms 71772 KB Output is correct
73 Correct 685 ms 73068 KB Output is correct
74 Correct 881 ms 76884 KB Output is correct