이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
int n,dp[5005],ans,cnt,id[5005],root,dp2[5005];
bool leaf[5005];
vector<int>g[5005];
void build(int v,int p){
dp[v]=0;
dp2[v]=0;
for(int to:g[v])if(to!=p){
build(to,v);
if(dp[to]+1>dp[v]){
dp[v]=dp[to]+1;
id[v]=to;
}
}
for(int to:g[v])if(to!=p&&to!=id[v])dp2[v]=max(dp2[v],dp[to]+1);
}
void dfs(int v,int p,int depth,int mx){
if(v!=root&&leaf[v]&&leaf[root]){
//cout<<root<<"-"<<v<<" "<<depth<<" "<<max(mx,dp[v])<<endl;
int tmp=depth*max(mx,dp[v]);
if(tmp>ans){
ans=tmp;
cnt=1;
}else if(tmp==ans)cnt++;
}
for(int to:g[v])if(to!=p){
if(to==id[v]){
dfs(to,v,depth+1,max(mx,dp2[v]));
}else{
dfs(to,v,depth+1,max(mx,dp[v]));
}
}
}
int main(){
scanf("%d",&n);
for(int i=1;i<n;i++){
int x,y;
scanf("%d%d",&x,&y);
g[x].push_back(y);
g[y].push_back(x);
}
for(int i=1;i<=n;i++)leaf[i]=(g[i].size()==1);
for(root=1;root<=n;root++){
if(!leaf[root])continue;
build(root,root);
dfs(root,root,0,0);
}
if(ans==0)cnt=2;
printf("%d %d",ans,(cnt>>1));
}
컴파일 시 표준 에러 (stderr) 메시지
road.cpp: In function 'int main()':
road.cpp:36:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
36 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
road.cpp:39:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
39 | scanf("%d%d",&x,&y);
| ~~~~~^~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |