이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define scan(x) do{while((x=getchar())<'0'); for(x-='0'; '0'<=(_=getchar()); x=(x<<3)+(x<<1)+_-'0');}while(0)
char _;
#define pii pair<int, int>
#define mp make_pair
#define f first
#define s second
vector<int> edges[1005], edges2[1005];
vector<pair<pii, vector<pii>>> paths[1005];
vector<pair<pii, int>> unpaved;
int parent[1005], depth[1005], parity[1005], ind[1005][1005], dp[1005][1024], precalc[5005];
int ans(int v, int b) {
if (dp[v][b] != -1) return dp[v][b];
dp[v][b] = 0;
for (int i : edges[v]) {
if ((b&ind[v][i]) == 0) dp[v][b] += ans(i, 0);
}
for (auto path : paths[v]) {
if (b&path.s.back().s) continue;
if (precalc[path.f.s] == -1) {
precalc[path.f.s] = path.f.f;
for (int i=0; i+1<path.s.size(); i++) precalc[path.f.s] += ans(path.s[i].f, path.s[i].s);
}
dp[v][b] = max(dp[v][b], precalc[path.f.s]+ans(v, b^path.s.back().s));
}
return dp[v][b];
}
int main() {
int n, m, a, b, c, v, p, d, t, total = 0, pathnum = 0;
scan(n);
scan(m);
while (m--) {
scan(a);
scan(b);
scan(c);
total += c;
if (c) unpaved.push_back({{a, b}, c});
else {
edges2[a].push_back(b);
edges2[b].push_back(a);
}
}
vector<tuple<int, int, int, int>> st = {{1, 0, 0, 0}};
while (!st.empty()) {
tie(v, p, d, t) = st.back();
st.pop_back();
parent[v] = p;
depth[v] = d;
parity[v] = t;
for (int i : edges2[v]) {
if (i != p) {
edges[v].push_back(i);
st.push_back({i, v, d+1, 1^t});
}
}
}
for (int i=1; i<=n; i++) {
for (int j=0; j<edges[i].size(); j++) ind[i][edges[i][j]] = 1<<j;
}
for (auto i : unpaved) {
a = i.f.f, b = i.f.s, c = i.s;
if (parity[a] != parity[b]) continue;
if (depth[a] < depth[b]) swap(a, b);
vector<pii> path = {{a, 0}};
while (depth[a] != depth[b]) {
path.emplace_back(parent[a], ind[parent[a]][a]);
a = parent[a];
}
if (a != b) {
path.emplace_back(b, 0);
while (parent[a] != parent[b]) {
path.emplace_back(parent[a], ind[parent[a]][a]);
a = parent[a];
path.emplace_back(parent[b], ind[parent[b]][b]);
b = parent[b];
}
path.emplace_back(parent[a], ind[parent[a]][a]^ind[parent[a]][b]);
a = parent[a];
}
paths[a].push_back({{c, pathnum++}, path});
}
fill(&dp[0][0], &dp[1004][0], -1);
fill(&precalc[0], &precalc[5004], -1);
printf("%d\n", total-ans(1, 0));
}
컴파일 시 표준 에러 (stderr) 메시지
training.cpp: In function 'int ans(int, int)':
training.cpp:29:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | for (int i=0; i+1<path.s.size(); i++) precalc[path.f.s] += ans(path.s[i].f, path.s[i].s);
| ~~~^~~~~~~~~~~~~~
training.cpp: In function 'int main()':
training.cpp:66:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | for (int j=0; j<edges[i].size(); j++) ind[i][edges[i][j]] = 1<<j;
| ~^~~~~~~~~~~~~~~~
# | 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... |
# | 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... |