Submission #393068

# Submission time Handle Problem Language Result Execution time Memory
393068 2021-04-22T16:13:54 Z patrikpavic2 Hard route (IZhO17_road) C++17
0 / 100
24 ms 39496 KB
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>

#define X first
#define Y second
#define PB push_back

using namespace std;

typedef long long ll;
typedef pair < int , int > pii;
typedef pair < ll , ll > pll;

const int N = 1e6 + 500;

int n;
vector < pii > v[N];
pll dis[N];
ll cnt[N][3];

inline pll add(const pll &A, const pll &B){
	return {(A.X > B.X ? A.X : B.X), A.Y * (A.X >= B.X) + B.Y * (B.X >= A.X)};
}

pll calc_dis(int x, int lst, int ind){
	if(dis[2 * ind + (x < lst)].X != -1) return dis[2 * ind + (x < lst)];
	pll ret = {0, 1};
	for(pii y : v[x])
		if(y.X != lst)
			ret = add(ret, calc_dis(y.X, x, y.Y));
	ret.X++;
	return dis[2 * ind + (x < lst)] = ret;
}

ll ch(int n, int k){
	int ret = 1;
	for(int i = 0;i < k;i++)
		ret *= (n - i), ret /= i + 1;
	return ret;
}

int main(){
	for(int i = 0;i < N;i++) dis[i].X = -1;
	scanf("%d", &n);
	for(int i = 1;i < n;i++){
		int x, y; scanf("%d%d", &x, &y);
		v[x].PB({y, i}), v[y].PB({x, i});
	}
	pll sol = {0, 0};
	for(int i = 1;i <= n;i++){
		vector < int > sus;
		for(pii nxt : v[i]){
			pll nw = calc_dis(nxt.X, i, nxt.Y);
			sus.PB(nw.X);
			cnt[nw.X][2] += nw.Y * cnt[nw.X][1];
			cnt[nw.X][1] += nw.Y;
		}
		for(int j = 0;j < 3;j++)
			for(int i = 1;i < (int)sus.size();i++)
				if(sus[i - 1] < sus[i]) swap(sus[i], sus[i - 1]);
		if((int)sus.size() > 2){
			if(sus[0] > sus[1] && sus[1] > sus[2])
				sol = add(sol, {(ll)(sus[1] + sus[2]) * sus[0], cnt[sus[1]][1] * cnt[sus[2]][1]});
			else if(sus[0] == sus[1] && sus[1] == sus[2])
				sol = add(sol, {(ll)(sus[1] + sus[2]) * sus[0], cnt[sus[0]][2]});
			else if(sus[0] > sus[1])
				sol = add(sol, {(ll)(sus[1] + sus[2]) * sus[0], cnt[sus[1]][2]});				
			else
				sol = add(sol, {(ll)(sus[1] + sus[2]) * sus[0], cnt[sus[1]][2] & cnt[sus[2]][1]});						
		}
		for(int x : sus){
			cnt[x][1] = 0, cnt[x][2] = 0;
		}
	}
	if(!sol.X) sol.Y++;
	printf("%lld %lld\n", sol.X, sol.Y);
	return 0;
}






Compilation message

road.cpp: In function 'int main()':
road.cpp:46:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   46 |  scanf("%d", &n);
      |  ~~~~~^~~~~~~~~~
road.cpp:48:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   48 |   int x, y; scanf("%d%d", &x, &y);
      |             ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 20 ms 39368 KB Output is correct
2 Correct 21 ms 39376 KB Output is correct
3 Correct 21 ms 39440 KB Output is correct
4 Correct 24 ms 39372 KB Output is correct
5 Correct 21 ms 39344 KB Output is correct
6 Correct 21 ms 39432 KB Output is correct
7 Correct 22 ms 39496 KB Output is correct
8 Correct 22 ms 39460 KB Output is correct
9 Incorrect 21 ms 39352 KB Output isn't correct
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 20 ms 39368 KB Output is correct
2 Correct 21 ms 39376 KB Output is correct
3 Correct 21 ms 39440 KB Output is correct
4 Correct 24 ms 39372 KB Output is correct
5 Correct 21 ms 39344 KB Output is correct
6 Correct 21 ms 39432 KB Output is correct
7 Correct 22 ms 39496 KB Output is correct
8 Correct 22 ms 39460 KB Output is correct
9 Incorrect 21 ms 39352 KB Output isn't correct
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 20 ms 39368 KB Output is correct
2 Correct 21 ms 39376 KB Output is correct
3 Correct 21 ms 39440 KB Output is correct
4 Correct 24 ms 39372 KB Output is correct
5 Correct 21 ms 39344 KB Output is correct
6 Correct 21 ms 39432 KB Output is correct
7 Correct 22 ms 39496 KB Output is correct
8 Correct 22 ms 39460 KB Output is correct
9 Incorrect 21 ms 39352 KB Output isn't correct
10 Halted 0 ms 0 KB -