54分求调
查看原帖
54分求调
641163
abensyl楼主2022/12/4 20:53

https://www.luogu.com.cn/record/96579032

#include<bits/stdc++.h>

using namespace std;

const int V = 107;
int hd[V];
int id[V];
int cnt[V];
int low[V];
int dfn[V];
int stk[V];
int scc,tp;
int idx=0;
int in[V];
int out[V];
struct Edge {
	int to,nx;
} eg[V*V];

void adeg(int st,int ed) {
	eg[++idx]={ed,hd[st]};
	hd[st]=idx;
}

void tarjan(int u,int &clk) {
	low[u]=dfn[u]=++clk;
	stk[++tp]=u;
	
	for(int i=hd[u];i;i=eg[i].nx) {
		int v=eg[i].to;
		if(!dfn[v])
			tarjan(v,clk),
			low[u]=min(low[u],low[v]);
		else if(!id[v])
			low[u]=min(low[u],low[v]);
	}

	if(low[u]==dfn[u])
		for(++scc;stk[tp+1]!=u;--tp)
			id[stk[tp]]=scc,++cnt[scc];
}

int main() {
  ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	int n;
	cin>>n;
	for(int i=1;i<=n;++i) {
		while(true) {
			int zc;
			cin>>zc;
			if(zc) adeg(i,zc);
			else break;
		}
	}

	for(int i=1,clk=0;i<=n;++i)
		if(!id[i]) tarjan(i,clk);
  
	for(int i=1;i<=n;i++){
		for(int e=hd[i];e;e=eg[e].nx){
			if(id[i]!=id[eg[e].to]){
				in[id[eg[e].to]]=true;
				out[id[i]]=true;
			}
		}
	}
	
	int ans=0,res=0;

	for(int i=1;i<=scc;++i) 
		res+=!((bool)out[i]),ans+=!((bool)in[i]);

	cout<<ans<<'\n';
	if(scc!=1) cout<<min(ans,res)<<'\n';
	else cout<<0<<'\n';
    
	return 0;
}
2022/12/4 20:53
加载中...