Submission #211045

# Submission time Handle Problem Language Result Execution time Memory
211045 2020-03-19T06:07:48 Z HNO2 Designated Cities (JOI19_designated_cities) C++17
100 / 100
1128 ms 80548 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn=2e5+7;
const int inf=INT_MAX;
const ll inff=1e18;
const ll mod=1e9+7;
#define pii pair<int,int>
#define mkp make_pair
#define F first
#define S second
#define pb push_back
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(),(v).end()
#define int ll

#ifdef HNO2
#define IOS
#else
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0);
#endif // HNO2

int x[maxn*2],y[maxn*2],w[maxn*2];
int indeg[maxn];
vector <int> G[maxn];

int ans[maxn];
int tmpsum=0;

pii maxx[maxn];

int root,root2;

void dfs(int now,int p)
{
    for (int i:G[now])
        if ((x[i]^y[i]^now)!=p)
    {
        ans[1]+=w[i];
        dfs((x[i]^y[i]^now),now);
    }
}

void dfs2(int now,int p)
{
    ans[1]=min(ans[1],tmpsum);
    for (int i:G[now])
        if ((x[i]^y[i]^now)!=p)
    {
        tmpsum-=(w[i]-w[i^1]);
        dfs2((x[i]^y[i]^now),now);
        tmpsum+=(w[i]-w[i^1]);
    }
}

void dfs3(int now,int p)
{
    pair<pii,pii> nowmax=mkp(mkp(-inff,-inff),mkp(-inff,-inff));

    if (sz(G[now])==1)
    {
        maxx[now]=mkp(0,now);
        return;
    }

    for (int i:G[now])
        if ((x[i]^y[i]^now)!=p)
    {
        int I=(x[i]^y[i]^now);
        tmpsum-=(w[i]-w[i^1]);
        dfs3(I,now);

        //renew
        pii nowpair=mkp(maxx[I].F+w[i],maxx[I].S);
        if (nowpair>=nowmax.F) nowmax.S=nowmax.F,nowmax.F=nowpair;
        else if (nowpair>=nowmax.S) nowmax.S=nowpair;
        //

        tmpsum+=(w[i]-w[i^1]);
    }

    if (nowmax.F.F!=-inff&&nowmax.S.F!=-inff&&ans[2]>tmpsum-nowmax.F.F-nowmax.S.F)
        ans[2]=tmpsum-nowmax.F.F-nowmax.S.F,root=nowmax.F.S,root2=nowmax.S.S;
    maxx[now]=nowmax.F;
}

void dfs4(int now,int p)
{
     for (int i:G[now])
        if ((x[i]^y[i]^now)!=p)
    {
        tmpsum+=w[i];
        dfs4((x[i]^y[i]^now),now);
    }
}

int pp[maxn],weight[maxn],used[maxn];
int L[maxn],R[maxn],rL[maxn];
int Cnt=1;

struct segtree
{
    pii seg[maxn*4];
    int tag[maxn*4];

    void Build(int now,int l,int r)
    {
        if (l==r)
        {
            seg[now]=mkp(0,l);
            return;
        }
        int m=(l+r)>>1;
        Build(now*2,l,m);
        Build(now*2+1,m+1,r);
        seg[now]=max(seg[now*2],seg[now*2+1]);
    }

    void push(int now,int l,int r)
    {
        if (tag[now]==0||l==r)
        {
            tag[now]=0;
            return;
        }
        seg[now*2].F+=tag[now];
        tag[now*2]+=tag[now];
        seg[now*2+1].F+=tag[now];
        tag[now*2+1]+=tag[now];
        tag[now]=0;
    }

    void modify(int now,int l,int r,int ql,int qr,int v)
    {
        //cout<<now<<' '<<l<<' '<<r<<' '<<ql<<' '<<qr<<endl;
        if (l>=ql&&r<=qr)
        {
            seg[now].F+=v;
            tag[now]+=v;
            return;
        }
        int m=(l+r)>>1;
        push(now,l,r);
        if (ql<=m) modify(now*2,l,m,ql,qr,v);
        if (qr>m) modify(now*2+1,m+1,r,ql,qr,v);
        seg[now]=max(seg[now*2],seg[now*2+1]);
    }

    pii query()
    {
        return seg[1];
    }
}seg;

void dfs5(int now,int p)
{
    pp[now]=p;
    L[now]=Cnt++;
    rL[L[now]]=now;
    for (int i:G[now])
        if ((x[i]^y[i]^now)!=p)
    {
        weight[(x[i]^y[i]^now)]=w[i];
        dfs5((x[i]^y[i]^now),now);
    }
    R[now]=Cnt-1;
}

void use(int xx,int n)
{
    while (!used[xx])
    {
        used[xx]=1;
        seg.modify(1,1,n,L[xx],R[xx],-weight[xx]);
        xx=pp[xx];
    }
}

int32_t main()
{
    IOS
    int n;
    cin>>n;
    for (int i=1;i<=n;i++) ans[i]=inff;

    int _=0;
    for (int i=1;i<=n-1;i++)
    {
        int X,Y,Z,W;
        cin>>X>>Y>>Z>>W;
        x[_]=X,y[_]=Y,w[_]=Z;
        x[_^1]=Y,y[_^1]=X,w[_^1]=W;
        G[X].pb(_);
        G[Y].pb(_^1);
        _+=2;
        indeg[X]++;
        indeg[Y]++;
    }
    //1: calculate ans[1]
    ans[1]=0;
    dfs(1,-1);
    tmpsum=ans[1];
    dfs2(1,-1);
    //
    //2: ans[2]
    if (n==2)
    {
        ans[2]=0;
        int q;
        cin>>q;
        while (q--)
        {
            int X;
            cin>>X;
            cout<<ans[X]<<endl;
        }
        return 0;
    }
    for (int i=1;i<=n;i++)
        if (indeg[i]>=2)
        {
            tmpsum=0;
            dfs4(i,-1);
            dfs3(i,-1);
            break;
        }
    //
    //3: using ans[2] to calculate ans[i]
    seg.Build(1,1,n);
    dfs5(root,-1);
    for (int i=1;i<=n;i++)
        if (i!=root) seg.modify(1,1,n,L[i],R[i],weight[i]);
    used[root]=1;
    use(root2,n);

    //cout<<"Hi"<<endl;
    seg.modify(1,1,n,L[root],L[root],-inff);
    //cout<<seg.query().F<<endl;
    seg.modify(1,1,n,L[root2],L[root2],-inff);
   //cout<<seg.query().F<<endl;
    for (int i=3;i<=n;i++)
    {
        ans[i]=ans[i-1];
        pii ret=seg.query();
        ans[i]-=ret.F;
        use(rL[ret.S],n);
        seg.modify(1,1,n,ret.S,ret.S,-inff);
    }
    //
    int q;
    cin>>q;
    while (q--)
    {
        int X;
        cin>>X;
        cout<<ans[X]<<endl;
    }
    //
}

# Verdict Execution time Memory Grader output
1 Correct 14 ms 17664 KB Output is correct
2 Correct 13 ms 17664 KB Output is correct
3 Correct 14 ms 17664 KB Output is correct
4 Correct 15 ms 17664 KB Output is correct
5 Correct 14 ms 17664 KB Output is correct
6 Correct 13 ms 17664 KB Output is correct
7 Correct 15 ms 17664 KB Output is correct
8 Correct 14 ms 17640 KB Output is correct
9 Correct 14 ms 17664 KB Output is correct
10 Correct 14 ms 17664 KB Output is correct
11 Correct 14 ms 17604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 17584 KB Output is correct
2 Correct 943 ms 60540 KB Output is correct
3 Correct 1010 ms 77168 KB Output is correct
4 Correct 768 ms 59128 KB Output is correct
5 Correct 784 ms 60348 KB Output is correct
6 Correct 924 ms 63020 KB Output is correct
7 Correct 829 ms 60472 KB Output is correct
8 Correct 1074 ms 77736 KB Output is correct
9 Correct 632 ms 61156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 17716 KB Output is correct
2 Correct 1066 ms 60480 KB Output is correct
3 Correct 882 ms 80132 KB Output is correct
4 Correct 906 ms 59100 KB Output is correct
5 Correct 965 ms 60576 KB Output is correct
6 Correct 849 ms 63284 KB Output is correct
7 Correct 733 ms 60712 KB Output is correct
8 Correct 1128 ms 71428 KB Output is correct
9 Correct 634 ms 60644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 17664 KB Output is correct
2 Correct 13 ms 17664 KB Output is correct
3 Correct 14 ms 17664 KB Output is correct
4 Correct 15 ms 17664 KB Output is correct
5 Correct 14 ms 17664 KB Output is correct
6 Correct 13 ms 17664 KB Output is correct
7 Correct 15 ms 17664 KB Output is correct
8 Correct 14 ms 17640 KB Output is correct
9 Correct 14 ms 17664 KB Output is correct
10 Correct 14 ms 17664 KB Output is correct
11 Correct 14 ms 17604 KB Output is correct
12 Correct 14 ms 17664 KB Output is correct
13 Correct 20 ms 18048 KB Output is correct
14 Correct 18 ms 18176 KB Output is correct
15 Correct 20 ms 18176 KB Output is correct
16 Correct 19 ms 18176 KB Output is correct
17 Correct 17 ms 18144 KB Output is correct
18 Correct 19 ms 18048 KB Output is correct
19 Correct 19 ms 18048 KB Output is correct
20 Correct 17 ms 18164 KB Output is correct
21 Correct 18 ms 18176 KB Output is correct
22 Correct 20 ms 18048 KB Output is correct
23 Correct 17 ms 18048 KB Output is correct
24 Correct 18 ms 18176 KB Output is correct
25 Correct 17 ms 18208 KB Output is correct
26 Correct 16 ms 18176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 17584 KB Output is correct
2 Correct 943 ms 60540 KB Output is correct
3 Correct 1010 ms 77168 KB Output is correct
4 Correct 768 ms 59128 KB Output is correct
5 Correct 784 ms 60348 KB Output is correct
6 Correct 924 ms 63020 KB Output is correct
7 Correct 829 ms 60472 KB Output is correct
8 Correct 1074 ms 77736 KB Output is correct
9 Correct 632 ms 61156 KB Output is correct
10 Correct 13 ms 17716 KB Output is correct
11 Correct 1066 ms 60480 KB Output is correct
12 Correct 882 ms 80132 KB Output is correct
13 Correct 906 ms 59100 KB Output is correct
14 Correct 965 ms 60576 KB Output is correct
15 Correct 849 ms 63284 KB Output is correct
16 Correct 733 ms 60712 KB Output is correct
17 Correct 1128 ms 71428 KB Output is correct
18 Correct 634 ms 60644 KB Output is correct
19 Correct 16 ms 17664 KB Output is correct
20 Correct 949 ms 60544 KB Output is correct
21 Correct 891 ms 80548 KB Output is correct
22 Correct 912 ms 59040 KB Output is correct
23 Correct 788 ms 60564 KB Output is correct
24 Correct 940 ms 59404 KB Output is correct
25 Correct 855 ms 60592 KB Output is correct
26 Correct 790 ms 59412 KB Output is correct
27 Correct 875 ms 60492 KB Output is correct
28 Correct 821 ms 62576 KB Output is correct
29 Correct 803 ms 60592 KB Output is correct
30 Correct 786 ms 59612 KB Output is correct
31 Correct 718 ms 60780 KB Output is correct
32 Correct 900 ms 72224 KB Output is correct
33 Correct 582 ms 61044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 17664 KB Output is correct
2 Correct 13 ms 17664 KB Output is correct
3 Correct 14 ms 17664 KB Output is correct
4 Correct 15 ms 17664 KB Output is correct
5 Correct 14 ms 17664 KB Output is correct
6 Correct 13 ms 17664 KB Output is correct
7 Correct 15 ms 17664 KB Output is correct
8 Correct 14 ms 17640 KB Output is correct
9 Correct 14 ms 17664 KB Output is correct
10 Correct 14 ms 17664 KB Output is correct
11 Correct 14 ms 17604 KB Output is correct
12 Correct 14 ms 17584 KB Output is correct
13 Correct 943 ms 60540 KB Output is correct
14 Correct 1010 ms 77168 KB Output is correct
15 Correct 768 ms 59128 KB Output is correct
16 Correct 784 ms 60348 KB Output is correct
17 Correct 924 ms 63020 KB Output is correct
18 Correct 829 ms 60472 KB Output is correct
19 Correct 1074 ms 77736 KB Output is correct
20 Correct 632 ms 61156 KB Output is correct
21 Correct 13 ms 17716 KB Output is correct
22 Correct 1066 ms 60480 KB Output is correct
23 Correct 882 ms 80132 KB Output is correct
24 Correct 906 ms 59100 KB Output is correct
25 Correct 965 ms 60576 KB Output is correct
26 Correct 849 ms 63284 KB Output is correct
27 Correct 733 ms 60712 KB Output is correct
28 Correct 1128 ms 71428 KB Output is correct
29 Correct 634 ms 60644 KB Output is correct
30 Correct 14 ms 17664 KB Output is correct
31 Correct 20 ms 18048 KB Output is correct
32 Correct 18 ms 18176 KB Output is correct
33 Correct 20 ms 18176 KB Output is correct
34 Correct 19 ms 18176 KB Output is correct
35 Correct 17 ms 18144 KB Output is correct
36 Correct 19 ms 18048 KB Output is correct
37 Correct 19 ms 18048 KB Output is correct
38 Correct 17 ms 18164 KB Output is correct
39 Correct 18 ms 18176 KB Output is correct
40 Correct 20 ms 18048 KB Output is correct
41 Correct 17 ms 18048 KB Output is correct
42 Correct 18 ms 18176 KB Output is correct
43 Correct 17 ms 18208 KB Output is correct
44 Correct 16 ms 18176 KB Output is correct
45 Correct 16 ms 17664 KB Output is correct
46 Correct 949 ms 60544 KB Output is correct
47 Correct 891 ms 80548 KB Output is correct
48 Correct 912 ms 59040 KB Output is correct
49 Correct 788 ms 60564 KB Output is correct
50 Correct 940 ms 59404 KB Output is correct
51 Correct 855 ms 60592 KB Output is correct
52 Correct 790 ms 59412 KB Output is correct
53 Correct 875 ms 60492 KB Output is correct
54 Correct 821 ms 62576 KB Output is correct
55 Correct 803 ms 60592 KB Output is correct
56 Correct 786 ms 59612 KB Output is correct
57 Correct 718 ms 60780 KB Output is correct
58 Correct 900 ms 72224 KB Output is correct
59 Correct 582 ms 61044 KB Output is correct
60 Correct 15 ms 17536 KB Output is correct
61 Correct 911 ms 63212 KB Output is correct
62 Correct 953 ms 79104 KB Output is correct
63 Correct 905 ms 61844 KB Output is correct
64 Correct 1101 ms 63320 KB Output is correct
65 Correct 983 ms 61868 KB Output is correct
66 Correct 973 ms 63204 KB Output is correct
67 Correct 844 ms 61816 KB Output is correct
68 Correct 850 ms 63176 KB Output is correct
69 Correct 854 ms 65212 KB Output is correct
70 Correct 946 ms 63012 KB Output is correct
71 Correct 850 ms 62448 KB Output is correct
72 Correct 791 ms 63916 KB Output is correct
73 Correct 1024 ms 72800 KB Output is correct
74 Correct 701 ms 65236 KB Output is correct