이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define pii pair<int,int>
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define mem(a, b) memset(a, b, sizeof(a))
#define name "test"
using namespace std;
const int N = 3e5 + 5;
const int mod = 1e9 + 7;
template <typename T1, typename T2> bool maxi(T1 &a, T2 b){if (a < b){a = b; return 1;} return 0;}
template <typename T1, typename T2> bool mini(T1 &a, T2 b){if (a > b){a = b; return 1;} return 0;}
int n;
int s;
int t;
bool stop[N];
vector<int> path;
vector<int> dp[N];
vector<int> adj[N];
void load(){
cin.tie(0)->sync_with_stdio(0);
if (fopen(name".inp", "r")){
freopen(name".inp", "r", stdin);
freopen(name".out", "w", stdout);
}
}
void input(){
cin >> n >> s >> t;
for (int i = 1; i < n; i++){
int u, v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
}
bool getPath(int u, int pre){
if (u == t){
path.push_back(u);
return 1;
}
for (int v : adj[u]) if (v != pre){
if (getPath(v, u)){
path.push_back(u);
return 1;
}
}
return 0;
}
int dfs(int u, int pre, bool type){
dp[u].clear();
for (int v : adj[u]) if (v != pre && !stop[v]){
dp[u].push_back(dfs(v, u, type));
}
sort(dp[u].begin(), dp[u].end(), greater<int>());
int res = 0;
for (int i = 0; i < (int)dp[u].size(); i++)
maxi(res, dp[u][i] + i + 1);
if (res) cout << u << " " << res << '\n';
return res;
}
int cal(int j){
int ans = 0;
stop[path[j + 1]] = 1;
ans = dfs(s, 0, 0);
stop[path[j + 1]] = 0;
stop[path[j]] = 1;
maxi(ans, dfs(t, 0, 1));
stop[path[j]] = 0;
return ans;
}
void solve(){
if (s == t){
assert(false);
cout << dfs(s, 0, 0);
return;
}
getPath(s, 0);
reverse(path.begin(), path.end());
int ans = cal(0);
int l = 0, r = (int)path.size() - 2;
while (l < r){
int m1 = (l + r) >> 1;
int m2 = m1 + 1;
int tmp1 = cal(m1);
int tmp2 = cal(m2);
mini(ans, min(tmp1, tmp2));
if (tmp1 <= tmp2) r = m1;
else l = m2;
}
cout << ans;
}
int main(){
load();
input();
solve();
}
컴파일 시 표준 에러 (stderr) 메시지
torrent.cpp: In function 'void load()':
torrent.cpp:30:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | freopen(name".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
torrent.cpp:31:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | freopen(name".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |