이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <cstring>
#include <queue>
#include <unordered_set>
#include <unordered_map>
#include <chrono>
#define FOR(var,bound) for(int var = 0; var < bound; var++)
#define FORB(var,lb,ub) for (int var = lb; var < ub; var++)
#define FORR(var,bound) for(int var = bound-1; var >= 0; var--)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> pii;
vvi conn;
vector<signed> p;
vector<int> dp(1000000);
int bestNode = -1;
int bestVal = 2000000005;
int dfs1(int n, int par) {
int res = p[n];
for (int x: conn[n]) {
if (x != par) {
res += dfs1(x, n);
}
}
return dp[n] = res;
}
void reroot(int oldRoot, int newRoot) {
dp[oldRoot] -= dp[newRoot];
dp[newRoot] += dp[oldRoot];
}
void dfs2(int n, int par) {
int traffic = 0;
for (int x: conn[n]) {
traffic = max(traffic, dp[x]);
if (x == par) continue;
reroot(n, x);
dfs2(x, n);
reroot(x, n);
}
if (traffic < bestVal) {
bestVal = traffic;
bestNode = n;
}
#ifdef LOCAL_TEST
//cout << n << " -> " << traffic << endl;
#endif
}
int LocateCentre(int N, int* P, int* S, int* D) {
p.resize(N);
FOR(i, N) p[i] = P[i];
conn.resize(N);
FOR(i, N - 1) {
conn[S[i]].push_back(D[i]);
conn[D[i]].push_back(S[i]);
}
int m = N / 2;
dfs1(m, -1);
dfs2(m, -1);
return bestNode;
}
#ifdef LOCAL_TEST
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
srand(NULL);
int n;
//cin >> n;
n = 1000000;
vi p(n), s(n-1), d(n-1);
FOR(i, n) {
//cin >> p[i];
p[i] = rand() % 2000;
}
FOR(i, n -1) {
//cin >> s[i] >> d[i];
s[i] = i;
d[i] = i + 1;
}
auto start = chrono::high_resolution_clock::now();
cout << LocateCentre(n, p.data(), s.data(), d.data()) << endl;
auto end = chrono::high_resolution_clock::now();
cout << chrono::duration_cast<chrono::milliseconds>(end - start).count() << endl;
}
#endif
# | 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... |