Submission #171315

# Submission time Handle Problem Language Result Execution time Memory
171315 2019-12-28T10:45:29 Z GioChkhaidze Hard route (IZhO17_road) C++14
0 / 100
14 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=D[x].S-M1.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++) {
		int to=v[x][i];
		s.push_back(D[to]);
	}
	
	sort(s.begin(),s.end());
	reverse(s.begin(),s.end());
	
	if (s.size()>=3) {
		for (int i=3; i<s.size(); i++) 
			if (s[2].F==s[i].F) s[2].S+=s[i].S;
		
		ll g=s[0].F*(s[1].F+s[2].F),tot=0;
			
		if (s[0].F==s[1].F && s[1].F==s[2].F) tot=s[1].S*s[2].S+s[0].S*s[1].S+s[0].S*s[2].S;
			else
		if (s[0].F==s[1].F) tot=s[0].S*s[2].S+s[1].S*s[2].S;
			else
		if (s[1].F==s[2].F) tot=s[1].S*s[2].S;
		
		if (ans.F<g) ans.F=g,ans.S=tot;
				else 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:18: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:36:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i=0; i<v[x].size(); i++) {
                ~^~~~~~~~~~~~
road.cpp:46: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:65:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i=0; i<v[x].size(); i++) {
                ~^~~~~~~~~~~~
road.cpp:74:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i=3; i<s.size(); i++) 
                 ~^~~~~~~~~
road.cpp:89: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:93:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main () { 
       ^
road.cpp: In function 'int main()':
road.cpp:94:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&n);
  ~~~~~^~~~~~~~~
road.cpp:98: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 13 ms 12152 KB Output is correct
2 Correct 12 ms 12152 KB Output is correct
3 Correct 13 ms 12024 KB Output is correct
4 Incorrect 14 ms 12024 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 13 ms 12152 KB Output is correct
2 Correct 12 ms 12152 KB Output is correct
3 Correct 13 ms 12024 KB Output is correct
4 Incorrect 14 ms 12024 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 13 ms 12152 KB Output is correct
2 Correct 12 ms 12152 KB Output is correct
3 Correct 13 ms 12024 KB Output is correct
4 Incorrect 14 ms 12024 KB Output isn't correct
5 Halted 0 ms 0 KB -