답안 #973866

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
973866 2024-05-02T12:05:16 Z PieArmy Hard route (IZhO17_road) C++17
100 / 100
552 ms 98108 KB
typedef long long ll;
ll pie(ll army){return (1ll<<army);}
#include <bits/stdc++.h>
#define fr first
#define sc second
#define pb push_back
#define endl '\n'
#define mid ((left+right)>>1)
const ll inf=2000000000000000005;
const int sonsuz=2000000005;
using namespace std;
ll fpow(ll x,ll y,ll m=0){if(y<0){cout<<"powError";return -1;}if(m)x%=m;ll res=1;while(y>0){if(y&1)res*=x;x*=x;if(m){x%=m;res%=m;}y>>=1;}return res;}

int n;
ll dp[500001][3][3],ans=0,say=1;
vector<int>komsu[500001];

void ekle(int loc,int x,ll cnt){
    if(x>dp[loc][0][0]){
        dp[loc][2][0]=dp[loc][1][0];
        dp[loc][2][1]=dp[loc][1][1];
        dp[loc][2][2]=dp[loc][1][2];
        dp[loc][1][0]=dp[loc][0][0];
        dp[loc][1][1]=dp[loc][0][1];
        dp[loc][1][2]=dp[loc][0][2];
        dp[loc][0][0]=x;
        dp[loc][0][1]=0;
        dp[loc][0][2]=0;
    }
    else if(x>dp[loc][1][0]){
        dp[loc][2][0]=dp[loc][1][0];
        dp[loc][2][1]=dp[loc][1][1];
        dp[loc][2][2]=dp[loc][1][2];
        dp[loc][1][0]=x;
        dp[loc][1][1]=0;
        dp[loc][1][2]=0;
        if(x==dp[loc][0][0]){
            dp[loc][1][1]=dp[loc][0][1];
            dp[loc][1][2]=dp[loc][0][2];
        }
    }
    else if(x>dp[loc][2][0]){
        dp[loc][2][0]=x;
        dp[loc][2][1]=0;
        dp[loc][2][2]=0;
        if(x==dp[loc][1][0]){
            dp[loc][2][1]=dp[loc][1][1];
            dp[loc][2][2]=dp[loc][1][2];
        }
    }
    for(int i=0;i<3;i++){
        if(x==dp[loc][i][0]){
            dp[loc][i][2]+=dp[loc][i][1]*cnt;
            dp[loc][i][1]+=cnt;
        }
    }
}

void dfs1(int pos,int root){
    dp[pos][0][1]=1;
    for(int x:komsu[pos]){
        if(x==root)continue;
        dfs1(x,pos);
        ekle(pos,dp[x][0][0]+1,dp[x][0][1]);
    }
}

void dfs2(int pos,int root){
    if(root){
        if(dp[root][0][0]-1==dp[pos][0][0]){
            ll y=dp[root][1][1];
            if(dp[root][1][0]-1==dp[pos][0][0]){
                y-=dp[pos][0][1];
            }
            ekle(pos,dp[root][1][0]+1,y);
        }
        else{
            ekle(pos,dp[root][0][0]+1,dp[root][0][1]);
        }
    }
    if(dp[pos][0][0]&&dp[pos][1][0]&&dp[pos][2][0]){
        ll art=0;
        if(dp[pos][1][0]==dp[pos][2][0]){
            art=dp[pos][1][2];
        }
        else{
            art=dp[pos][1][1]*dp[pos][2][1];
        }
        if(dp[pos][0][0]*(dp[pos][1][0]+dp[pos][2][0])>ans){
            ans=dp[pos][0][0]*(dp[pos][1][0]+dp[pos][2][0]);
            say=art;
        }
        else if(dp[pos][0][0]*(dp[pos][1][0]+dp[pos][2][0])==ans){
            say+=art;
        }
    }
    for(int x:komsu[pos]){
        if(x==root)continue;
        dfs2(x,pos);
    }
}

void code(){
    cin>>n;
    for(int i=1;i<n;i++){
        int a,b;cin>>a>>b;
        komsu[a].pb(b);
        komsu[b].pb(a);
    }
    dfs1(1,0);
    dfs2(1,0);
    cout<<ans<<" "<<say;
}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(NULL);
	bool usaco=0;if(usaco){freopen(".in","r",stdin);freopen(".out","w",stdout);}
	int t=1;
	if(!t)cin>>t;
	while(t--){code();cout<<endl;}
    return 0;
}

Compilation message

road.cpp: In function 'int main()':
road.cpp:117:32: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  117 |  bool usaco=0;if(usaco){freopen(".in","r",stdin);freopen(".out","w",stdout);}
      |                         ~~~~~~~^~~~~~~~~~~~~~~~~
road.cpp:117:57: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  117 |  bool usaco=0;if(usaco){freopen(".in","r",stdin);freopen(".out","w",stdout);}
      |                                                  ~~~~~~~^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12892 KB Output is correct
2 Correct 4 ms 12956 KB Output is correct
3 Correct 4 ms 13148 KB Output is correct
4 Correct 3 ms 12920 KB Output is correct
5 Correct 3 ms 12892 KB Output is correct
6 Correct 4 ms 12888 KB Output is correct
7 Correct 3 ms 12888 KB Output is correct
8 Correct 4 ms 12892 KB Output is correct
9 Correct 3 ms 12892 KB Output is correct
10 Correct 3 ms 12892 KB Output is correct
11 Correct 3 ms 12892 KB Output is correct
12 Correct 3 ms 12888 KB Output is correct
13 Correct 3 ms 12888 KB Output is correct
14 Correct 4 ms 12892 KB Output is correct
15 Correct 3 ms 12892 KB Output is correct
16 Correct 3 ms 12892 KB Output is correct
17 Correct 3 ms 12892 KB Output is correct
18 Correct 3 ms 12892 KB Output is correct
19 Correct 3 ms 12892 KB Output is correct
20 Correct 3 ms 12888 KB Output is correct
21 Correct 4 ms 12892 KB Output is correct
22 Correct 4 ms 12892 KB Output is correct
23 Correct 3 ms 12892 KB Output is correct
24 Correct 3 ms 12892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12892 KB Output is correct
2 Correct 4 ms 12956 KB Output is correct
3 Correct 4 ms 13148 KB Output is correct
4 Correct 3 ms 12920 KB Output is correct
5 Correct 3 ms 12892 KB Output is correct
6 Correct 4 ms 12888 KB Output is correct
7 Correct 3 ms 12888 KB Output is correct
8 Correct 4 ms 12892 KB Output is correct
9 Correct 3 ms 12892 KB Output is correct
10 Correct 3 ms 12892 KB Output is correct
11 Correct 3 ms 12892 KB Output is correct
12 Correct 3 ms 12888 KB Output is correct
13 Correct 3 ms 12888 KB Output is correct
14 Correct 4 ms 12892 KB Output is correct
15 Correct 3 ms 12892 KB Output is correct
16 Correct 3 ms 12892 KB Output is correct
17 Correct 3 ms 12892 KB Output is correct
18 Correct 3 ms 12892 KB Output is correct
19 Correct 3 ms 12892 KB Output is correct
20 Correct 3 ms 12888 KB Output is correct
21 Correct 4 ms 12892 KB Output is correct
22 Correct 4 ms 12892 KB Output is correct
23 Correct 3 ms 12892 KB Output is correct
24 Correct 3 ms 12892 KB Output is correct
25 Correct 4 ms 13148 KB Output is correct
26 Correct 4 ms 13144 KB Output is correct
27 Correct 4 ms 13288 KB Output is correct
28 Correct 4 ms 13148 KB Output is correct
29 Correct 4 ms 13144 KB Output is correct
30 Correct 5 ms 13144 KB Output is correct
31 Correct 4 ms 13144 KB Output is correct
32 Correct 4 ms 13136 KB Output is correct
33 Correct 4 ms 13148 KB Output is correct
34 Correct 4 ms 13144 KB Output is correct
35 Correct 5 ms 13148 KB Output is correct
36 Correct 6 ms 13148 KB Output is correct
37 Correct 5 ms 13340 KB Output is correct
38 Correct 4 ms 13216 KB Output is correct
39 Correct 5 ms 13148 KB Output is correct
40 Correct 5 ms 13148 KB Output is correct
41 Correct 5 ms 13144 KB Output is correct
42 Correct 4 ms 12964 KB Output is correct
43 Correct 4 ms 13152 KB Output is correct
44 Correct 4 ms 13144 KB Output is correct
45 Correct 4 ms 12904 KB Output is correct
46 Correct 4 ms 12896 KB Output is correct
47 Correct 5 ms 13152 KB Output is correct
48 Correct 4 ms 13148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12892 KB Output is correct
2 Correct 4 ms 12956 KB Output is correct
3 Correct 4 ms 13148 KB Output is correct
4 Correct 3 ms 12920 KB Output is correct
5 Correct 3 ms 12892 KB Output is correct
6 Correct 4 ms 12888 KB Output is correct
7 Correct 3 ms 12888 KB Output is correct
8 Correct 4 ms 12892 KB Output is correct
9 Correct 3 ms 12892 KB Output is correct
10 Correct 3 ms 12892 KB Output is correct
11 Correct 3 ms 12892 KB Output is correct
12 Correct 3 ms 12888 KB Output is correct
13 Correct 3 ms 12888 KB Output is correct
14 Correct 4 ms 12892 KB Output is correct
15 Correct 3 ms 12892 KB Output is correct
16 Correct 3 ms 12892 KB Output is correct
17 Correct 3 ms 12892 KB Output is correct
18 Correct 3 ms 12892 KB Output is correct
19 Correct 3 ms 12892 KB Output is correct
20 Correct 3 ms 12888 KB Output is correct
21 Correct 4 ms 12892 KB Output is correct
22 Correct 4 ms 12892 KB Output is correct
23 Correct 3 ms 12892 KB Output is correct
24 Correct 3 ms 12892 KB Output is correct
25 Correct 4 ms 13148 KB Output is correct
26 Correct 4 ms 13144 KB Output is correct
27 Correct 4 ms 13288 KB Output is correct
28 Correct 4 ms 13148 KB Output is correct
29 Correct 4 ms 13144 KB Output is correct
30 Correct 5 ms 13144 KB Output is correct
31 Correct 4 ms 13144 KB Output is correct
32 Correct 4 ms 13136 KB Output is correct
33 Correct 4 ms 13148 KB Output is correct
34 Correct 4 ms 13144 KB Output is correct
35 Correct 5 ms 13148 KB Output is correct
36 Correct 6 ms 13148 KB Output is correct
37 Correct 5 ms 13340 KB Output is correct
38 Correct 4 ms 13216 KB Output is correct
39 Correct 5 ms 13148 KB Output is correct
40 Correct 5 ms 13148 KB Output is correct
41 Correct 5 ms 13144 KB Output is correct
42 Correct 4 ms 12964 KB Output is correct
43 Correct 4 ms 13152 KB Output is correct
44 Correct 4 ms 13144 KB Output is correct
45 Correct 4 ms 12904 KB Output is correct
46 Correct 4 ms 12896 KB Output is correct
47 Correct 5 ms 13152 KB Output is correct
48 Correct 4 ms 13148 KB Output is correct
49 Correct 382 ms 78672 KB Output is correct
50 Correct 343 ms 80752 KB Output is correct
51 Correct 383 ms 82728 KB Output is correct
52 Correct 345 ms 75952 KB Output is correct
53 Correct 298 ms 79436 KB Output is correct
54 Correct 275 ms 81652 KB Output is correct
55 Correct 259 ms 76488 KB Output is correct
56 Correct 304 ms 78672 KB Output is correct
57 Correct 333 ms 83792 KB Output is correct
58 Correct 372 ms 81524 KB Output is correct
59 Correct 282 ms 81484 KB Output is correct
60 Correct 300 ms 80328 KB Output is correct
61 Correct 552 ms 98108 KB Output is correct
62 Correct 544 ms 92340 KB Output is correct
63 Correct 527 ms 75860 KB Output is correct
64 Correct 521 ms 71940 KB Output is correct
65 Correct 489 ms 69656 KB Output is correct
66 Correct 452 ms 68084 KB Output is correct
67 Correct 465 ms 67364 KB Output is correct
68 Correct 467 ms 67256 KB Output is correct
69 Correct 504 ms 66712 KB Output is correct
70 Correct 485 ms 66644 KB Output is correct
71 Correct 481 ms 66696 KB Output is correct
72 Correct 453 ms 66748 KB Output is correct
73 Correct 495 ms 67040 KB Output is correct
74 Correct 475 ms 67064 KB Output is correct
75 Correct 464 ms 66984 KB Output is correct
76 Correct 519 ms 67252 KB Output is correct
77 Correct 428 ms 67832 KB Output is correct
78 Correct 206 ms 68796 KB Output is correct