답안 #92858

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
92858 2019-01-05T11:07:44 Z emil_physmath Hard route (IZhO17_road) C++17
52 / 100
2000 ms 59036 KB
#include <iostream>
#include <stdio.h>
#include <vector>
#include <cstring>
using namespace std;
const int MAXN=500005;

vector<int> nei[MAXN];
pair<int, int> maxdist[MAXN];
bool used[MAXN];

void MaxDist(int u);
pair<int, int> DFS(int u, int dep, int dist);
int main()
{
	int n;
	cin>>n;
	for (int i=1; i<n; i++)
	{
		int u, v;
		scanf("%d%d", &u, &v);
		nei[u].push_back(v);
		nei[v].push_back(u);
	}
	pair<int, int> ans(0, 0);
	for (int u=1; u<=n; u++)
		if (nei[u].size()==1)
		{
			memset(used+1, 0, n*sizeof(bool));
			MaxDist(u);
			memset(used+1, 0, n*sizeof(bool));
			pair<int, int> curAns=DFS(u, 0, 0);
			if (curAns.first>ans.first) ans=curAns;
			else if (curAns.first==ans.first) ans.second+=curAns.second;
		}
	cout<<ans.first<<' '<<ans.second/2<<'\n';

	char I;
	cin >> I;
	return 0;
}

pair<int, int> DFS(int u, int dep, int dist)
{
	used[u]=true;
	if (nei[u].size()==1 && used[nei[u][0]])
		return make_pair(dep*dist, 1);
	pair<int, int> ans(0, 0);
	for (int i=0; i<nei[u].size(); i++)
	{
		int to=nei[u][i];
		if (!used[to])
		{
			pair<int, int> curAns;
			if (maxdist[u].first==maxdist[to].first+1)
				curAns=DFS(to, dep+1, max(dist, maxdist[u].second));
			else
				curAns=DFS(to, dep+1, max(dist, maxdist[u].first));

			if (curAns.first>ans.first) ans=curAns;
			else if (curAns.first==ans.first) ans.second+=curAns.second;
		}
	}
	return ans;
}

void MaxDist(int u)
{
	used[u]=true;
	maxdist[u]=make_pair(0, 0);
	if (nei[u].size()==1 && used[nei[u][0]]) return;
	for (int i=0; i<nei[u].size(); i++)
	{
		int to=nei[u][i];
		if (!used[to])
		{
			MaxDist(to);
			int temp=maxdist[to].first+1;
			if (temp>maxdist[u].first)
			{
				swap(maxdist[u].first, maxdist[u].second);
				maxdist[u].first=temp;
			}
			else if (temp>maxdist[u].second)
				maxdist[u].second=temp;
		}
	}
}

Compilation message

road.cpp: In function 'std::pair<int, int> DFS(int, int, int)':
road.cpp:49:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i=0; i<nei[u].size(); i++)
                ~^~~~~~~~~~~~~~
road.cpp: In function 'void MaxDist(int)':
road.cpp:72:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i=0; i<nei[u].size(); i++)
                ~^~~~~~~~~~~~~~
road.cpp: In function 'int main()':
road.cpp:21:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &u, &v);
   ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 12024 KB Output is correct
2 Correct 10 ms 12024 KB Output is correct
3 Correct 13 ms 12024 KB Output is correct
4 Correct 10 ms 12024 KB Output is correct
5 Correct 10 ms 12024 KB Output is correct
6 Correct 10 ms 12024 KB Output is correct
7 Correct 10 ms 12024 KB Output is correct
8 Correct 10 ms 12024 KB Output is correct
9 Correct 10 ms 12024 KB Output is correct
10 Correct 10 ms 12044 KB Output is correct
11 Correct 12 ms 12152 KB Output is correct
12 Correct 10 ms 12152 KB Output is correct
13 Correct 11 ms 12152 KB Output is correct
14 Correct 12 ms 12024 KB Output is correct
15 Correct 10 ms 12024 KB Output is correct
16 Correct 12 ms 12152 KB Output is correct
17 Correct 10 ms 12024 KB Output is correct
18 Correct 12 ms 12024 KB Output is correct
19 Correct 10 ms 12152 KB Output is correct
20 Correct 12 ms 12024 KB Output is correct
21 Correct 12 ms 12024 KB Output is correct
22 Correct 11 ms 12152 KB Output is correct
23 Correct 10 ms 12024 KB Output is correct
24 Correct 10 ms 12152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 12024 KB Output is correct
2 Correct 10 ms 12024 KB Output is correct
3 Correct 13 ms 12024 KB Output is correct
4 Correct 10 ms 12024 KB Output is correct
5 Correct 10 ms 12024 KB Output is correct
6 Correct 10 ms 12024 KB Output is correct
7 Correct 10 ms 12024 KB Output is correct
8 Correct 10 ms 12024 KB Output is correct
9 Correct 10 ms 12024 KB Output is correct
10 Correct 10 ms 12044 KB Output is correct
11 Correct 12 ms 12152 KB Output is correct
12 Correct 10 ms 12152 KB Output is correct
13 Correct 11 ms 12152 KB Output is correct
14 Correct 12 ms 12024 KB Output is correct
15 Correct 10 ms 12024 KB Output is correct
16 Correct 12 ms 12152 KB Output is correct
17 Correct 10 ms 12024 KB Output is correct
18 Correct 12 ms 12024 KB Output is correct
19 Correct 10 ms 12152 KB Output is correct
20 Correct 12 ms 12024 KB Output is correct
21 Correct 12 ms 12024 KB Output is correct
22 Correct 11 ms 12152 KB Output is correct
23 Correct 10 ms 12024 KB Output is correct
24 Correct 10 ms 12152 KB Output is correct
25 Correct 312 ms 12536 KB Output is correct
26 Correct 295 ms 12536 KB Output is correct
27 Correct 299 ms 12664 KB Output is correct
28 Correct 289 ms 12556 KB Output is correct
29 Correct 361 ms 12580 KB Output is correct
30 Correct 352 ms 12536 KB Output is correct
31 Correct 350 ms 12664 KB Output is correct
32 Correct 369 ms 12536 KB Output is correct
33 Correct 17 ms 12536 KB Output is correct
34 Correct 17 ms 12664 KB Output is correct
35 Correct 17 ms 12668 KB Output is correct
36 Correct 16 ms 12664 KB Output is correct
37 Correct 14 ms 12664 KB Output is correct
38 Correct 16 ms 12664 KB Output is correct
39 Correct 16 ms 12408 KB Output is correct
40 Correct 18 ms 12412 KB Output is correct
41 Correct 22 ms 12408 KB Output is correct
42 Correct 33 ms 12408 KB Output is correct
43 Correct 50 ms 12412 KB Output is correct
44 Correct 83 ms 12280 KB Output is correct
45 Correct 159 ms 12280 KB Output is correct
46 Correct 167 ms 12280 KB Output is correct
47 Correct 400 ms 12404 KB Output is correct
48 Correct 419 ms 12536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 12024 KB Output is correct
2 Correct 10 ms 12024 KB Output is correct
3 Correct 13 ms 12024 KB Output is correct
4 Correct 10 ms 12024 KB Output is correct
5 Correct 10 ms 12024 KB Output is correct
6 Correct 10 ms 12024 KB Output is correct
7 Correct 10 ms 12024 KB Output is correct
8 Correct 10 ms 12024 KB Output is correct
9 Correct 10 ms 12024 KB Output is correct
10 Correct 10 ms 12044 KB Output is correct
11 Correct 12 ms 12152 KB Output is correct
12 Correct 10 ms 12152 KB Output is correct
13 Correct 11 ms 12152 KB Output is correct
14 Correct 12 ms 12024 KB Output is correct
15 Correct 10 ms 12024 KB Output is correct
16 Correct 12 ms 12152 KB Output is correct
17 Correct 10 ms 12024 KB Output is correct
18 Correct 12 ms 12024 KB Output is correct
19 Correct 10 ms 12152 KB Output is correct
20 Correct 12 ms 12024 KB Output is correct
21 Correct 12 ms 12024 KB Output is correct
22 Correct 11 ms 12152 KB Output is correct
23 Correct 10 ms 12024 KB Output is correct
24 Correct 10 ms 12152 KB Output is correct
25 Correct 312 ms 12536 KB Output is correct
26 Correct 295 ms 12536 KB Output is correct
27 Correct 299 ms 12664 KB Output is correct
28 Correct 289 ms 12556 KB Output is correct
29 Correct 361 ms 12580 KB Output is correct
30 Correct 352 ms 12536 KB Output is correct
31 Correct 350 ms 12664 KB Output is correct
32 Correct 369 ms 12536 KB Output is correct
33 Correct 17 ms 12536 KB Output is correct
34 Correct 17 ms 12664 KB Output is correct
35 Correct 17 ms 12668 KB Output is correct
36 Correct 16 ms 12664 KB Output is correct
37 Correct 14 ms 12664 KB Output is correct
38 Correct 16 ms 12664 KB Output is correct
39 Correct 16 ms 12408 KB Output is correct
40 Correct 18 ms 12412 KB Output is correct
41 Correct 22 ms 12408 KB Output is correct
42 Correct 33 ms 12408 KB Output is correct
43 Correct 50 ms 12412 KB Output is correct
44 Correct 83 ms 12280 KB Output is correct
45 Correct 159 ms 12280 KB Output is correct
46 Correct 167 ms 12280 KB Output is correct
47 Correct 400 ms 12404 KB Output is correct
48 Correct 419 ms 12536 KB Output is correct
49 Execution timed out 2067 ms 59036 KB Time limit exceeded
50 Halted 0 ms 0 KB -