Submission #1301630

#TimeUsernameProblemLanguageResultExecution timeMemory
1301630escobrandTelegraph (JOI16_telegraph)C++20
100 / 100
27 ms8084 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;
}

Compilation message (stderr)

telegraph.cpp: In function 'int main()':
telegraph.cpp:73:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |         freopen("PIPELINK.INP","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
telegraph.cpp:74:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   74 |         freopen("PIPELINK.OUT","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...