QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#359359 | #410. Telegraph | jerzyk | 0 | 1ms | 3856kb | C++20 | 2.3kb | 2024-03-20 16:56:52 | 2024-03-20 16:56:53 |
answer
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define st first
#define nd second
typedef long long ll;
typedef long double ld;
const ll I = 1000LL * 1000LL * 1000LL * 1000LL * 1000LL * 1000LL;
const ll M = 1000LL * 1000LL * 1000LL + 7LL;
const int N = 1<<17;
bool czd[N];
ll cst[N], dp[N];
vector<int> ed[N];
int il[N], poi[N];
int LiczD(int n)
{
int v, cnt = 0;
queue<int> q;
for(int i = 1; i <= n; ++i)
if(il[i] == 0) q.push(i);
while(q.size() > 0)
{
++cnt;
v = q.front(); q.pop();
czd[v] = true;
ll ma = 0;
for(int i = 0; i < (int)ed[v].size(); ++i)
{dp[v] += dp[ed[v][i]] + cst[ed[v][i]]; ma = max(ma, cst[ed[v][i]]);}
dp[v] -= ma;
--il[poi[v]];
if(il[poi[v]] == 0)
q.push(poi[v]);
}
return cnt;
}
ll LiczC(int v)
{
ll ans = 0LL, ma = -I;
int x = poi[v];
vector<int> ver;
ver.pb(v);
while(x != v)
{
ver.pb(x);
x = poi[x];
}
for(int i = 0; i < (int)ver.size(); ++i)
{
int pr;
if(i > 0) pr = ver[i - 1];
else pr = ver.back();
v = ver[i];
ll cur = 0LL;
for(int j = 0; j < (int)ed[v].size(); ++j)
if(czd[ed[v][j]])
{ans += dp[ed[v][j]] + cst[ed[v][j]]; cur = max(cur, cst[ed[v][j]]);}
cur -= cst[pr];
ma = max(ma, cur);
//cout << "cycle: " << v << " " << cur << " " << ma << " " << ans << "\n";
}
for(int i = 0; i < (int)ver.size(); ++i)
czd[ver[i]] = true;
ans -= ma;
return ans;
}
void Solve()
{
int n, xd;
ll ans = 0LL;
cin >> n;
for(int i = 1; i <= n; ++i)
{
cin >> poi[i] >> cst[i];
ed[poi[i]].pb(i); ++il[poi[i]];
}
xd = LiczD(n);
for(int i = 1; i <= n; ++i)
if(!czd[i])
{
++xd;
ans += LiczC(i);
}
if(xd == 1)
cout << 0 << "\n";
else
cout << ans << "\n";
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
//int t; cin >> t;
//while(t--)
Solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 10
Accepted
time: 1ms
memory: 3608kb
input:
10 3 85377744 3 191391530 4 553475509 8 344887257 9 812158120 1 880268352 4 686078706 7 182546394 4 220137367 3 89957933
output:
1081551750
result:
ok single line: '1081551750'
Test #2:
score: 0
Accepted
time: 1ms
memory: 3856kb
input:
10 3 688989554 5 112566798 1 294281317 1 96941112 6 746415334 2 612836992 9 629868215 1 406822326 10 638640051 8 199983696
output:
503789227
result:
ok single line: '503789227'
Test #3:
score: 0
Accepted
time: 1ms
memory: 3608kb
input:
10 3 292601364 7 33742065 7 35087125 3 996478615 4 828156197 3 197921984 8 426174077 4 631098259 5 57142735 5 457493107
output:
1212506407
result:
ok single line: '1212506407'
Test #4:
score: 0
Accepted
time: 1ms
memory: 3852kb
input:
10 6 43696822 9 954917333 7 923376581 5 896016118 1 762413411 4 930490625 3 369963586 9 855374192 2 475645419 1 567518871
output:
1936096612
result:
ok single line: '1936096612'
Test #5:
score: -10
Wrong Answer
time: 0ms
memory: 3784kb
input:
10 6 647308632 3 876092601 4 664182389 6 648069973 7 844154274 5 663059265 1 166269448 3 79650124 7 894148102 7 825028282
output:
2396902653
result:
wrong answer 1st lines differ - expected: '2396141312', found: '2396902653'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #1:
0%
Subtask #4:
score: 0
Skipped
Dependency #1:
0%