有几个测试点始终过不了,求教
查看原帖
有几个测试点始终过不了,求教
431815
罗权楼主2021/8/8 09:35
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<string.h>
#include<math.h>
using namespace std;
int n, m;
int head_1[100010], next_1[100010], to_1[100010];
int total_1 = 0;
int dfn[100010], low[100010];
int book[100010], color[100010];
int cnt = 0, tot = 0, color_cnt = 0;
int num_size = 0;
int sta[100010];
int dot_num = 0;//表示我们在缩了之后点的数目
void add_1(int x, int y) {
	total_1++;
	to_1[total_1] = y;
	next_1[total_1] = head_1[x];
	head_1[x] = total_1;
}
void Tarjan(int x, int fa) {
	low[x] = ++tot;
	dfn[x] = tot;
	book[x] = 1;
	sta[++num_size] = x;
	for (int e = head_1[x]; e; e = next_1[e]) {
		if (!dfn[to_1[e]]) {
			Tarjan(to_1[e], x);
			low[x] = min(low[x], low[to_1[e]]);
		}
		else if (book[to_1[e]]) low[x] = min(low[x], dfn[to_1[e]]);
	}
	if (dfn[x] == low[x]) {
		color[x] = ++color_cnt;
		book[x] = 0;
		while (sta[num_size] != x) {
			color[sta[num_size]] = color_cnt;
			book[sta[num_size]] = 0;
			num_size--;
		}
		num_size--;
		dot_num++;
	}
}
int head_2[100010], next_2[100010], to_2[100010];//缩点之后的图储存的地方
int total_2 = 0;
int degree[100010];
void add_2(int x, int y) {
	total_2++;
	degree[x]++;
	to_2[total_2] = y;
	next_2[total_2] = head_2[x];
	head_2[x] = total_2;
}
void change() {
	for (int i = 1; i <= n; i++) {
		for (int e = head_1[i]; e; e = next_1[e]) {
			if (color[i] != color[to_1[e]]) {
				add_2(color[i], color[to_1[e]]);
			}
		}
	}
}
int visit[100010];
int main() {
	cin >> n >> m;
	for (int i = 1; i <= m; i++) {
		int x, y;
		cin >> x >> y;
		add_1(x,y);
	}
	for (int i = 1; i <= n; i++) {
		if (dfn[i] == 0) {//避免有的点不连通 
			Tarjan(i, 0);
		}
	}
	change();
	if (dot_num == 1) {
		cout << n << endl;
	}
	else {
		int ans = 0;
		for (int i = 1; i <= dot_num; i++) {//如果有多个出度为0的点,则不可能为超级牛明星
			if (degree[i] == 0) ans++;
		}
		if (ans != 1) {
			cout << "0" << endl;
		}
		else {
			cout << "1" << endl;
		}
	}
	return 0;
}
2021/8/8 09:35
加载中...