이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
#define debug(s) cout<< #s <<" = "<< s <<endl
#define all(v) (v).begin(), (v).end()
#define KeepUnique(v) (v).erase( unique(all(v)), v.end() )
#define MEMSET(a,val) memset(a, val, sizeof a)
#define PB push_back
#define endl '\n'
typedef long long ll;
inline int myrand(int l, int r) {
int ret = rand(); ret <<= 15; ret ^= rand();
if(ret < 0) ret = -ret; ret %= (r-l+1); ret += l;
return ret;
}
template <typename F, typename S>
ostream& operator << (ostream& os, const pair< F, S>& p) {
return os<<"(" <<p.first<<", "<<p.second<<")"; }
typedef pair<int, int> ii;
template<typename T> using min_pq =
priority_queue<T, vector<T>, greater<T> >;
//int dx[] = {-1, +0, +1, +0};
//int dy[] = {+0, +1, +0, -1};
const int maxn = 100005;
int n, m;
vector<int> g[maxn];
int lvl[maxn];
vector<int> paths[maxn];
vector<int> A, B, C;
vector<int> shitz[maxn], pathnodes[maxn];
int t[18][maxn];
int dp[maxn];
int S[maxn];
int dfs(int u, int par) {
t[0][u] = par;
lvl[u] = par == -1 ? 0 : lvl[par] + 1;
for(int v : g[u]) {
if(par - v) {
dfs(v, u);
}
}
}
int __lca(int u, int v) {
if(lvl[u] > lvl[v]) swap(u, v);
int d = lvl[v] - lvl[u];
for(int i = 16; i >= 0; i--) {
if(d & (1 << i)) {
v = t[i][v];
}
}
if(u == v) return u;
for(int i = 16; i >= 0; i--) {
if(t[i][u] != t[i][v]) {
u = t[i][u];
v = t[i][v];
}
} return t[0][u];
}
int pathcost(int i, int ret = 0) {
for(int u : pathnodes[i]) ret += S[u]; return ret;
}
void prep(int u, int par) {
for(int v : g[u]) {
if(v - par) {
prep(v, u);
S[u] += dp[v];
}
}
dp[u] = S[u];
for(int p : paths[u]) {
int ret = C[p] + pathcost(p);
dp[u] = max(dp[u], ret);
}
S[u] -= dp[u];
}
int32_t main () {
// freopen("in", "r", stdin);
cin >> n;
for(int i = 1; i < n; i++) {
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
MEMSET(t, -1);
dfs(1, -1);
for(int p = 1; p <= 16; p++) {
for(int i = 1; i <= n; i++) {
int mid = t[p - 1][i];
if(mid != -1) t[p][i] = t[p - 1][mid];
}
}
cin >> m;
A.resize(m), B.resize(m), C.resize(m);
for(int i = 0; i < m; i++) {
cin >> A[i] >> B[i] >> C[i];
int lca = __lca(A[i], B[i]);
paths[lca].push_back(i);
}
for(int i = 0; i < m; i++) {
set<int> st;
int lca = __lca(A[i], B[i]), now;
now = A[i];
while(now != lca) st.insert(now), now = t[0][now];
now = B[i];
while(now != lca) st.insert(now), now = t[0][now];
st.insert(lca);
for(int b : st) pathnodes[i].push_back(b);
// set<int> All;
// for(int u : st) {
// for(int v : g[u]) {
// All.insert(v);
// }
// }
// for(int u : st) All.erase(u); // remove all nodes on path
// if(t[0][lca] != -1) All.erase(t[0][lca]); // remove parent of lca
// for(int u : All) {
// shitz[i].push_back(u);
// }
}
prep(1, -1);
cout << dp[1] << endl;
}
컴파일 시 표준 에러 (stderr) 메시지
election_campaign.cpp: In function 'int myrand(int, int)':
election_campaign.cpp:17:2: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
if(ret < 0) ret = -ret; ret %= (r-l+1); ret += l;
^~
election_campaign.cpp:17:26: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
if(ret < 0) ret = -ret; ret %= (r-l+1); ret += l;
^~~
election_campaign.cpp: In function 'int dfs(int, int)':
election_campaign.cpp:51:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
election_campaign.cpp: In function 'int pathcost(int, int)':
election_campaign.cpp:71:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
for(int u : pathnodes[i]) ret += S[u]; return ret;
^~~
election_campaign.cpp:71:41: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
for(int u : pathnodes[i]) ret += S[u]; return ret;
^~~~~~
# | 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... |