Submission #171338

# Submission time Handle Problem Language Result Execution time Memory
171338 2019-12-28T11:12:34 Z GioChkhaidze Hard route (IZhO17_road) C++14
0 / 100
16 ms 12152 KB
#include <bits/stdc++.h>
#define ll long long
#define F first
#define S second
using namespace std;
const int N=5e5+5;
int n;
vector < int > 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 < int , int > M1,M2;
	M1.F=M1.S=M2.F=M2.S=-1; 
	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) {
		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

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: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 time Memory Grader output
1 Correct 10 ms 12152 KB Output is correct
2 Correct 16 ms 12152 KB Output is correct
3 Correct 16 ms 12024 KB Output is correct
4 Incorrect 16 ms 12148 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 10 ms 12152 KB Output is correct
2 Correct 16 ms 12152 KB Output is correct
3 Correct 16 ms 12024 KB Output is correct
4 Incorrect 16 ms 12148 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 10 ms 12152 KB Output is correct
2 Correct 16 ms 12152 KB Output is correct
3 Correct 16 ms 12024 KB Output is correct
4 Incorrect 16 ms 12148 KB Output isn't correct
5 Halted 0 ms 0 KB -