洛谷 P2835 刻录光盘

时间:2021-09-07 00:19:00

https://www.luogu.org/problem/show?pid=2835
这道题目
先把没有入度的点去灌水一遍;
然后对于剩下的每一个图;
他们不一定是一个环;
但是一定包含一个环;
我们只要找到一个在环上的点,那么就可以吧整个图都灌水;

#include<bits/stdc++.h>
#define Ll long long
using namespace std;
const int N=205;
struct cs{int to,nxt;}a[N*N*2];
int head[N],ll,v[N];
bool A[N],ok[N];
int n,m,x,y,z,ans;
void init(int x,int y){
a[++ll].to=y;
a[ll].nxt=head[x];
head[x]=ll;
}
void dfs(int x,int y){
v[x]=y;
for(int k=head[x];k;k=a[k].nxt)
if(!v[a[k].to])dfs(a[k].to,y);else
if(v[a[k].to]==y)ok[y]=1;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&x);
while(x){
A[x]=1;
init(i,x);
scanf("%d",&x);
}
}
for(int i=1;i<=n;i++)if(!A[i])ans++,dfs(i,N-1);
for(int i=1;i<=n;i++)if(!v[i])dfs(i,i);
for(int i=1;i<=n;i++)if(ok[i])ans++;
printf("%d",ans);
}