Submission #666618

# Submission time Handle Problem Language Result Execution time Memory
666618 2022-11-29T07:23:04 Z Kaztaev_Alisher Hard route (IZhO17_road) C++17
0 / 100
3 ms 2688 KB
//#pragma GCC optomize ("Ofast")
//#pragma GCC optomize ("unroll-loops")
//#pragma GCC target ("avx,avx2,fma")

#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define sz size
#define cl clear
#define ins insert
#define ers erase
#define pii pair < int , int >
#define pll pair< long long  , long long >
#define all(x) x.begin() , x.end()
#define ios ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
#define tostr(x) to_string(x)
#define tonum(s) atoi(s.c_str())
#define seon(x) setprecision(x)
#define bpop(x) __builtin_popcount(x)
#define deb(x) cerr << #x  << " = " << x << endl;

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
const double PI = 3.14159265;

const ll N = 1e5+5;
const ll mod = 1e9+7;
const ll inf = 1e9;
const ll INF = 1e18;

using namespace std;


int mx = 0 , bir = 0 , eki = 0;
ll pr[N] , dp[N];
vector<int> g[N];
bool was[N] , ok = 0;
void dfs(int v , int p , int d = 1){
	pr[v] = p;
	if(d > mx){
		mx = d;
		if(ok == 1) eki = v;
		else bir = v;
	}
	for(int to : g[v]){
		if(to == p) continue;	
		dfs(to , v , d+1);
	}
}
void dfsmx(int v , int p){
	if(was[v] == 0) was[v] = 2 , dp[v] = 0;
	for(int to : g[v]){
		if(to == p) continue;
		if(was[to] == 0){
			dfsmx(to,v);
			dp[v] = max(dp[to]+1 , dp[v]);
		} if(was[to] == 1) dfsmx(to,v);
	}
}
ll ans = 0 , cnt = 0;
void get(int v , int p , int d = 0){
	for(int to : g[v]){
		if(to == p) continue;
		if(was[to] == 1){
			get(to , v , d+1);
			ll res = 0;
			
			if(dp[v] > 0){
				res = (mx-d) * (d+dp[v]);
				if(res > ans){
					ans = res;
					cnt = 1;
				} else if (ans == res) cnt++;
				res = d * (mx-d+dp[v]);
				if(res > ans){
					ans = res;
					cnt = 1;
				} else if (ans == res) cnt++;
				res = mx*dp[v];
				if(res > ans){
					ans = res;
					cnt = 1;
				} else if (ans == res) cnt++;
			} 
		}
	}
}
void solve(){
	int n;
	cin >> n;
	for(int i = 1; i < n; i++) {
		int a , b;
		cin >> a >> b;
		g[a].pb(b);
		g[b].pb(a);
	}
	dfs(1 , 0);
	mx = 0;
	ok = 1;
	dfs(bir , 0);
	int v = eki;
	while(v > 0){
		was[v] = 1;
		v = pr[v];
	}
	mx--;
	dfsmx(bir , 0);
	get(bir , 0);
	if(ans == 0) cnt = 2;
	cout << ans <<" " << cnt/2 <<"\n";
	
}
signed main(){
	file("road");
	ios;
	solve();
	return 0;
}
/*
*/

Compilation message

road.cpp: In function 'int main()':
road.cpp:17:48: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 | #define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
      |                                         ~~~~~~~^~~~~~~~~~~~~~~~~~~~
road.cpp:118:2: note: in expansion of macro 'file'
  118 |  file("road");
      |  ^~~~
road.cpp:17:77: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 | #define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
      |                                                                      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
road.cpp:118:2: note: in expansion of macro 'file'
  118 |  file("road");
      |  ^~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2676 KB Output is correct
3 Correct 3 ms 2684 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2688 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Incorrect 1 ms 2644 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2676 KB Output is correct
3 Correct 3 ms 2684 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2688 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Incorrect 1 ms 2644 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2676 KB Output is correct
3 Correct 3 ms 2684 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2688 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Incorrect 1 ms 2644 KB Output isn't correct
8 Halted 0 ms 0 KB -