QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#84274#5651. Parmigiana With SeafoodCharlieVinnieWA 72ms10640kbC++14956b2023-03-06 08:45:282023-03-06 08:45:30

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:45:30]
  • 评测
  • 测评结果:WA
  • 用时:72ms
  • 内存:10640kb
  • [2023-03-06 08:45:28]
  • 提交

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[n]=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; int cc=1;
	Rev(i,n-1,1) if(col[i]==col[n]){
		cc++; int u=i; while(!vis[u]) vis[u]=1,u=fa[u];
		if(col[u]==col[n]&&cc>2) { 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: 2ms
memory: 5736kb

input:

4
1 2
1 3
1 4

output:

4

result:

ok single line: '4'

Test #2:

score: 0
Accepted
time: 1ms
memory: 5680kb

input:

5
1 5
5 3
3 4
4 2

output:

3

result:

ok single line: '3'

Test #3:

score: 0
Accepted
time: 64ms
memory: 10396kb

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:

99925

result:

ok single line: '99925'

Test #4:

score: -100
Wrong Answer
time: 72ms
memory: 10640kb

input:

99997
90325 59106
22545 8765
88871 37709
14739 95233
8778 29659
48110 57549
91258 76066
15724 65144
48244 87291
12076 94378
41946 96707
93645 12812
53817 34343
72097 94062
81212 263
78713 78150
6754 94906
20957 97539
59293 5018
77961 78090
57262 95225
79349 47902
99024 7869
10613 13728
61757 41090
4...

output:

99995

result:

wrong answer 1st lines differ - expected: '85398', found: '99995'