Submission #171333

# Submission time Handle Problem Language Result Execution time Memory
171333 2019-12-28T11:06:27 Z GioChkhaidze Hard route (IZhO17_road) C++14
0 / 100
16 ms 12128 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;
			
		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) {
			for (int i=3; i<s.size(); i++) 
				if (s[2].F==s[i].F) s[2].S+=s[i].S;
			tot=s[0].S*s[2].S+s[1].S*s[2].S;
		}
			else tot=s[1].S*s[2].S;
		
		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:69:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for (int i=0; i<s.size(); i++)
                  ~^~~~~~~~~
road.cpp:74:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for (int i=3; i<s.size(); i++) 
                  ~^~~~~~~~~
road.cpp:85: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:89:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main () { 
       ^
road.cpp: In function 'int main()':
road.cpp:90:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&n);
  ~~~~~^~~~~~~~~
road.cpp:94: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 16 ms 12024 KB Output is correct
2 Correct 15 ms 12128 KB Output is correct
3 Correct 13 ms 12024 KB Output is correct
4 Incorrect 16 ms 12024 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 16 ms 12024 KB Output is correct
2 Correct 15 ms 12128 KB Output is correct
3 Correct 13 ms 12024 KB Output is correct
4 Incorrect 16 ms 12024 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 16 ms 12024 KB Output is correct
2 Correct 15 ms 12128 KB Output is correct
3 Correct 13 ms 12024 KB Output is correct
4 Incorrect 16 ms 12024 KB Output isn't correct
5 Halted 0 ms 0 KB -