이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define fi first
#define se second
#define ii pair < int , int >
using namespace std;
const int maxn = 3e5 + 5;
int p[maxn] , id[maxn] , dp[maxn] , chl[maxn];
int u[maxn] , v[maxn];
bool vs[maxn];
vector < ii > g[maxn];
vector < int > adj[maxn] , vc;
int n , a , b;
void DFS(int u) {
for (auto v : g[u])
if (p[u] != v.fi) {
p[v.fi] = u;
id[v.fi] = v.se;
DFS(v.fi);
}
}
void init(int u,int ban) {
vs[u] = true;
chl[u] = 1;
for (auto v : g[u])
if (!vs[v.fi] && v.se != ban) {
adj[u].push_back(v.fi);
chl[u] += chl[v.fi];
init(v.fi , ban);
}
}
void GO(int u) {
for (int i = 0 ; i < adj[u].size() ; ++i) {
int v = adj[u][i];
GO(v);
dp[u] = max(dp[u] , dp[v] + i + 1);
}
}
bool cmp(int a,int b) {
return chl[a] < chl[b];
}
int get(int root,int ban) {
for (int i = 1 ; i <= n ; ++i) {
vs[i] = chl[i] = dp[i] = 0;
adj[i].clear();
}
init(root , ban);
for (int i = 1 ; i <= n ; ++i)
sort(adj[i].begin() , adj[i].end() , cmp);
GO(root);
return dp[root];
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin >> n >> a >> b;
for (int i = 1 ; i < n ; ++i) {
cin >> u[i] >> v[i];
g[u[i]].emplace_back(v[i] , i);
g[v[i]].emplace_back(u[i] , i);
}
DFS(a);
int tmp = b;
while (tmp != a) {
vc.push_back(id[tmp]);
tmp = p[tmp];
}
int l = 0;
int r = vc.size() - 1;
int res = 1e9;
while (l <= r) {
int mid = l + r >> 1;
int vala = get(a , vc[mid]);
int valb = get(b , vc[mid]);
res = min(res , max(vala , valb));
if (vala > valb) l = mid + 1;
else r = mid - 1;
}
cout << res;
}
컴파일 시 표준 에러 (stderr) 메시지
torrent.cpp: In function 'void GO(int)':
torrent.cpp:38:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for (int i = 0 ; i < adj[u].size() ; ++i) {
| ~~^~~~~~~~~~~~~~~
torrent.cpp: In function 'int get(int, int)':
torrent.cpp:51:24: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
51 | vs[i] = chl[i] = dp[i] = 0;
| ~~~~~~~^~~~~~~~~~~
torrent.cpp: In function 'int main()':
torrent.cpp:80:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
80 | int mid = l + r >> 1;
| ~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |