# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
55118 | ainta | Telegraph (JOI16_telegraph) | C++17 | 110 ms | 8424 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
vector<int>E[101000];
int n, C[101000], w[101000], Deg[101000], Q[101000], head, tail, v[101000], chk;
long long res;
void Do(int a) {
int t = a, i, rr = 1e9;
vector<int>T;
while (1) {
v[t] = 1;
T.push_back(t);
t = w[t];
if (t == a)break;
}
if (T.size() == n) {
chk = 1;
return;
}
for (auto &x : T) {
int Mx1 = -1, Mx2 = 0;
for (auto &y : E[x]) {
if (Deg[y])Mx1 = max(Mx1, C[y]);
else Mx2 = max(Mx2, C[y]);
}
rr = min(rr, Mx1 - Mx2);
}
rr = max(rr, 0);
res += rr;
}
int main() {
int i;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%d%d", &w[i],&C[i]);
Deg[w[i]]++;
E[w[i]].push_back(i);
}
for (i = 1; i <= n; i++) {
vector<int>T;
for (auto &t : E[i]) {
T.push_back(C[t]);
}
if (!T.empty()) {
sort(T.begin(), T.end());
for (int j = 0; j < T.size() - 1; j++) {
res += T[j];
}
}
}
for (i = 1; i <= n; i++)if (!Deg[i])Q[++tail] = i;
while (head < tail) {
int x = Q[++head];
v[x] = 1;
Deg[w[x]]--;
if (!Deg[w[x]])Q[++tail] = w[x];
}
for (i = 1; i <= n; i++) {
if (!v[i]) {
Do(i);
}
}
if (chk) {
printf("0\n");
return 0;
}
printf("%lld\n", res);
}
Compilation message (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... |