답안 #92847

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
92847 2019-01-05T10:00:19 Z emil_physmath Hard route (IZhO17_road) C++17
0 / 100
4 ms 2680 KB
#include <iostream>
#include <stdio.h>
#include <vector>
#include <cstring>
using namespace std;
const int MAXN=100005;

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);
pair<int, int> merge(const pair<int, int> &, const pair<int, int> &);
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, maxdist[u].second);
			else curAns=DFS(to, dep+1, 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);
			pair<int, int> temp=maxdist[to];
			temp.first++;
			if (temp.second) temp.second++;
			maxdist[u]=merge(maxdist[u], temp);
		}
	}
}

pair<int, int> merge(const pair<int, int> & a, const pair<int, int> & b)
{
	pair<int, int> temp;
	temp.first=max(a.first, b.first);
	temp.second=max(min(a.first, b.first), max(a.second, b.second));
	return temp;
}

Compilation message

road.cpp: In function 'std::pair<int, int> DFS(int, int, int)':
road.cpp:50: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:71: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:22: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 4 ms 2680 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
3 Correct 4 ms 2680 KB Output is correct
4 Correct 3 ms 2680 KB Output is correct
5 Correct 3 ms 2680 KB Output is correct
6 Correct 4 ms 2680 KB Output is correct
7 Incorrect 4 ms 2680 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
3 Correct 4 ms 2680 KB Output is correct
4 Correct 3 ms 2680 KB Output is correct
5 Correct 3 ms 2680 KB Output is correct
6 Correct 4 ms 2680 KB Output is correct
7 Incorrect 4 ms 2680 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
3 Correct 4 ms 2680 KB Output is correct
4 Correct 3 ms 2680 KB Output is correct
5 Correct 3 ms 2680 KB Output is correct
6 Correct 4 ms 2680 KB Output is correct
7 Incorrect 4 ms 2680 KB Output isn't correct
8 Halted 0 ms 0 KB -