QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#359691 | #410. Telegraph | weajink2 | 0 | 2ms | 8716kb | C++14 | 1.8kb | 2024-03-20 20:03:14 | 2024-03-20 20:03:28 |
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 21372137213722;
ll koszt[100001];
ll kraw[100001];
vector<pair<ll,ll> > sas[100001];
vector<ll> sas2[100001];
bool vis[100001];
bool nacyklu[100001];
bool vis2[100001];
vector<ll> spojna;
void DFS(ll v){
vis2[v] = 1;
spojna.push_back(v);
for (ll u : sas2[v]) if(!vis2[u]) DFS(u);
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
ll i;
ll n;
cin>>n;
for (i = 1; i <= n; i++){
cin>>kraw[i]>>koszt[i];
sas[kraw[i]].push_back({koszt[i],i});
sas2[kraw[i]].push_back(i);
sas2[i].push_back(kraw[i]);
}
for (i = 1; i <= n; i++){
sort(sas[i].begin(),sas[i].end());
reverse(sas[i].begin(),sas[i].end());
}
ll laczwy = 0;
ll ilespojnych = 0;
bool czycykl = 1;
for (ll v = 1; v <= n; v++){
if (vis2[v]) continue;
ilespojnych++;
ll wy = 0;
ll roz = INF;
spojna.clear();
DFS(v);
vis[v] = 1;
vector<ll> sciezka;
sciezka.push_back(1);
while (1){
v = kraw[v];
if (vis[v]){
nacyklu[v] = 1;
while (sciezka.back() != v){
nacyklu[sciezka.back()] = 1;
sciezka.pop_back();
}
break;
}
sciezka.push_back(v);
vis[v] = 1;
}
for (ll u : spojna){
if (!sas[u].size()){
czycykl = 0;
continue;
}
for (auto j : sas[u]){
wy += j.first;
}
if (sas[u].size() > 1 && nacyklu[u] && nacyklu[sas[u][0].second]){
//cout<<i<<" "<<sas[i][0].first<<" "<<sas[i][1].first<<"\n";
roz = min(roz,sas[u][0].first-sas[u][1].first);
}else if (nacyklu[u] && nacyklu[sas[u][0].second]) roz = min(roz,sas[u][0].first);
wy -= sas[u][0].first;
}
if (roz == INF) laczwy += wy;
else laczwy += wy+roz;
}
if (ilespojnych == 1 && czycykl) cout<<"0\n";
else cout<<laczwy<<"\n";
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Runtime Error
Test #1:
score: 10
Accepted
time: 2ms
memory: 8716kb
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: -10
Runtime Error
input:
10 3 688989554 5 112566798 1 294281317 1 96941112 6 746415334 2 612836992 9 629868215 1 406822326 10 638640051 8 199983696
output:
result:
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #1:
0%
Subtask #4:
score: 0
Skipped
Dependency #1:
0%