Submission #172175

#TimeUsernameProblemLanguageResultExecution timeMemory
172175GioChkhaidzeHard route (IZhO17_road)C++14
100 / 100
1195 ms112860 KiB
#include <bits/stdc++.h> #define ll long long #define F first #define S second using namespace std; const int N=5e5+5; ll n; vector < ll > v[N]; pair < ll , ll > D[N],T[N],ans; void Dfs(int x,int p) { for (int i=0; i<v[x].size(); i++) if (v[x][i]==p) { swap(v[x][i],v[x][v[x].size()-1]); v[x].pop_back(); break; } for (int i=0; i<v[x].size(); i++) { int to=v[x][i]; Dfs(to,x); if (D[to].F>D[x].F) D[x]=D[to]; else if (D[to].F==D[x].F) D[x].S+=D[to].S; } D[x].F++; if (!v[x].size()) D[x].S=1; } void Ufs(int x) { pair < ll , ll > M1,M2; M1.F=M1.S=M2.F=M2.S=0; M1=D[x],M1.F--; for (int i=0; i<v[x].size(); i++) { int to=v[x][i]; if (M1.F==D[to].F) continue; if (M2.F<D[to].F) M2=D[to]; else if (M2.F==D[to].F) M2.S+=D[to].S; } if (x==1) T[x].S=1; for (int i=0; i<v[x].size(); i++) { int to=v[x][i]; if (M1.F==D[to].F && M1.S==D[to].S) T[to]=M2; else if (M1.F==D[to].F && M1.S>D[to].S) T[to].F=M1.F,T[to].S=M1.S-D[to].S; else T[to]=M1; if (T[x].F>T[to].F) T[to]=T[x]; else if (T[x].F==T[to].F) T[to].S+=T[x].S; T[to].F++; Ufs(to); } } void Df(int x) { vector < pair < ll , ll > > s; s.push_back(T[x]); for (int i=0; i<v[x].size(); i++) s.push_back(D[v[x][i]]); sort(s.begin(),s.end()); reverse(s.begin(),s.end()); if (s.size()>=3 && s[2].F>0) { ll g=s[0].F*(s[1].F+s[2].F),tot=0,sum=0,S3=0; for (int i=2; i<s.size(); i++) if (s[2].F==s[i].F) S3+=s[i].S; if (s[0].F==s[1].F && s[1].F==s[2].F) { for (int i=0; i<s.size(); i++) if (s[0].F==s[i].F) tot+=s[i].S*sum,sum+=s[i].S; } else if (s[0].F==s[1].F) { tot=s[0].S*S3+s[1].S*S3; } else if (s[1].F==s[2].F) { for (int i=1; i<s.size(); i++) if (s[1].F==s[i].F) tot+=s[i].S*sum,sum+=s[i].S; } else tot=s[1].S*S3; if (ans.F<g) ans.F=g,ans.S=tot; else if (ans.F==g) ans.S+=tot; } for (int i=0; i<v[x].size(); i++) Df(v[x][i]); } main () { scanf("%d",&n); for (int i=1; i<n; i++) { int a,b; scanf("%d%d",&a,&b); v[a].push_back(b); v[b].push_back(a); } Dfs(1,1); Ufs(1); Df(1); if (!ans.F) ans.S=1; printf("%lld %lld \n",ans.F,ans.S); }

Compilation message (stderr)

road.cpp: In function 'void Dfs(int, int)':
road.cpp:11:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i=0; i<v[x].size(); i++) 
                ~^~~~~~~~~~~~
road.cpp:14:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i=0; i<v[x].size(); i++) {
                ~^~~~~~~~~~~~
road.cpp: In function 'void Ufs(int)':
road.cpp:31:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i=0; i<v[x].size(); i++) {
                ~^~~~~~~~~~~~
road.cpp:41:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i=0; i<v[x].size(); i++) {
                ~^~~~~~~~~~~~
road.cpp: In function 'void Df(int)':
road.cpp:59:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i=0; i<v[x].size(); i++) 
                ~^~~~~~~~~~~~
road.cpp:68:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i=2; i<s.size(); i++) 
                 ~^~~~~~~~~
road.cpp:72:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for (int i=0; i<s.size(); i++)
                  ~^~~~~~~~~
road.cpp:81:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for (int i=1; i<s.size(); i++)
                  ~^~~~~~~~~
road.cpp:91:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i=0; i<v[x].size(); i++)
                ~^~~~~~~~~~~~
road.cpp: At global scope:
road.cpp:95:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main () { 
       ^
road.cpp: In function 'int main()':
road.cpp:96:15: warning: format '%d' expects argument of type 'int*', but argument 2 has type 'long long int*' [-Wformat=]
  scanf("%d",&n);
             ~~^
road.cpp:96:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&n);
  ~~~~~^~~~~~~~~
road.cpp:100:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d",&a,&b);
   ~~~~~^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...