QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#84273#5651. Parmigiana With SeafoodCharlieVinnieRE 4ms5764kbC++14934b2023-03-06 08:43:332023-03-06 08:43:36

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 08:43:36]
  • 评测
  • 测评结果:RE
  • 用时:4ms
  • 内存:5764kb
  • [2023-03-06 08:43:33]
  • 提交

answer

#include <bits/stdc++.h>
#define For(i,a,b) for(int i=a;i<=b;i++)
#define Rev(i,a,b) for(int i=a;i>=b;i--)
#define Fin(file) freopen(file,"r",stdin)
#define Fout(file) freopen(file,"w",stdout)
using namespace std; using ll = long long;
const int N=1e5+5;
int n,col[N],fa[N],vis[N]; vector<int> to[N];
void dfs(int u,int pa){
	fa[u]=pa; for(int v:to[u]) if(v!=pa) col[v]=3-col[u],dfs(v,u);
}
int main(){
	cin>>n; For(i,1,n-1) { int x,y; cin>>x>>y; to[x].push_back(y); to[y].push_back(x); }
	if(n%2==0||to[n].size()==1u) { cout<<n; exit(0); }
	col[1]=1; dfs(n,0);
	int hh[3]={0}; For(i,1,n) hh[col[i]]=max(hh[col[i]],i);
	int ans=min(hh[1],hh[2]); For(i,1,n) if(to[i].size()==1u) ans=max(ans,i);
	//~ if(n==97687) cout<<hh[1]<<' '<<hh[2]<<'\n';
	vis[n]=1;
	Rev(i,n-1,1) if(col[i]==col[n]){
		int u=i; while(!vis[u]) vis[u]=1,u=fa[u];
		if(col[u]==col[n]) { ans=max(ans,i); break; }
	}
	cout<<ans;
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 5764kb

input:

4
1 2
1 3
1 4

output:

4

result:

ok single line: '4'

Test #2:

score: -100
Dangerous Syscalls

input:

5
1 5
5 3
3 4
4 2

output:


result: