QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#84444#5651. Parmigiana With Seafoodjohnsonloy_xWA 46ms37372kbC++142.0kb2023-03-06 15:03:332023-03-06 15:04:56

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-06 15:04:56]
  • 评测
  • 测评结果:WA
  • 用时:46ms
  • 内存:37372kb
  • [2023-03-06 15:03:33]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

#define ll long long
const int mod = 1e9+7;
const int maxn = 1e6+10;

namespace IO{
	void openfile(){
		#ifndef ONLINE_JUDGE
		freopen("in.in","r",stdin);
		freopen("out.out","w",stdout);
		#endif
	}

	int add(int x,int y){
		x+=y;
		if(x>=mod)
			x-=mod;
		return x;
	}

	int sub(int x,int y){
		x-=y;
		if(x<0)
			x+=mod;
		return x;
	}

	inline int read(){
		int x = 0,f = 0;
		char c = getchar();
		while(!isdigit(c))
			f|=c=='-',c = getchar();
		while(isdigit(c))
			x = x*10+c-'0',c = getchar();
		if(f)x = -x;
		return x;
	}
}
using namespace IO;

int n,dep[maxn],ans,f[maxn][2];
set<int>q;
vector<int>e[maxn],d[2];

void dfs(int x,int fa){
	dep[x] = dep[fa]+1;
	for(int i=0;i<e[x].size();i++){
		int v = e[x][i];
		if(v==fa)continue;
		dfs(v,x);
	}
}

void dfs1(int x,int fa){
	if(dep[x]&1){
		set<int>::iterator y = q.lower_bound(x);
		if(y!=q.begin())
			y--,ans = max(ans,*y);
	}
	for(int i=0;i<e[x].size();i++){
		int v = e[x][i];
		if(v==fa)continue;
		dfs1(v,x);
	}
	q.insert(x);
}

void dfs2(int x,int fa){
	f[x][dep[x]&1] = x;
	for(int i=0;i<e[x].size();i++){
		int v = e[x][i];
		if(v==fa)continue;
		dfs2(v,x);
		f[x][0] = max(f[x][0],f[v][0]);
		f[x][1] = max(f[x][1],f[v][1]);
	}
}

signed main(){
//	openfile();
	n = read();
	for(int i=1;i<n;i++){
		int x = read(),y = read();
		e[x].push_back(y),e[y].push_back(x);
	}
	if(!(n&1)){
		printf("%d\n",n);
		return 0;
	}
	for(int i=1;i<=n;i++)
		if(e[i].size()==1)
			ans = max(ans,i);
	dfs(n,0);
	for(int i=1;i<=n;i++)
		if(!(dep[i]&1))
			ans = max(ans,i);
	dfs1(n,0);
	dfs2(n,0);
	for(int i=0;i<e[n].size();i++){
		int v = e[n][i];
		d[0].push_back(f[v][0]);
		d[1].push_back(f[v][1]);
	}
	sort(d[0].begin(),d[0].end());
	sort(d[1].begin(),d[1].end());
	if(e[n].size()>=3)
		ans = max(ans,max(d[0][e[n].size()-3],d[1][e[n].size()-3]));
	printf("%d\n",ans);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 11ms
memory: 28816kb

input:

4
1 2
1 3
1 4

output:

4

result:

ok single line: '4'

Test #2:

score: 0
Accepted
time: 2ms
memory: 28772kb

input:

5
1 5
5 3
3 4
4 2

output:

3

result:

ok single line: '3'

Test #3:

score: -100
Wrong Answer
time: 46ms
memory: 37372kb

input:

99999
81856 39633
81856 94012
99999 43062
99946 220
81856 46131
99933 36505
99939 35662
99952 70971
99999 3275
99938 58416
99976 66658
99991 87922
81856 80992
99933 6392
99951 41047
99970 54115
81856 38150
99934 73554
81856 64578
81856 18576
99951 67996
99938 84479
81856 39617
99999 18664
99946 2505...

output:

99997

result:

wrong answer 1st lines differ - expected: '99925', found: '99997'