Submission #441172

# Submission time Handle Problem Language Result Execution time Memory
441172 2021-07-04T13:17:04 Z leaked Hard route (IZhO17_road) C++14
100 / 100
1106 ms 163232 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC opimize(-O3)
//#pragma GCC opimize(Ofast)
//#pragma GCC opimize(unroll-loops)
//#pragma GCC target("avx,avx2,popcnt,sse,sse2,sse3,sse4,abm,tune=native")
#define m_p make_pair
#define vec vector
#define all(x) x.begin(),x.end()
#define pb push_back
#define sz(x) (int)x.size()
#define pw(x) (1LL<<x)
#define f first
#define s second
#define ar array
using namespace std;
using namespace __gnu_pbds;
typedef long double ld;
typedef long long ll;
typedef pair<int,int> pii;
//#define
const int M=1e9+7;
const int N=5e5+4;
const int inf=N;
struct fck{
    int val=-N;
    ll cl=0;
    fck(){
        val=-N;cl=0;
    }
};
void mg(fck &a,fck b){
    if(a.val<b.val) a=b;
    else if(a.val==b.val){
        a.cl+=b.cl;
    }
}
fck dpd[N],dpu[N];
vec<fck>go[N];
vec<int>g[N];
ll ans1=0;
ll ans2=0;
/// a<=b<=c c*(a+b)
void dfs1(int v,int p){
    dpd[v].val=0,dpd[v].cl=1;
    go[v].resize(sz(g[v]));
    for(int i=0;i<sz(g[v]);i++){
        int z=g[v][i];
        if(z==p) continue;
        dfs1(z,v);
        auto lel=dpd[z];
        lel.val++;
        go[v][i]=lel;
        mg(dpd[v],lel);
    }
}
void dfs2(int v,int p,fck from){
    vec<int>sons;
    for(int i=0;i<sz(g[v]);i++){
        int z=g[v][i];
        if(z==p){
            go[v][i]=from;
        }
        else{
            sons.pb(z);
        }
    }
    int n=sz(sons);
    vec<fck>pref(n);
    vec<fck>suf(n);
    {
        fck cr;
        for(int i=0;i<n;i++){
            pref[i]=cr;
            fck lel=dpd[sons[i]];lel.val++;
            mg(cr,lel);
        }
    }
    {
        fck cr;
        for(int i=n-1;i>=0;i--){
            suf[i]=cr;
            fck lel=dpd[sons[i]];lel.val++;
            mg(cr,lel);
        }
    }
    for(int i=0;i<sz(sons);i++){
        int u=sons[i];
        fck lel=from;
        mg(lel,pref[i]);mg(lel,suf[i]);
        lel.val++;
        dfs2(u,v,lel);
    }
}
void ins(vec<ll> &vl,ll x){
    for(int i=2;i>=0;i--){
        if(vl[i]==x) break;
        if(vl[i]<x) swap(vl[i],x);
    }
}
void dfs3(int v,int p){
    for(auto &z : g[v]){
        if(z==p) continue;
        dfs3(z,v);
    }
    if(sz(g[v])<=2) return;
    vec<vec<int>>prek(3,vec<int>());
    vec<ll>value(3,-2);
    for(int i=0;i<sz(g[v]);i++){
        ll x=go[v][i].val;
        ins(value,x);
    }
     for(int i=0;i<sz(g[v]);i++){
        for(int j=0;j<3;j++){
            if(value[j]==go[v][i].val){
                prek[j].pb(go[v][i].cl);
            }
        }
    }
    vec<ll>dp(4,0);
    ll x=-1;
    vec<int>to;
    if(sz(prek[2])==1){
        if(sz(prek[1])>1){
            dp[1]=1;
            x=value[2]*(value[1]+value[1]);
            to=prek[1];
        }
        else if(sz(prek[1])==1 && sz(prek[0])){
            dp[2]=prek[1][0];
            to=prek[0];
            x=value[2]*(value[1]+value[0]);
        }
    }
    else if(sz(prek[2])==2){
        dp[2]=prek[2][0]+prek[2][1];
        to=prek[1];
        if(sz(to))x=1ll*value[2]*(value[2]+value[1]);
    }
    else if(sz(prek[2])>2){
        dp[1]=1;
        to=prek[2];
        if(sz(to))x=1ll*value[2]*(value[2]+value[2]);
    }
    for(auto &z : to){
        dp[3]+=1ll*dp[2]*z;
        dp[2]+=1ll*dp[1]*z;
    }
    if(x>ans1) ans1=x,ans2=dp[3];
    else if(x==ans1) ans2+=dp[3];
//    cerr<<v+1<<' '<<x<<' '<<ans2<<endl;
}
signed main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//    ifstream cin("road.in");
//    ofstream cout("road.out");
    int n;
    cin>>n;
    for(int i=1;i<n;i++){
        int v,u;
        cin>>v>>u;--v;--u;
        g[v].pb(u);g[u].pb(v);
    }
    int root=-1;
    for(int i=0;i<n;i++){
        if(sz(g[i])>2) root=i;
    }
    if(root==-1){
        cout<<0<<' '<<1;
        ///just line;
        return 0;
    }
    dfs1(root,root);
    fck blya;
    assert(blya.val==-inf);
    dfs2(root,root,blya);
    dfs3(root,root);
    cout<<ans1<<' '<<ans2;
    return 0;
}
/*
7
1 2
1 3
2 4
2 5
3 6
3 7
4
1 2
2 3
2 4

5
1 2
2 3
3 4
4 5

*/
# Verdict Execution time Memory Grader output
1 Correct 22 ms 39416 KB Output is correct
2 Correct 21 ms 39448 KB Output is correct
3 Correct 20 ms 39372 KB Output is correct
4 Correct 20 ms 39372 KB Output is correct
5 Correct 20 ms 39452 KB Output is correct
6 Correct 21 ms 39372 KB Output is correct
7 Correct 21 ms 39392 KB Output is correct
8 Correct 22 ms 39448 KB Output is correct
9 Correct 21 ms 39372 KB Output is correct
10 Correct 22 ms 39352 KB Output is correct
11 Correct 22 ms 39372 KB Output is correct
12 Correct 22 ms 39412 KB Output is correct
13 Correct 21 ms 39436 KB Output is correct
14 Correct 21 ms 39456 KB Output is correct
15 Correct 22 ms 39448 KB Output is correct
16 Correct 22 ms 39456 KB Output is correct
17 Correct 22 ms 39372 KB Output is correct
18 Correct 22 ms 39408 KB Output is correct
19 Correct 22 ms 39448 KB Output is correct
20 Correct 23 ms 39372 KB Output is correct
21 Correct 21 ms 39468 KB Output is correct
22 Correct 23 ms 39372 KB Output is correct
23 Correct 22 ms 39384 KB Output is correct
24 Correct 22 ms 39448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 39416 KB Output is correct
2 Correct 21 ms 39448 KB Output is correct
3 Correct 20 ms 39372 KB Output is correct
4 Correct 20 ms 39372 KB Output is correct
5 Correct 20 ms 39452 KB Output is correct
6 Correct 21 ms 39372 KB Output is correct
7 Correct 21 ms 39392 KB Output is correct
8 Correct 22 ms 39448 KB Output is correct
9 Correct 21 ms 39372 KB Output is correct
10 Correct 22 ms 39352 KB Output is correct
11 Correct 22 ms 39372 KB Output is correct
12 Correct 22 ms 39412 KB Output is correct
13 Correct 21 ms 39436 KB Output is correct
14 Correct 21 ms 39456 KB Output is correct
15 Correct 22 ms 39448 KB Output is correct
16 Correct 22 ms 39456 KB Output is correct
17 Correct 22 ms 39372 KB Output is correct
18 Correct 22 ms 39408 KB Output is correct
19 Correct 22 ms 39448 KB Output is correct
20 Correct 23 ms 39372 KB Output is correct
21 Correct 21 ms 39468 KB Output is correct
22 Correct 23 ms 39372 KB Output is correct
23 Correct 22 ms 39384 KB Output is correct
24 Correct 22 ms 39448 KB Output is correct
25 Correct 29 ms 40472 KB Output is correct
26 Correct 26 ms 40544 KB Output is correct
27 Correct 25 ms 40504 KB Output is correct
28 Correct 27 ms 40524 KB Output is correct
29 Correct 27 ms 40336 KB Output is correct
30 Correct 26 ms 40308 KB Output is correct
31 Correct 27 ms 40480 KB Output is correct
32 Correct 26 ms 40408 KB Output is correct
33 Correct 25 ms 40592 KB Output is correct
34 Correct 26 ms 40540 KB Output is correct
35 Correct 25 ms 40520 KB Output is correct
36 Correct 25 ms 40516 KB Output is correct
37 Correct 23 ms 39628 KB Output is correct
38 Correct 24 ms 39664 KB Output is correct
39 Correct 25 ms 40172 KB Output is correct
40 Correct 25 ms 40012 KB Output is correct
41 Correct 24 ms 39948 KB Output is correct
42 Correct 26 ms 39796 KB Output is correct
43 Correct 25 ms 39900 KB Output is correct
44 Correct 25 ms 39884 KB Output is correct
45 Correct 24 ms 39780 KB Output is correct
46 Correct 27 ms 39828 KB Output is correct
47 Correct 25 ms 39884 KB Output is correct
48 Correct 24 ms 40104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 39416 KB Output is correct
2 Correct 21 ms 39448 KB Output is correct
3 Correct 20 ms 39372 KB Output is correct
4 Correct 20 ms 39372 KB Output is correct
5 Correct 20 ms 39452 KB Output is correct
6 Correct 21 ms 39372 KB Output is correct
7 Correct 21 ms 39392 KB Output is correct
8 Correct 22 ms 39448 KB Output is correct
9 Correct 21 ms 39372 KB Output is correct
10 Correct 22 ms 39352 KB Output is correct
11 Correct 22 ms 39372 KB Output is correct
12 Correct 22 ms 39412 KB Output is correct
13 Correct 21 ms 39436 KB Output is correct
14 Correct 21 ms 39456 KB Output is correct
15 Correct 22 ms 39448 KB Output is correct
16 Correct 22 ms 39456 KB Output is correct
17 Correct 22 ms 39372 KB Output is correct
18 Correct 22 ms 39408 KB Output is correct
19 Correct 22 ms 39448 KB Output is correct
20 Correct 23 ms 39372 KB Output is correct
21 Correct 21 ms 39468 KB Output is correct
22 Correct 23 ms 39372 KB Output is correct
23 Correct 22 ms 39384 KB Output is correct
24 Correct 22 ms 39448 KB Output is correct
25 Correct 29 ms 40472 KB Output is correct
26 Correct 26 ms 40544 KB Output is correct
27 Correct 25 ms 40504 KB Output is correct
28 Correct 27 ms 40524 KB Output is correct
29 Correct 27 ms 40336 KB Output is correct
30 Correct 26 ms 40308 KB Output is correct
31 Correct 27 ms 40480 KB Output is correct
32 Correct 26 ms 40408 KB Output is correct
33 Correct 25 ms 40592 KB Output is correct
34 Correct 26 ms 40540 KB Output is correct
35 Correct 25 ms 40520 KB Output is correct
36 Correct 25 ms 40516 KB Output is correct
37 Correct 23 ms 39628 KB Output is correct
38 Correct 24 ms 39664 KB Output is correct
39 Correct 25 ms 40172 KB Output is correct
40 Correct 25 ms 40012 KB Output is correct
41 Correct 24 ms 39948 KB Output is correct
42 Correct 26 ms 39796 KB Output is correct
43 Correct 25 ms 39900 KB Output is correct
44 Correct 25 ms 39884 KB Output is correct
45 Correct 24 ms 39780 KB Output is correct
46 Correct 27 ms 39828 KB Output is correct
47 Correct 25 ms 39884 KB Output is correct
48 Correct 24 ms 40104 KB Output is correct
49 Correct 895 ms 156432 KB Output is correct
50 Correct 927 ms 156432 KB Output is correct
51 Correct 871 ms 156356 KB Output is correct
52 Correct 909 ms 156588 KB Output is correct
53 Correct 833 ms 136132 KB Output is correct
54 Correct 835 ms 163232 KB Output is correct
55 Correct 827 ms 163220 KB Output is correct
56 Correct 848 ms 126944 KB Output is correct
57 Correct 799 ms 156144 KB Output is correct
58 Correct 802 ms 156372 KB Output is correct
59 Correct 822 ms 156156 KB Output is correct
60 Correct 812 ms 156256 KB Output is correct
61 Correct 375 ms 62124 KB Output is correct
62 Correct 379 ms 62256 KB Output is correct
63 Correct 1100 ms 113968 KB Output is correct
64 Correct 1106 ms 99700 KB Output is correct
65 Correct 1092 ms 92612 KB Output is correct
66 Correct 1044 ms 88556 KB Output is correct
67 Correct 1049 ms 87132 KB Output is correct
68 Correct 1016 ms 86404 KB Output is correct
69 Correct 994 ms 85948 KB Output is correct
70 Correct 972 ms 85784 KB Output is correct
71 Correct 973 ms 85708 KB Output is correct
72 Correct 967 ms 85832 KB Output is correct
73 Correct 998 ms 86192 KB Output is correct
74 Correct 984 ms 86572 KB Output is correct
75 Correct 960 ms 87816 KB Output is correct
76 Correct 898 ms 89684 KB Output is correct
77 Correct 796 ms 95416 KB Output is correct
78 Correct 559 ms 105480 KB Output is correct