建议加强数据!!暴力dfs都有72分!!!
查看原帖
建议加强数据!!暴力dfs都有72分!!!
90027
fanypcd楼主2020/7/26 18:41

hhh

暴力72分,TLE3个点hhh
#include<bits/stdc++.h>
using namespace std;
int tot =  0;
int next[100005], first[100005], to[100005], vis[100005], n, m;
void add(int x, int y)
{
	tot++;
	next[tot] = first[x];
	first[x] = tot;
	to[tot] = y;
}
void dfs(int x)
{
	if(vis[x] == 1)
	{
		return;
	}
	vis[x] = 1;
	for(int i = first[x]; i; i = next[i])
	{
		int u = to[i];
		dfs(u);
	}
	return;
}
int check()
{
	for(int i = 1; i <= n; i++)
	{
		if(vis[i] == 0)
		{
			return 0;
		}
	}
	return 1;
}
int main()
{
	int ans = 0;
	cin >> n >> m;
	int a, b;
	for(int i = 1; i <= m; i++)
	{
		cin >> a >> b;
		add(b, a);
	}
	for(int i = 1; i <= n; i++)
	{
		memset(vis, 0, sizeof(vis));
		dfs(i);
		if(check())
		{
			ans++;
		}
	}
	cout << ans;
	return 0;
}
2020/7/26 18:41
加载中...