답안 #885612

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
885612 2023-12-10T10:00:09 Z alexdd Hard route (IZhO17_road) C++17
19 / 100
2000 ms 197928 KB
#include<bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
using namespace std;
int n;
vector<int> con[500005];
map<int,pair<int,pair<int,int>>> dp[500005];
int root;
int depth[500005];
int siz[500005];
int maxd[500005];
int sus[500005];
int parent[500005];
pair<int,int> mxm = {0,0};
pair<int,pair<int,int>> combine(pair<int,pair<int,int>> x, pair<int,pair<int,int>> y)
{
    if(x.second.second==0)
        return y;
    if(y.second.second==0)
        return x;
    if(x.first > y.first)
        return x;
    if(x.first < y.first)
        return y;
    return {x.first, {x.second.first + y.second.first, x.second.second + y.second.second}};
}
void add_mxm_brut(int nod, set<pair<int,int>> s)
{
    for(auto adj:con[nod])
    {
        if(adj==parent[nod])
            continue;
        for(auto adj2:con[nod])
        {
            if(adj2==parent[nod] || adj>=adj2)
                continue;

            s.erase({maxd[adj],adj});
            s.erase({maxd[adj2],adj2});
            for(auto it:dp[adj])
            {
                for(auto it2:dp[adj2])
                {
                    pair<int,int> idk = *s.rbegin();
                    ///pair<int,int> aux = {(it.first + it2.first - 2*depth[nod]) * max({it.second.first,it2.second.first,sus[nod],idk.first-depth[nod]}), it.second.second * it2.second.second};
                    int moduri;

                    if(max(sus[nod],idk.first-depth[nod]) == max({it.second.first,it2.second.first,sus[nod],idk.first-depth[nod]}))
                    {
                        moduri = it.second.second.second * it2.second.second.second;
                    }
                    else if(it.second.first > it2.second.first)
                    {
                        moduri = it.second.second.first * it2.second.second.second;
                    }
                    else if(it.second.first < it2.second.first)
                    {
                        moduri = it2.second.second.first * it.second.second.second;
                    }
                    else
                    {
                        moduri = it.second.second.first * it2.second.second.second + it2.second.second.first * it.second.second.second - it.second.second.first * it2.second.second.first;
                    }

                    pair<int,int> aux = {(it.first + it2.first - 2*depth[nod]) * max({it.second.first,it2.second.first,sus[nod],idk.first-depth[nod]}), moduri};
                    if(aux.first > mxm.first)
                        mxm = aux;
                    else if(aux.first == mxm.first)
                        mxm.second += aux.second;
                }
            }
            s.insert({maxd[adj],adj});
            s.insert({maxd[adj2],adj2});
        }
    }
}
void add_mxm_fast(int nod)
{
    map<int,pair<int,pair<int,int>>> best;
    int maxpref=-1;
    for(int i=0;i<con[nod].size();i++)
    {
        int adj = con[nod][i];
        if(adj==parent[nod])
            continue;
        int idk=depth[nod];
        for(int j=(int)con[nod].size()-1;j>i;j--)
            idk = max(idk, maxd[con[nod][j]]);
        //cerr<<nod<<"   "<<i<<" "<<con[nod][i]<<"   "<<idk<<" idk\n";
        for(auto it:dp[adj])
        {
            for(auto it2:best)
            {
                int moduri;

                if(max(sus[nod],idk-depth[nod]) == max({it.second.first,it2.second.first,sus[nod],idk-depth[nod]}))
                {
                    moduri = it.second.second.second * it2.second.second.second;
                }
                else if(it.second.first > it2.second.first)
                {
                    moduri = it.second.second.first * it2.second.second.second;
                }
                else if(it.second.first < it2.second.first)
                {
                    moduri = it2.second.second.first * it.second.second.second;
                }
                else
                {
                    moduri = it.second.second.first * it2.second.second.second + it2.second.second.first * it.second.second.second - it.second.second.first * it2.second.second.first;
                }

                pair<int,int> aux = {(it.first + it2.first - 2*depth[nod]) * max({it.second.first,it2.second.first,sus[nod],idk-depth[nod]}), moduri};
                if(aux.first > mxm.first)
                    mxm = aux;
                else if(aux.first == mxm.first)
                    mxm.second += aux.second;
            }
        }
        for(auto&it:best)
        {
            if(it.second.first <= maxd[adj])
            {
                it.second = {maxd[adj],{it.second.second.second,it.second.second.second}};
            }
        }
        for(auto it:dp[adj])
        {
            pair<int,pair<int,int>> x = it.second;
            if(maxpref >= x.first)
                x = {maxpref, {x.second.second, x.second.second}};
            best[it.first] = combine(best[it.first], x);
        }
        maxpref = max(maxpref, maxd[adj]);
    }
}
void dfs_init(int nod, int par)
{
    parent[nod]=par;
    siz[nod]=1;
    maxd[nod]=depth[nod];
    for(auto adj:con[nod])
    {
        if(adj!=par)
        {
            depth[adj]=depth[nod]+1;
            dfs_init(adj,nod);
            maxd[nod] = max(maxd[nod], maxd[adj]);
            siz[nod]+=siz[adj];
        }
    }
}
void dfs(int nod)
{
    //cout<<nod<<" start\n";
    int cntc=0;
    set<pair<int,int>> s;
    s.insert({-1,-1});
    for(auto adj:con[nod])
    {
        if(adj!=parent[nod])
        {
            dfs(adj);
            s.insert({maxd[adj], adj});
            cntc++;
        }
    }
    add_mxm_brut(nod,s);
    //add_mxm_fast(nod);
    if(cntc==0)
    {
        dp[nod][depth[nod]] = {0,{1,1}};
    }
    else
    {
        for(auto adj:con[nod])
        {
            if(adj==parent[nod])
                continue;
            s.erase({maxd[adj],adj});
            pair<int,int> idk = *s.rbegin();
            //cout<<nod<<"   "<<adj<<"   "<<idk.first<<" "<<idk.second<<" idk\n";
            for(auto it:dp[adj])
            {
                pair<int,pair<int,int>> x = it.second;
                x.first = max(x.first, idk.first - depth[nod]);
                if(idk.first - depth[nod] >= x.first)
                {
                    x = {idk.first - depth[nod], {x.second.second, x.second.second}};
                }
                dp[nod][it.first] = combine(dp[nod][it.first], x);
            }
            s.insert({maxd[adj],adj});
            //dp[adj].clear();
        }
    }
    //for(auto it:dp[nod])
    //    cout<<nod<<"   "<<it.first<<"  "<<it.second.first<<" "<<it.second.second<<"\n";
    //cout<<nod<<" end\n";
}
void calc_sus()
{
    for(int i=1;i<=n;i++)
    {
        sus[i] = depth[i];
        int prec=i;
        for(int j=parent[i];j!=0;j=parent[j])
        {
            for(auto adj:con[j])
            {
                if(adj!=parent[j] && adj!=prec)
                {
                    sus[i] = max(sus[i], (maxd[adj] - depth[j]) + (depth[i] - depth[j]));
                }
            }
            prec=j;
        }
        //cout<<i<<" "<<sus[i]<<" sus\n";
    }
}
signed main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    cin>>n;
    if(n==2)
    {
        cout<<0<<" "<<1;
        return 0;
    }
    int a,b;
    for(int i=1;i<n;i++)
    {
        cin>>a>>b;
        con[a].push_back(b);
        con[b].push_back(a);
    }
    for(int i=n;i>0;i--)
        if((int)con[i].size()>1)
            root=i;
    //cerr<<root<<" is the root\n";
    dfs_init(root,0);
    calc_sus();
    dfs(root);
    cout<<mxm.first<<" "<<mxm.second<<"\n";
    return 0;
}
/**

7
1 2
1 3
2 4
2 5
4 6
4 7

*/

Compilation message

road.cpp: In function 'void add_mxm_fast(int)':
road.cpp:80:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   80 |     for(int i=0;i<con[nod].size();i++)
      |                 ~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 43100 KB Output is correct
2 Correct 8 ms 43100 KB Output is correct
3 Correct 9 ms 43096 KB Output is correct
4 Correct 9 ms 43100 KB Output is correct
5 Correct 10 ms 43100 KB Output is correct
6 Correct 8 ms 36952 KB Output is correct
7 Correct 9 ms 43100 KB Output is correct
8 Correct 8 ms 43196 KB Output is correct
9 Correct 8 ms 43100 KB Output is correct
10 Correct 9 ms 43256 KB Output is correct
11 Correct 9 ms 43324 KB Output is correct
12 Correct 9 ms 43100 KB Output is correct
13 Correct 9 ms 43100 KB Output is correct
14 Correct 9 ms 43340 KB Output is correct
15 Correct 8 ms 43096 KB Output is correct
16 Correct 9 ms 43100 KB Output is correct
17 Correct 9 ms 43172 KB Output is correct
18 Correct 9 ms 43100 KB Output is correct
19 Correct 9 ms 43100 KB Output is correct
20 Correct 9 ms 43284 KB Output is correct
21 Correct 9 ms 43100 KB Output is correct
22 Correct 9 ms 43100 KB Output is correct
23 Correct 9 ms 43100 KB Output is correct
24 Correct 9 ms 43100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 43100 KB Output is correct
2 Correct 8 ms 43100 KB Output is correct
3 Correct 9 ms 43096 KB Output is correct
4 Correct 9 ms 43100 KB Output is correct
5 Correct 10 ms 43100 KB Output is correct
6 Correct 8 ms 36952 KB Output is correct
7 Correct 9 ms 43100 KB Output is correct
8 Correct 8 ms 43196 KB Output is correct
9 Correct 8 ms 43100 KB Output is correct
10 Correct 9 ms 43256 KB Output is correct
11 Correct 9 ms 43324 KB Output is correct
12 Correct 9 ms 43100 KB Output is correct
13 Correct 9 ms 43100 KB Output is correct
14 Correct 9 ms 43340 KB Output is correct
15 Correct 8 ms 43096 KB Output is correct
16 Correct 9 ms 43100 KB Output is correct
17 Correct 9 ms 43172 KB Output is correct
18 Correct 9 ms 43100 KB Output is correct
19 Correct 9 ms 43100 KB Output is correct
20 Correct 9 ms 43284 KB Output is correct
21 Correct 9 ms 43100 KB Output is correct
22 Correct 9 ms 43100 KB Output is correct
23 Correct 9 ms 43100 KB Output is correct
24 Correct 9 ms 43100 KB Output is correct
25 Correct 34 ms 45676 KB Output is correct
26 Correct 26 ms 44880 KB Output is correct
27 Correct 32 ms 45408 KB Output is correct
28 Correct 24 ms 44632 KB Output is correct
29 Correct 335 ms 166024 KB Output is correct
30 Correct 414 ms 197928 KB Output is correct
31 Correct 416 ms 195432 KB Output is correct
32 Correct 282 ms 150356 KB Output is correct
33 Correct 30 ms 44884 KB Output is correct
34 Correct 35 ms 45392 KB Output is correct
35 Correct 26 ms 44376 KB Output is correct
36 Correct 27 ms 44456 KB Output is correct
37 Correct 46 ms 44536 KB Output is correct
38 Correct 84 ms 45140 KB Output is correct
39 Correct 35 ms 44116 KB Output is correct
40 Correct 28 ms 44116 KB Output is correct
41 Correct 19 ms 43856 KB Output is correct
42 Correct 14 ms 43612 KB Output is correct
43 Correct 15 ms 43768 KB Output is correct
44 Correct 18 ms 43612 KB Output is correct
45 Correct 48 ms 43612 KB Output is correct
46 Correct 150 ms 43608 KB Output is correct
47 Correct 872 ms 44012 KB Output is correct
48 Execution timed out 2061 ms 44188 KB Time limit exceeded
49 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 43100 KB Output is correct
2 Correct 8 ms 43100 KB Output is correct
3 Correct 9 ms 43096 KB Output is correct
4 Correct 9 ms 43100 KB Output is correct
5 Correct 10 ms 43100 KB Output is correct
6 Correct 8 ms 36952 KB Output is correct
7 Correct 9 ms 43100 KB Output is correct
8 Correct 8 ms 43196 KB Output is correct
9 Correct 8 ms 43100 KB Output is correct
10 Correct 9 ms 43256 KB Output is correct
11 Correct 9 ms 43324 KB Output is correct
12 Correct 9 ms 43100 KB Output is correct
13 Correct 9 ms 43100 KB Output is correct
14 Correct 9 ms 43340 KB Output is correct
15 Correct 8 ms 43096 KB Output is correct
16 Correct 9 ms 43100 KB Output is correct
17 Correct 9 ms 43172 KB Output is correct
18 Correct 9 ms 43100 KB Output is correct
19 Correct 9 ms 43100 KB Output is correct
20 Correct 9 ms 43284 KB Output is correct
21 Correct 9 ms 43100 KB Output is correct
22 Correct 9 ms 43100 KB Output is correct
23 Correct 9 ms 43100 KB Output is correct
24 Correct 9 ms 43100 KB Output is correct
25 Correct 34 ms 45676 KB Output is correct
26 Correct 26 ms 44880 KB Output is correct
27 Correct 32 ms 45408 KB Output is correct
28 Correct 24 ms 44632 KB Output is correct
29 Correct 335 ms 166024 KB Output is correct
30 Correct 414 ms 197928 KB Output is correct
31 Correct 416 ms 195432 KB Output is correct
32 Correct 282 ms 150356 KB Output is correct
33 Correct 30 ms 44884 KB Output is correct
34 Correct 35 ms 45392 KB Output is correct
35 Correct 26 ms 44376 KB Output is correct
36 Correct 27 ms 44456 KB Output is correct
37 Correct 46 ms 44536 KB Output is correct
38 Correct 84 ms 45140 KB Output is correct
39 Correct 35 ms 44116 KB Output is correct
40 Correct 28 ms 44116 KB Output is correct
41 Correct 19 ms 43856 KB Output is correct
42 Correct 14 ms 43612 KB Output is correct
43 Correct 15 ms 43768 KB Output is correct
44 Correct 18 ms 43612 KB Output is correct
45 Correct 48 ms 43612 KB Output is correct
46 Correct 150 ms 43608 KB Output is correct
47 Correct 872 ms 44012 KB Output is correct
48 Execution timed out 2061 ms 44188 KB Time limit exceeded
49 Halted 0 ms 0 KB -