답안 #1051040

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1051040 2024-08-09T18:43:38 Z Ahmed57 Hard route (IZhO17_road) C++17
52 / 100
342 ms 89928 KB
#include "bits/stdc++.h"

using namespace std;
vector<int> adj[500001];
int mx[500001];
int cnt[500001];
pair<int,int> pref[500001];
long long overMA = 0;
long long overCNT = 1;
void dfs(int i,int pr){
    mx[i] = 0 , cnt[i] = 1;
    for(auto j:adj[i]){
        if(j==pr)continue;
        dfs(j,i);
        if(mx[j]+1>mx[i]){
            mx[i] = mx[j]+1;
            cnt[i] = 0;
        }
        if(mx[j]+1==mx[i]){
            cnt[i]+=cnt[j];
        }
    }
}
void reroot(int i,int pr,int MA,int CNT){
    vector<pair<int,int>> v;
    if(MA)v.push_back({MA,CNT});
    for(auto j:adj[i]){
        if(j==pr)continue;
        v.push_back({mx[j]+1,cnt[j]});
    }
    sort(v.begin(),v.end());
    reverse(v.begin(),v.end());
    if(v.size()>=3){
        int z = v[0].first , y = v[1].first , x = v[2].first;
        long long cost = 0;
        if(x<y&&y<z){
            int A = v[0].second , B = v[1].second , C = v[2].second;
            for(int i = 3;i<v.size();i++)if(v[i].first==x)C+=v[i].second;
            cost = B*C;
        }else if(x<y&&y==z){
            int A = v[0].second , B = v[1].second , C = v[2].second;
            for(int i = 3;i<v.size();i++)if(v[i].first==x)C+=v[i].second;
            cost = (A+B)*C;
        }else if(x==y&&y<z){
            int A = v[0].second , B = v[1].second , C = v[2].second;
            cost = B*C;
            long long sum = B+C;
            for(int i = 3;i<v.size();i++){
                if(v[i].first==x){
                    cost+=sum*v[i].second;
                    sum+=v[i].second;
                }
            }
        }else if(x==y&&y==z){
            int A = v[0].second , B = v[1].second , C = v[2].second;
            cost = A*B+(A+B)*C;
            long long sum = A+B+C;
            for(int i = 3;i<v.size();i++){
                if(v[i].first==x){
                    cost+=sum*v[i].second;
                    sum+=v[i].second;
                }
            }
        }
        if(overMA<(x+y)*z){
            overMA = (x+y)*z;
            overCNT = 0;
        }
        if(overMA==(x+y)*z){
            overCNT+=cost;
        }
    }
    pair<int,int> cur = {MA,CNT};
    for(auto j:adj[i]){
        if(j==pr)continue;
        pref[j] = cur;
        if(mx[j]+1>cur.first){
            cur.first = mx[j]+1;
            cur.second = 0;
        }
        if(mx[j]+1==cur.first){
            cur.second+=cnt[j];
        }
    }
    reverse(adj[i].begin(),adj[i].end());
    cur = {-1e9,0};
    for(auto j:adj[i]){
        if(j==pr)continue;
        if(cur.first>pref[j].first)pref[j] = cur;
        else if(cur.first==pref[j].first)pref[j].second+=cur.second;
        if(mx[j]+1>cur.first){
            cur.first = mx[j]+1;
            cur.second = 0;
        }
        if(mx[j]+1==cur.first){
            cur.second+=cnt[j];
        }
    }
    reverse(adj[i].begin(),adj[i].end());
    for(auto j:adj[i]){
        if(j==pr)continue;
        reroot(j,i,pref[j].first+1,pref[j].second);
    }
}
int main(){
    int n;
    cin>>n;
    for(int i = 1;i<n;i++){
        int a,b;
        cin>>a>>b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    dfs(1,0);
    reroot(1,0,0,1);
    cout<<overMA<<" "<<overCNT<<endl;
}

Compilation message

road.cpp: In function 'void reroot(int, int, int, int)':
road.cpp:38:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |             for(int i = 3;i<v.size();i++)if(v[i].first==x)C+=v[i].second;
      |                           ~^~~~~~~~~
road.cpp:37:17: warning: unused variable 'A' [-Wunused-variable]
   37 |             int A = v[0].second , B = v[1].second , C = v[2].second;
      |                 ^
road.cpp:42:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |             for(int i = 3;i<v.size();i++)if(v[i].first==x)C+=v[i].second;
      |                           ~^~~~~~~~~
road.cpp:48:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   48 |             for(int i = 3;i<v.size();i++){
      |                           ~^~~~~~~~~
road.cpp:45:17: warning: unused variable 'A' [-Wunused-variable]
   45 |             int A = v[0].second , B = v[1].second , C = v[2].second;
      |                 ^
road.cpp:58:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |             for(int i = 3;i<v.size();i++){
      |                           ~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 17756 KB Output is correct
2 Correct 2 ms 17756 KB Output is correct
3 Correct 2 ms 17756 KB Output is correct
4 Correct 2 ms 17756 KB Output is correct
5 Correct 2 ms 17756 KB Output is correct
6 Correct 2 ms 17756 KB Output is correct
7 Correct 2 ms 17756 KB Output is correct
8 Correct 2 ms 17756 KB Output is correct
9 Correct 2 ms 17752 KB Output is correct
10 Correct 2 ms 17756 KB Output is correct
11 Correct 2 ms 17756 KB Output is correct
12 Correct 2 ms 17756 KB Output is correct
13 Correct 2 ms 17756 KB Output is correct
14 Correct 2 ms 17756 KB Output is correct
15 Correct 2 ms 17756 KB Output is correct
16 Correct 2 ms 17756 KB Output is correct
17 Correct 2 ms 17756 KB Output is correct
18 Correct 2 ms 17756 KB Output is correct
19 Correct 2 ms 17780 KB Output is correct
20 Correct 2 ms 17600 KB Output is correct
21 Correct 2 ms 17756 KB Output is correct
22 Correct 2 ms 17756 KB Output is correct
23 Correct 2 ms 17756 KB Output is correct
24 Correct 2 ms 17756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 17756 KB Output is correct
2 Correct 2 ms 17756 KB Output is correct
3 Correct 2 ms 17756 KB Output is correct
4 Correct 2 ms 17756 KB Output is correct
5 Correct 2 ms 17756 KB Output is correct
6 Correct 2 ms 17756 KB Output is correct
7 Correct 2 ms 17756 KB Output is correct
8 Correct 2 ms 17756 KB Output is correct
9 Correct 2 ms 17752 KB Output is correct
10 Correct 2 ms 17756 KB Output is correct
11 Correct 2 ms 17756 KB Output is correct
12 Correct 2 ms 17756 KB Output is correct
13 Correct 2 ms 17756 KB Output is correct
14 Correct 2 ms 17756 KB Output is correct
15 Correct 2 ms 17756 KB Output is correct
16 Correct 2 ms 17756 KB Output is correct
17 Correct 2 ms 17756 KB Output is correct
18 Correct 2 ms 17756 KB Output is correct
19 Correct 2 ms 17780 KB Output is correct
20 Correct 2 ms 17600 KB Output is correct
21 Correct 2 ms 17756 KB Output is correct
22 Correct 2 ms 17756 KB Output is correct
23 Correct 2 ms 17756 KB Output is correct
24 Correct 2 ms 17756 KB Output is correct
25 Correct 4 ms 18012 KB Output is correct
26 Correct 4 ms 18268 KB Output is correct
27 Correct 4 ms 18268 KB Output is correct
28 Correct 4 ms 18268 KB Output is correct
29 Correct 4 ms 18140 KB Output is correct
30 Correct 4 ms 18184 KB Output is correct
31 Correct 4 ms 18336 KB Output is correct
32 Correct 4 ms 18268 KB Output is correct
33 Correct 5 ms 18268 KB Output is correct
34 Correct 4 ms 18272 KB Output is correct
35 Correct 4 ms 18268 KB Output is correct
36 Correct 4 ms 18268 KB Output is correct
37 Correct 4 ms 18524 KB Output is correct
38 Correct 4 ms 18780 KB Output is correct
39 Correct 4 ms 18080 KB Output is correct
40 Correct 4 ms 18012 KB Output is correct
41 Correct 4 ms 18012 KB Output is correct
42 Correct 4 ms 17828 KB Output is correct
43 Correct 4 ms 17752 KB Output is correct
44 Correct 4 ms 17756 KB Output is correct
45 Correct 4 ms 17756 KB Output is correct
46 Correct 4 ms 17756 KB Output is correct
47 Correct 4 ms 18012 KB Output is correct
48 Correct 4 ms 18012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 17756 KB Output is correct
2 Correct 2 ms 17756 KB Output is correct
3 Correct 2 ms 17756 KB Output is correct
4 Correct 2 ms 17756 KB Output is correct
5 Correct 2 ms 17756 KB Output is correct
6 Correct 2 ms 17756 KB Output is correct
7 Correct 2 ms 17756 KB Output is correct
8 Correct 2 ms 17756 KB Output is correct
9 Correct 2 ms 17752 KB Output is correct
10 Correct 2 ms 17756 KB Output is correct
11 Correct 2 ms 17756 KB Output is correct
12 Correct 2 ms 17756 KB Output is correct
13 Correct 2 ms 17756 KB Output is correct
14 Correct 2 ms 17756 KB Output is correct
15 Correct 2 ms 17756 KB Output is correct
16 Correct 2 ms 17756 KB Output is correct
17 Correct 2 ms 17756 KB Output is correct
18 Correct 2 ms 17756 KB Output is correct
19 Correct 2 ms 17780 KB Output is correct
20 Correct 2 ms 17600 KB Output is correct
21 Correct 2 ms 17756 KB Output is correct
22 Correct 2 ms 17756 KB Output is correct
23 Correct 2 ms 17756 KB Output is correct
24 Correct 2 ms 17756 KB Output is correct
25 Correct 4 ms 18012 KB Output is correct
26 Correct 4 ms 18268 KB Output is correct
27 Correct 4 ms 18268 KB Output is correct
28 Correct 4 ms 18268 KB Output is correct
29 Correct 4 ms 18140 KB Output is correct
30 Correct 4 ms 18184 KB Output is correct
31 Correct 4 ms 18336 KB Output is correct
32 Correct 4 ms 18268 KB Output is correct
33 Correct 5 ms 18268 KB Output is correct
34 Correct 4 ms 18272 KB Output is correct
35 Correct 4 ms 18268 KB Output is correct
36 Correct 4 ms 18268 KB Output is correct
37 Correct 4 ms 18524 KB Output is correct
38 Correct 4 ms 18780 KB Output is correct
39 Correct 4 ms 18080 KB Output is correct
40 Correct 4 ms 18012 KB Output is correct
41 Correct 4 ms 18012 KB Output is correct
42 Correct 4 ms 17828 KB Output is correct
43 Correct 4 ms 17752 KB Output is correct
44 Correct 4 ms 17756 KB Output is correct
45 Correct 4 ms 17756 KB Output is correct
46 Correct 4 ms 17756 KB Output is correct
47 Correct 4 ms 18012 KB Output is correct
48 Correct 4 ms 18012 KB Output is correct
49 Correct 314 ms 77648 KB Output is correct
50 Correct 342 ms 84380 KB Output is correct
51 Correct 307 ms 89928 KB Output is correct
52 Correct 302 ms 69968 KB Output is correct
53 Incorrect 291 ms 83904 KB Output isn't correct
54 Halted 0 ms 0 KB -