QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#84273 | #5651. Parmigiana With Seafood | CharlieVinnie | RE | 4ms | 5764kb | C++14 | 934b | 2023-03-06 08:43:33 | 2023-03-06 08:43:36 |
Judging History
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