求助!RE致10pts
查看原帖
求助!RE致10pts
556455
AllureLove2410喜欢若彤楼主2022/11/12 22:34

rt,code:

#include<bits/stdc++.h>
using namespace std;
const int xrt=1e4+3;
int n;
struct zsm{
	int to;
	int next;
}e[5000003];
int cnt;
int head[xrt];
void insery(int x,int y){
	e[++cnt].to=y;
	e[cnt].next=head[x];
	head[x]=cnt;
}
int dfn[xrt];
int scc[xrt];
int sccin[xrt];
int sccout[xrt];
int idx;
int low[xrt];
int sccnum;
stack<int>stk;
void dfs(int root){
	//cout<<root<<"\n";
	if(dfn[root]){
		return;
	}
	dfn[root]=low[root]=++idx;
	stk.push(root);
	for(int i=head[root];i;i=e[i].next){
		int t=e[i].to;
		if(!dfn[t]){
			dfs(t);
			low[root]=min(low[root],low[t]);
		}else if(!scc[t]){
			low[root]=min(low[root],dfn[t]);
		}
		if(low[root]==dfn[root]){
			sccnum++;
			while(true){
				int x=stk.top();
				stk.pop();
				scc[x]=sccnum;
				if(x==root)break;
			}
		}
	}
	return;
}
int main(){
	//ios::sync_with_stdio(0);
	//cin.tie(0),cout.tie(0);
//	freopen("1.in","r",stdin);
	cin>>n;
	for(int i=1,t;i<=n;i++){
		while(true){
			cin>>t;
			if(t==0)break;
			insery(i,t);
		//	cout<<"e:"<<i<<" "<<t<<"\n";
		}
		//cout<<t<<"\n";
	}
//	dfs(1);
//	return 0;
	for(int i=1;i<=n;i++){
		if(!dfn[i])dfs(i);
	}
	for(int x=1;x<=n;x++){
		for(int i=head[x];i;i=e[i].next){
			int t=e[i].to;
			if(scc[x]!=scc[t]){
				sccin[scc[t]]+=1;
				sccout[scc[x]]+=1;
			}
		}
	}
	int cntin=0,cntout=0;
	for(int i=1;i<=sccnum;i++){
		if(!sccin[i])cntin+=1;
		if(!sccout[i])cntout+=1;
	}
	if(sccnum==1){
		cout<<"1\n0\n";
	}else{
		cout<<cntin<<"\n"<<max(cntin,cntout)<<"\n";
	}
	return 0;
}
2022/11/12 22:34
加载中...