이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/stdc++.h"
using namespace std;
#define ff first
#define ss secox
#define all(v) v.begin(), v.ex()
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define tr(i, c) for(auto i = c.begin(); i != c.end(); ++i)
#define wr puts("----------------")
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
const int N = 5005;
vector<int> adj[N];
int answer, sm, dp[N], odp[N];
void wow(int x, int pr = -1){
dp[x] = odp[x] = 0;
tr(it, adj[x]){
int to = (*it);
if(to != pr){
wow(to, x);
int same = dp[x];
if(umax(same, dp[to]+1))
odp[x] = dp[x], dp[x] = same;
else if(odp[x] < dp[to]+1)
odp[x] = dp[to]+1;
}
}
}
void dfs(int x, int pr, int dis, int mx){
if(~pr and (int)adj[x].size()==1){
if(umax(answer, dis*mx))
sm = 1;
else if(dis*mx == answer)
sm++;
return;
}
tr(it, adj[x]){
int to = (*it);
if(to != pr){
if(dp[to]+1 == dp[x])
dfs(to, x, dis+1, max(odp[x],mx));
else
dfs(to, x, dis+1, max(dp[x],mx));
}
}
}
int main(){
int n;
scanf("%d", &n);
for(int i = 1; i < n; ++i){
int a, b;
scanf("%d%d", &a, &b);
adj[a].pb(b);
adj[b].pb(a);
}
vector<int> A;
for(int i = 1; i <= n; ++i)
if((int)adj[i].size()==1)
A.pb(i);
for(int &to : A){
wow(to);
dfs(to, -1, 0, 0);
}
printf("%d %d\n", answer, sm>>1);
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
road.cpp: In function 'int main()':
road.cpp:55:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
55 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
road.cpp:58:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
58 | scanf("%d%d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |