题目
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
vector<int> eg[N], lg[N];
int in[N], out[N];
int early[N], late[N];
int w[N];
bool cannt = false;
void dfs(int i, int mode)
{
if(mode == 0)
{
if(!eg[i].size()) return;
for(auto c : eg[i])
{
early[c] = max(early[c], early[i] + w[i]);
dfs(c, 0);
}
}
else
{
if(cannt) return;
if(!lg[i].size()) return;
for(auto c : lg[i])
{
late[c] = min(late[c], late[i] - w[c]);
if(late[c] <= 0) cannt = true;
dfs(c, 1);
}
}
}
int main()
{
int n, m;
cin >> n >> m;
for(int i = 1; i <= m; i++)
{
int depend;
cin >> depend;
if(depend)
{
eg[depend].push_back(i);
lg[i].push_back(depend);
out[depend]++;
in[i]++;
}
}
for(int i = 1; i <= m; i++)
{
cin >> w[i];
early[i] = 1;
late[i] = n-w[i]+1;
}
for(int i = 1; i <= m; i++)
{
if(!in[i] && out[i]) dfs(i, 0);
else if(!out[i] && in[i]) dfs(i, 1);
}
for(int i = 1; i <= m; i++)
{
cout << early[i] << " ";
}
cout << endl;
if(!cannt)
{
for(int i = 1; i <= m; i++)
{
cout << late[i] << " ";
}
}
return 0;
}