Submission #173329

#TimeUsernameProblemLanguageResultExecution timeMemory
173329achibasadzishviliHard route (IZhO17_road)C++14
0 / 100
13 ms12152 KiB
#include<bits/stdc++.h> #define ll long long #define f first #define s second #define pb push_back #define N 500005 using namespace std; ll ans = 0,ansraod = 0; pair<pair<ll,ll> ,pair<ll,ll> >mx[N]; vector<ll>v[N]; ll n,m,star; void go(ll x,ll par){ for(int i=0; i<v[x].size(); i++) if(v[x][i] != par) go(v[x][i] , x); ll cur = -1 , raod = 1 , ind = 0 , wn = -1 , sh = 0; for(int i=0; i<v[x].size(); i++) if(v[x][i] != par){ ll to = v[x][i]; if(mx[to].f.f < cur && mx[to].f.f > wn){ wn = mx[to].f.f; } if(mx[to].f.f > cur){ raod = 0; sh = 0; wn = cur; cur = mx[to].f.f; } if(mx[to].f.f == cur){ ind = to; sh++; raod += mx[to].f.s; } } /* mx[x].f.f aris maximaluri sigrme xidan mx[x].f.s ki aris aseti wveroebis raodenoba mx[x].s.f aris danartis max sigrdze mx[x].s.s maxiani danarti ro moipovebodes egetebis raodenoba */ if(ind == 0){ mx[x].f.f = 1; mx[x].f.s = 1; mx[x].s.f = 0; mx[x].s.s = 1; return; } cur += 1; mx[x].f.f = cur; mx[x].f.s = raod; if(sh > 1){ mx[x].s.f = cur - 1; mx[x].s.s = sh; return; } mx[x].s.f = wn + 1; mx[x].s.s = 1; } void calc(ll x,ll y,ll gare,ll pir,ll meo){ //cout << x << "<-->" << y << " " << gare << " " << pir << " " << meo << endl; ll namr = max(pir , meo); if(gare >= namr){ namr = gare; if(namr * (mx[x].f.f + mx[y].f.f) > ans){ ans = namr * (mx[x].f.f + mx[y].f.f); ansraod = 0; } if(namr * (mx[x].f.f + mx[y].f.f) == ans){ ansraod += mx[x].f.s * mx[y].f.s; } return; } if(pir == meo){ if(namr * (mx[x].f.f + mx[y].f.f) > ans){ ans = namr * (mx[x].f.f + mx[y].f.f); ansraod = 0; } if(namr * (mx[x].f.f + mx[y].f.f) == ans){ ansraod += mx[x].s.s * mx[y].f.s + mx[y].s.s * mx[x].f.s; ansraod -= mx[x].s.s * mx[y].s.s; } return; } if(mx[x].s.f == namr){ if(namr * (mx[x].f.f + mx[y].f.f) > ans){ ans = namr * (mx[x].f.f + mx[y].f.f); ansraod = 0; } if(namr * (mx[x].f.f + mx[y].f.f) == ans){ ansraod += mx[x].s.s * mx[y].f.s; } return; } if(mx[y].s.f == namr){ if(namr * (mx[x].f.f + mx[y].f.f) > ans){ ans = namr * (mx[x].f.f + mx[y].f.f); ansraod = 0; } if(namr * (mx[x].f.f + mx[y].f.f) == ans){ ansraod += mx[y].s.s * mx[x].f.s; } return; } } void solve(ll x,ll par,ll zed){ //cout << x << " " << par << " " << zed << endl; ll mx1 = -1,mx2 = -1,ind = 0; for(int i=0; i<v[x].size(); i++) if(v[x][i] != par){ int to = v[x][i]; if(mx[to].f.f >= mx1){ ind = to; mx2 = mx1; mx1 = mx[to].f.f; } else if(mx[to].f.f > mx2)mx2 = mx[to].f.f; } mx1++; mx2++; for(int i=0; i<v[x].size(); i++) if(v[x][i] != par){ int to = v[x][i]; ll nex = 0; if(to == ind) nex = max(zed + 1 , mx2); else nex = max(zed + 1 , mx1); solve(to , x , nex); } if((int)v[x].size() - (x != star) < 2){ return; } ll gan = 0 , sum = 0 , minus = 0; for(int i=0; i<v[x].size(); i++) if(v[x][i] != par){ int to = v[x][i]; if(mx[to].f.f == mx[x].f.f - 1){ gan++; sum += mx[to].f.s; minus += (mx[to].f.s - 1) * mx[to].f.s / 2 + mx[to].f.s; } } if(gan > 2){ ll namr = max(mx[x].f.f - 1 , zed); //cout << namr << " " << sum << "<<<<<" << minus << endl; if(namr * 2LL * (mx[x].f.f - 1) > ans){ ans = namr * 2LL * (mx[x].f.f - 1); ansraod = 0; } if(namr * 2LL * (mx[x].f.f - 1) == ans){ ansraod += (sum * sum - minus) / 2; } return; } if(gan == 2){ ll ind1 = 0 , ind2 = 0 , mxx = -1; for(int i=0; i<v[x].size(); i++) if(v[x][i] != par){ int to = v[x][i]; if(mx[to].f.f == mx[x].f.f - 1){ if(!ind1){ ind1 = to; continue; } ind2 = to; continue; } mxx = max(mxx , mx[to].f.f); } ll namr = max(mx[ind1].s.f , mx[ind2].s.f); ll gar = max(mxx , zed); calc(ind1 , ind2 , gar , mx[ind1].s.f , mx[ind2].s.f); ll ne = -1 , in = 0 , ram = 0; for(int i=0; i<v[x].size(); i++){ if(v[x][i] != par){ int to = v[x][i]; if(to == ind1 || to == ind2)continue; if(mx[to].f.f > ne){ ne = mx[to].f.f; in = to; ram = 0; } if(ne == mx[to].f.f){ ram++; } } } if(ne == -1)return; if(max(mx[ind1].f.f , zed) * (mx[ind1].f.f + ne) > ans){ ans = max(mx[ind1].f.f , zed) * (mx[ind1].f.f + ne); ansraod = 0; } if(max(mx[ind1].f.f , zed) * (mx[ind1].f.f + ne) == ans){ ansraod += 2LL * ram; } return; } ll f = 0; for(int i=0; i<v[x].size(); i++){ if(v[x][i] != par){ int to = v[x][i]; if(mx[to].f.f == mx[x].f.f - 1){ f = to; break; } } } ll m1 = -1 , m2 = -1, mi = 0; for(int i=0; i<v[x].size(); i++){ if(v[x][i] != par){ int to = v[x][i]; if(mx[to].f.f != mx[x].f.f - 1){ if(mx[to].f.f >= m1){ m2 = m1; mi = to; m1 = mx[to].f.f; } else if(mx[to].f.f > m2)m2 = mx[to].f.f; } } } for(int i=0; i<v[x].size(); i++){ if(v[x][i] != par){ int to = v[x][i]; if(mx[to].f.f == m1){ ll gar = max(zed , m2); calc(f , to , gar , mx[f].s.f , mx[to].s.f); } } } ll ne = -1 , ni = 0 , inn = 0; for(int i=0; i<v[x].size(); i++){ if(v[x][i] != par){ int to = v[x][i]; if(to == f)continue; if(mx[to].f.f > ne){ ne = mx[to].f.f; inn = to; ni = 0; } if(mx[to].f.f == ne){ ni++; } } } if(ni <= 0)return; if(ni > 1){ if(max(mx[f].f.f , zed) * (ne + ne) > ans){ ans = max(mx[f].f.f , zed) * (ne + ne); ansraod = 0; } if(max(mx[f].f.f , zed) * (ne + ne) == ans){ ansraod += ni * (ni - 1) / 2; } return; } ll po = -1 , pi = 0; for(int i=0; i<v[x].size(); i++){ if(v[x][i] != par){ int to = v[x][i]; if(to == inn || to == f)continue; if(mx[to].f.f > po){ po = mx[to].f.f; pi = 0; } if(mx[to].f.f == po){ pi++; } } } if(po <= 0)return; if(max(mx[f].f.f , zed) * (po + ne) > ans){ ans = max(mx[f].f.f , zed) * (po + ne); ansraod = 0; } if(max(mx[f].f.f , zed) * (po + ne) == ans){ ansraod += po; } } int main(){ ios::sync_with_stdio(false); cin >> n; if(n == 2){ cout << 0 << " " << 1 << '\n'; return 0; } for(int i=1; i<n; i++){ int x,y; cin >> x >> y; v[x].pb(y); v[y].pb(x); } ll kk = 0; for(int i=1; i<=n; i++) if((int)v[i].size() > 1){ star = i; kk++; } //cout << star << endl; go(star , 0); solve(star , 0 , 0); /*if(ans == 0){ cout << 0 << " " << kk * (kk - 1) / 2 << '\n'; return 0; }*/ cout << ans << " " << ansraod << '\n'; return 0; }

Compilation message (stderr)

road.cpp: In function 'void go(long long int, long long int)':
road.cpp:13:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0; i<v[x].size(); i++)
                  ~^~~~~~~~~~~~
road.cpp:17:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0; i<v[x].size(); i++)
                  ~^~~~~~~~~~~~
road.cpp: In function 'void solve(long long int, long long int, long long int)':
road.cpp:108:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0; i<v[x].size(); i++)
                  ~^~~~~~~~~~~~
road.cpp:120:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0; i<v[x].size(); i++)
                  ~^~~~~~~~~~~~
road.cpp:133:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0; i<v[x].size(); i++)
                  ~^~~~~~~~~~~~
road.cpp:156:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i=0; i<v[x].size(); i++)
                      ~^~~~~~~~~~~~
road.cpp:173:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i=0; i<v[x].size(); i++){
                      ~^~~~~~~~~~~~
road.cpp:169:12: warning: unused variable 'namr' [-Wunused-variable]
         ll namr = max(mx[ind1].s.f , mx[ind2].s.f);
            ^~~~
road.cpp:172:22: warning: variable 'in' set but not used [-Wunused-but-set-variable]
         ll ne = -1 , in = 0 , ram = 0;
                      ^~
road.cpp:198:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0; i<v[x].size(); i++){
                  ~^~~~~~~~~~~~
road.cpp:208:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0; i<v[x].size(); i++){
                  ~^~~~~~~~~~~~
road.cpp:221:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0; i<v[x].size(); i++){
                  ~^~~~~~~~~~~~
road.cpp:231:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0; i<v[x].size(); i++){
                  ~^~~~~~~~~~~~
road.cpp:257:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0; i<v[x].size(); i++){
                  ~^~~~~~~~~~~~
road.cpp:207:27: warning: variable 'mi' set but not used [-Wunused-but-set-variable]
     ll m1 = -1 , m2 = -1, mi = 0;
                           ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...