이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
//#define int long long
#define endl "\n"
int mod=1e9+7;
const int N=1e5+5;
template<class x>
using ordered_multiset = tree<x, null_type,less_equal<x>, rb_tree_tag,tree_order_statistics_node_update>;
int n,m,t;
vector<vector<int> > v(N),vv(N);
vector<int> topo;
int ans[N];
vector<vector<int> > par(N),ch(N);
bool vis[N];
void dfs(int i) {
vis[i]=true;
for (int j:vv[i]) {
if (!vis[j]) {
dfs(j);
}
}
topo.push_back(i);
}
signed main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen(".in", "r", stdin); freopen(".out", "w", stdout);
cin>>n>>m>>t;
for (int i=0;i<m;i++) {
int a,b; cin>>a>>b;
v[a].push_back(b);
vv[b].push_back(a);
ch[b].push_back(a);
}
for (int tt=0;tt<t;tt++) {
bool busy[N];
memset(busy,false,sizeof busy);
int plc; cin>>plc;
int b; cin>>b;
for (int i=0;i<b;i++) {
int x; cin>>x; busy[x]=true;
}
topo.clear();
for (int i=0;i<N;i++) {par[i].clear(); vis[i]=false;}
dfs(plc);
reverse(topo.begin(),topo.end());
memset(vis,false,sizeof vis);
queue<int> q;
int dis[N];
for (int i=0;i<N;i++) dis[i]=0;
for (int i:topo) {
for (int j:ch[i]) {
par[j].push_back(i);
}
}
for (int i:topo) {
if (i==plc) continue;
int mx=0;
for (int j:par[i]) {
mx=max(mx,dis[j]);
}
dis[i]=mx+1;
}
int cnt=-1;
for (int i=1;i<=n;i++) {
if (!busy[i]) cnt=max(cnt,dis[i]);
}
ans[tt]=cnt;
}
for (int i=0;i<t;i++) cout<<ans[i]<<endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |