이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
//#include "towns.h"
#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll INFL = (1LL<<62)-1;
const int INF = (1<<30)-1;
const int MAXN = 0;
int getDistance(int i, int j);
int hubDistance(int n, int sub){
vector<int> ds (n,0), db(n,0), da(n,-1);
int a, b;
int maxi = -1;
for(int i = 1; i < n; i++){
ds[i] = getDistance(0,i);
if(ds[i] > maxi)
maxi = ds[i], b = i;
}
maxi = -1;
db[0] = ds[b];
for(int i = 1; i < n; i++){
if(i==b)
continue;
db[i] = getDistance(b,i);
if(db[i] > maxi)
maxi = db[i], a = i;
}
da[a] = 0;
da[b] = db[a];
da[0] = ds[a];
vector<int> dlca(n,0), v(n,0);
for(int i = 0; i < n; i++){
dlca[i] = (db[0] + db[i] - ds[i])>>1;
}
iota(v.begin(), v.end(), 0);
sort(v.begin(),v.end(),[&](int x, int y){
return dlca[x] > dlca[y];
});
vector<bool> type(n,0);
// se type = 1, faz parte da subarvore de s
int resp = max(ds[a] + db[a] - ds[b],ds[b] + da[b] - ds[a])>>1;
// resposta comeca com o ponto do caminho (a,b) que intersecta com o caminho de (s,a) e (s,b)
type[0] = 1;
vector<int> retirar;
for(int i = 1; i < n; i++){
int cur = v[i];
if(dlca[cur] == dlca[v[i-1]]){
type[cur] = 1;
retirar.push_back(cur);
continue;
}
if(da[cur] == -1)
da[cur] = getDistance(a,cur);
int d1 = (da[cur] + db[a] - db[cur])>>1;
int d2 = (db[cur] + da[b] - da[cur])>>1;
if(max(d1,d2) > resp){
break;
}
resp = max(d1,d2);
type[cur] = 1;
retirar.clear();
retirar.push_back(cur);
}
for(int i : retirar)
type[i] = 0;
return resp;
}
컴파일 시 표준 에러 (stderr) 메시지
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:17:28: warning: unused parameter 'sub' [-Wunused-parameter]
17 | int hubDistance(int n, int sub){
| ~~~~^~~
towns.cpp:29:17: warning: 'b' may be used uninitialized in this function [-Wmaybe-uninitialized]
29 | db[0] = ds[b];
| ^
towns.cpp:37:9: warning: 'a' may be used uninitialized in this function [-Wmaybe-uninitialized]
37 | da[a] = 0;
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |