답안 #734324

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
734324 2023-05-02T09:36:27 Z bin9638 도로 폐쇄 (APIO21_roads) C++11
14 / 100
2000 ms 20244 KB
#include <bits/stdc++.h>

#ifndef SKY
#include "roads.h"
#endif // SKY

using namespace std;

#define N 100010
#define ll long long
#define fs first
#define sc second
#define ii pair<ll,int>
#define pb push_back

#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("omit-frame-pointer")
#pragma GCC optimize("unroll-loops")

const ll MAX_VAL=2e14;

int cnt,BLOCKS,k,n,ktr[N],spec[N];
vector<ii>g[N],edge[N];
ll tong,them,cost[N],dp[N][2];
vector<ll>sum[N];
vector<int>lis;

struct nho
{
    void build(int u,int p)
    {
        for(auto v:g[u])
            if(v.sc!=p)
            {
                build(v.sc,u);
                cost[v.sc]=v.fs;
            }
    }

    ll solve(int u,int p,int sl)
    {

        if(sl<0)
            return MAX_VAL;
        ll res=0;
        vector<ll>s;
        for(auto v:g[u])
            if(v.sc!=p)
                if(dp[v.sc][0]<=dp[v.sc][1])
                {
                    res+=dp[v.sc][0];
                }else
                {
                    res+=dp[v.sc][1];
                    s.pb(dp[v.sc][0]-dp[v.sc][1]);
                }
        sort(s.begin(),s.end());
        for(int i=0;i<(int)s.size()-sl;i++)
            res+=s[i];
        return res;
    }

    void DFS(int u,int p)
    {
        for(auto v:g[u])
            if(v.sc!=p)
                DFS(v.sc,u);
        //0
        dp[u][0]=solve(u,p,k)+cost[u];
        //1
        dp[u][1]=solve(u,p,k-(u!=0));
    }
}nho;

ll get(int u,int sl)
{
    if(sl>=sum[u].size())
        return 0;
    if(sl==0)
        return sum[u].back();
    return sum[u].back()-sum[u][sl-1];
}

ll solve(int u,int p,int sl)
{
    if(sl<0)
        return MAX_VAL;
    ll cong=0,res=MAX_VAL;
    vector<ll>s;
    for(auto v:edge[u])
        if(v.sc!=p)
        {
             if(dp[v.sc][0]<=dp[v.sc][1])
                {
                    cong+=dp[v.sc][0];
                }else
                {
                    cong+=dp[v.sc][1];
                    s.pb(dp[v.sc][0]-dp[v.sc][1]);
                }
        }
    //sort(s.begin(),s.end());
    if((int)s.size()<=sl)
        res=min(res,cong+get(u,sl-(int)s.size()));
    for(int i=0;i<s.size();i++)
    {
        cong+=s[i];
        if((int)s.size()-i-1<=sl)
            res=min(res,cong+get(u,sl-((int)s.size()-i-1)));
        if(sl-((int)s.size()-i-1)>=sum[u].size())
            break;
    }
    return res;
}

void DFS(int u,int p)
{
    ktr[u]=k;
    for(auto v:edge[u])
        if(v.sc!=p)
        {
            cost[v.sc]=v.fs;
            DFS(v.sc,u);
        }
    //0
    dp[u][0]=solve(u,p,k)+cost[u];
    //1
    dp[u][1]=solve(u,p,k-(p!=-1));
}

vector<long long> minimum_closure_costs(int NNN, vector<int> UUU, vector<int> VVV, vector<int> WWW)
{
    n=NNN;
    for(int i=0;i<n-1;i++)
    {
        int u=UUU[i],v=VVV[i],w=WWW[i];
        g[u].pb({w,v});
        g[v].pb({w,u});
    }
    cost[0]=1e18;
    nho.build(0,-1);
    vector<ll>kq;
    BLOCKS=sqrt(n);
      for(int i=0;i<n;i++)
        spec[i]=((int)g[i].size()>BLOCKS);
    for(int i=0;i<n-1;i++)
    {
        int u=UUU[i],v=VVV[i],w=WWW[i];
        tong+=1ll*w;
        if((int)g[u].size()<(int)g[v].size())
            swap(u,v);
        if(spec[u]&&spec[v])
        {
            edge[u].pb({w,v});
            edge[v].pb({w,u});
            cnt++;
            continue;
        }
        if(spec[u])
        {
            sum[u].pb(w);
            continue;
        }
        them+=1ll*w;
    }
    assert(cnt<=BLOCKS);
    for(k=0;k<=BLOCKS;k++)
    {
        nho.DFS(0,-1);
        kq.pb(dp[0][1]);
    }
    for(int i=0;i<n;i++)
    {
        sort(sum[i].begin(),sum[i].end(),greater<ll>());
        for(int j=1;j<sum[i].size();j++)
            sum[i][j]+=sum[i][j-1];
    }
    for(int i=0;i<n;i++)
        if(spec[i]==1)
            lis.pb(i);
    memset(cost,0x3f3f,sizeof(cost));
    for(k=BLOCKS+1;k<n;k++)
    {
        ll res=0;
        for(auto u:lis)
            if(ktr[u]!=k)
                DFS(u,-1),res+=dp[u][1];
        kq.pb(res);
    }
    return kq;
}

#ifdef SKY
int main()
{
    freopen("A.inp","r",stdin);
    freopen("A.out","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    int n;
    cin>>n;
    vector<int>U(n-1),V(n-1),W(n-1);
    for(int i=0;i<n-1;i++)
        cin>>U[i]>>V[i]>>W[i];
    vector<ll>kq=minimum_closure_costs(n,U,V,W);
    for(auto u:kq)cout<<u<<" ";
    return 0;
}
#endif

Compilation message

roads.cpp: In member function 'long long int nho::solve(int, int, int)':
roads.cpp:49:15: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
   49 |             if(v.sc!=p)
      |               ^
roads.cpp: In function 'long long int get(int, int)':
roads.cpp:78:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |     if(sl>=sum[u].size())
      |        ~~^~~~~~~~~~~~~~~
roads.cpp: In function 'long long int solve(int, int, int)':
roads.cpp:106:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  106 |     for(int i=0;i<s.size();i++)
      |                 ~^~~~~~~~~
roads.cpp:111:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  111 |         if(sl-((int)s.size()-i-1)>=sum[u].size())
      |            ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
roads.cpp: In function 'std::vector<long long int> minimum_closure_costs(int, std::vector<int>, std::vector<int>, std::vector<int>)':
roads.cpp:176:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  176 |         for(int j=1;j<sum[i].size();j++)
      |                     ~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8020 KB Output is correct
2 Correct 13 ms 8276 KB Output is correct
3 Correct 15 ms 8380 KB Output is correct
4 Correct 12 ms 8380 KB Output is correct
5 Correct 4 ms 8148 KB Output is correct
6 Correct 5 ms 8148 KB Output is correct
7 Correct 4 ms 8148 KB Output is correct
8 Correct 9 ms 8264 KB Output is correct
9 Correct 10 ms 8276 KB Output is correct
10 Correct 4 ms 8148 KB Output is correct
11 Correct 4 ms 8148 KB Output is correct
12 Correct 1176 ms 15220 KB Output is correct
13 Execution timed out 2078 ms 20244 KB Time limit exceeded
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 8020 KB Output is correct
2 Execution timed out 2069 ms 19552 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8020 KB Output is correct
2 Correct 5 ms 8020 KB Output is correct
3 Correct 5 ms 8020 KB Output is correct
4 Correct 5 ms 8148 KB Output is correct
5 Correct 5 ms 8148 KB Output is correct
6 Correct 7 ms 8076 KB Output is correct
7 Correct 5 ms 8148 KB Output is correct
8 Correct 5 ms 8148 KB Output is correct
9 Correct 5 ms 8148 KB Output is correct
10 Correct 5 ms 8148 KB Output is correct
11 Correct 5 ms 8148 KB Output is correct
12 Correct 5 ms 8148 KB Output is correct
13 Correct 4 ms 8148 KB Output is correct
14 Correct 5 ms 8148 KB Output is correct
15 Correct 6 ms 8148 KB Output is correct
16 Correct 4 ms 8148 KB Output is correct
17 Correct 5 ms 8148 KB Output is correct
18 Correct 7 ms 8148 KB Output is correct
19 Correct 5 ms 8148 KB Output is correct
20 Correct 5 ms 8148 KB Output is correct
21 Correct 4 ms 8020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8020 KB Output is correct
2 Correct 5 ms 8020 KB Output is correct
3 Correct 5 ms 8020 KB Output is correct
4 Correct 5 ms 8148 KB Output is correct
5 Correct 5 ms 8148 KB Output is correct
6 Correct 7 ms 8076 KB Output is correct
7 Correct 5 ms 8148 KB Output is correct
8 Correct 5 ms 8148 KB Output is correct
9 Correct 5 ms 8148 KB Output is correct
10 Correct 5 ms 8148 KB Output is correct
11 Correct 5 ms 8148 KB Output is correct
12 Correct 5 ms 8148 KB Output is correct
13 Correct 4 ms 8148 KB Output is correct
14 Correct 5 ms 8148 KB Output is correct
15 Correct 6 ms 8148 KB Output is correct
16 Correct 4 ms 8148 KB Output is correct
17 Correct 5 ms 8148 KB Output is correct
18 Correct 7 ms 8148 KB Output is correct
19 Correct 5 ms 8148 KB Output is correct
20 Correct 5 ms 8148 KB Output is correct
21 Correct 4 ms 8020 KB Output is correct
22 Correct 6 ms 8148 KB Output is correct
23 Correct 11 ms 8240 KB Output is correct
24 Correct 17 ms 8276 KB Output is correct
25 Correct 14 ms 8344 KB Output is correct
26 Correct 14 ms 8276 KB Output is correct
27 Incorrect 16 ms 8404 KB Output isn't correct
28 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2083 ms 17444 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2083 ms 17444 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8020 KB Output is correct
2 Correct 13 ms 8276 KB Output is correct
3 Correct 15 ms 8380 KB Output is correct
4 Correct 12 ms 8380 KB Output is correct
5 Correct 4 ms 8148 KB Output is correct
6 Correct 5 ms 8148 KB Output is correct
7 Correct 4 ms 8148 KB Output is correct
8 Correct 9 ms 8264 KB Output is correct
9 Correct 10 ms 8276 KB Output is correct
10 Correct 4 ms 8148 KB Output is correct
11 Correct 4 ms 8148 KB Output is correct
12 Correct 1176 ms 15220 KB Output is correct
13 Execution timed out 2078 ms 20244 KB Time limit exceeded
14 Halted 0 ms 0 KB -