답안 #210971

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
210971 2020-03-19T04:39:49 Z HNO2 Designated Cities (JOI19_designated_cities) C++17
7 / 100
451 ms 41720 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];

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]=min(ans[2],tmpsum-nowmax.F.F-nowmax.S.F);
}

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;
    }
    int tmproot;
    for (int i=1;i<=n;i++)
        if (indeg[i]>=2) tmproot=i;
    dfs3(tmproot,-1);
    //
    //3: using ans[2] to calculate ans[i]
    //
    int q;
    cin>>q;
    while (q--)
    {
        int X;
        cin>>X;
        cout<<ans[X]<<endl;
    }
    //
}

Compilation message

designated_cities.cpp: In function 'int32_t main()':
designated_cities.cpp:129:9: warning: 'tmproot' may be used uninitialized in this function [-Wmaybe-uninitialized]
     dfs3(tmproot,-1);
     ~~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Incorrect 7 ms 5120 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 342 ms 28024 KB Output is correct
3 Correct 444 ms 35880 KB Output is correct
4 Correct 345 ms 28024 KB Output is correct
5 Correct 335 ms 28016 KB Output is correct
6 Correct 363 ms 30200 KB Output is correct
7 Correct 269 ms 28008 KB Output is correct
8 Correct 451 ms 41720 KB Output is correct
9 Correct 192 ms 28520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Incorrect 360 ms 28024 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Incorrect 7 ms 5120 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 342 ms 28024 KB Output is correct
3 Correct 444 ms 35880 KB Output is correct
4 Correct 345 ms 28024 KB Output is correct
5 Correct 335 ms 28016 KB Output is correct
6 Correct 363 ms 30200 KB Output is correct
7 Correct 269 ms 28008 KB Output is correct
8 Correct 451 ms 41720 KB Output is correct
9 Correct 192 ms 28520 KB Output is correct
10 Correct 7 ms 5120 KB Output is correct
11 Incorrect 360 ms 28024 KB Output isn't correct
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Incorrect 7 ms 5120 KB Output isn't correct
3 Halted 0 ms 0 KB -