| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 93469 | Flying_dragon_02 | Telegraph (JOI16_telegraph) | C++14 | 1077 ms | 16232 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define int long long
typedef pair<int, int> ii;
const int N = 1e5 + 5;
const int inf = 1e15;
int c[N], par[N], f[N], ck[N], res[2], val[N], ans, dp[N], n, lmao[N];
vector<int> graph[N], cycle, vec;
bool vis[N];
void dfs(int u) {
if(lmao[u]) return ;
vec.pb(u);
if(vis[u] == 1) {
int p = par[u];
cycle.pb(u);
while(p != u) {
cycle.pb(p);
p = par[p];
}
reverse(cycle.begin(), cycle.end());
return ;
}
vis[u]++;
for(int i = 0; i < graph[u].size(); i++) {
int v = graph[u][i];
par[v] = u;
dfs(v);
}
}
void bfs(int u) {
vis[u] = 1;
int mx = 0;
for(int i = 0; i < graph[u].size(); i++) {
int v = graph[u][i];
if(ck[v]) continue;
bfs(v);
dp[u] += dp[v];
dp[u] += c[v];
mx = max(mx, c[v]);
}
dp[u] -= mx;
val[u] = mx;
}
signed main() {
cin.tie(0), ios::sync_with_stdio(0);
//freopen("test.inp", "r", stdin);
//freopen("test.out", "w", stdout);
cin >> n;
for(int i = 1; i <= n; i++) {
int p;
cin >> p >> c[i];
graph[p].pb(i);
}
for(int i = 1; i <= n; i++) {
if(!vis[i]) {
vec.clear();
cycle.clear();
dfs(i);
for(int j = 0; j < vec.size(); j++)
lmao[vec[j]] = 1;
if(cycle.size() == 0) continue;
if(cycle.size() == n) {
cout << "0\n";
exit(0);
}
for(int j = 0; j < cycle.size(); j++)
ck[cycle[j]] = 1;
memset(f, 0, sizeof(f));
f[1] = inf;
int m = cycle.size();
for(int j = 0; j < m; j++) {
int u = cycle[j], nxt = cycle[(j + 1) % m];
bfs(u);
f[1] = min(f[1] + dp[cycle[j]] + val[cycle[j]], f[0] + dp[cycle[j]] + c[nxt]);
f[0] = f[0] + dp[cycle[j]] + val[cycle[j]];
f[0] = min(f[1], f[0]);
}
ans = ans + f[1];
}
}
cout << ans;
}
컴파일 시 표준 에러 (stderr) 메시지
| # | 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... | ||||
