这题显然的深搜,然后我刚开始打成
#include<bits/stdc++.h>
const int maxn = 1e5+5;
using namespace std;
int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f*=(-1);ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
return x*f;
}
vector<int> ve[maxn];
bool vis[maxn];
bool flag;
int tot=0,ans=-1;
void dfs(int now){
if(flag) return ;
if(ve[now].empty()){ans=tot;flag=1;return ;}
int nx=ve[now][0];
if(vis[nx]){flag=1;return ;}
tot++;
dfs(nx);
}
int main(){
int n,m,p;
n=read();
m=read();
p=read();
for(int i=1;i<=n;i++){
int u,v;
u=read();
v=read();
ve[v].push_back(u);
}
vis[p]=1;
dfs(p);
cout<<ans;
return 0;
}
只把开始点vis了上交居然有95分,只剩一个1s的超时,只能说数据太弱了。