# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1115493 | EfeBabagil | Jobs (BOI24_jobs) | C++14 | 65 ms | 31816 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 <bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e5+5;
const int mod=1e9+7;
vector<int> arr[N];
int vis[N];
stack<int> st;
void dfs(int x)
{
vis[x]=1;
for(int i=0;i<arr[x].size();i++)
{
if(!vis[i])
dfs(i);
}
st.push(x);
}
int32_t main()
{
vis[0]=1;
int n,s;
cin>>n>>s;
vector<array<int,3>> jobs(n+1);
vector<int> visd(n+1);
int mon=s;
jobs[0]={0,-1,0};
visd[0]=1;
for(int i=1;i<=n;i++)
{
int a,b;
cin>>a>>b;
arr[i].push_back(b);
jobs[i]={a,b,i};
if(b==0)
{
if(a>0)
{
mon+=a;
visd[i]=1;
}
}
}
for(int i=1;i<=n;i++)
{
if(!vis[i])
dfs(i);
}
vector<int> topo;
while(!st.empty())
{
topo.push_back(st.top());
//cout<<st.top()<<" ";
st.pop();
}
for(int i=0;i<n;i++)
{
array<int,3> x=jobs[topo[i]];
int prof=0;
vector<int> done;
while(!visd[x[2]])
{
prof+=x[0];
done.push_back(x[2]);
x=jobs[x[1]];
}
if(prof>0)
{
for(int i=0;i<done.size();i++)
visd[done[i]]=1;
mon+=prof;
}
}
cout<<mon-s;
return 0;
}
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... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |