| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 1301630 | escobrand | Telegraph (JOI16_telegraph) | C++20 | 27 ms | 8084 KiB |
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
#define all(v) v.begin(),v.end()
#define mk make_pair
#define pb push_back
#define eb emplace_back
typedef pair<int,int> pii;
const int maxn = 1e5 + 10;
int f[maxn],t;
int r[maxn],pre[maxn],dih[maxn];
int com[maxn];
int c[maxn];
stack<int> st;
bool in_stack[maxn];
void dfs(int i)
{
st.push(i);
in_stack[i] = 1;
c[i] = 1;
int k = f[i];
if(c[k])
{
if(in_stack[k])
{
t++;
while(st.top() != k)
{
in_stack[st.top()] = 0;
com[st.top()] = t;
st.pop();
}
in_stack[st.top()] = 0;
com[st.top()] = t;
st.pop();
}
}
else dfs(k);
in_stack[i] = 0;
}
vector<int> v[maxn];
vector<pii> adj[maxn];
ll ans = 0;
int mn[maxn];
void cal(int i)
{
int mx = 0;
for(pii k: adj[i])
{
cal(k.fi);
ans += k.se;
mx = max(mx,k.se);
}
ans -= mx;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
if(fopen("PIPELINK.INP","r"))
{
freopen("PIPELINK.INP","r",stdin);
freopen("PIPELINK.OUT","w",stdout);
}
int n;
cin>>n;
for(int i = 1;i<=n;i++)
{
cin>>f[i]>>r[i];
dih[i] = INT_MAX;
}
for(int i = 1;i<=n;i++)
{
if(!c[i])dfs(i);
int k = f[i];
if(com[i] && com[k]) pre[k] = r[i];
if(com[i]) dih[com[i]] = min(dih[com[i]],r[i]);
if(!com[i] && com[k])v[com[k]].pb(i);
if(!com[i] && !com[k])adj[k].pb(mk(i,r[i]));
}
if(t==1 && v[1].empty())
{
cout<<0;
return 0;
}
for(int i = 1;i<=t;i++)
{
for(int k : v[i])
{
cal(k);
ans += r[k];
int g = f[k];
int tmp = pre[g] - r[k];
dih[i] = min(dih[i],tmp);
if(mn[g] > tmp)
{
ans -= mn[g];
mn[g] = tmp;
ans += tmp;
}
}
if(dih[i]>0)ans += dih[i];
}
cout<<ans<<'\n';
// cout<<(ll)648069973 + 844154274 + 825028282 + 79650124;
return 0;
}
컴파일 시 표준 에러 (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... | ||||
