92求助
查看原帖
92求助
332914
happybob楼主2022/3/17 19:24

RE 了一个点,为什么啊?

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <ctime>
using namespace std;

constexpr int N(1e5 + 5);

int in[N], in2[N];
int n;

inline int read()
{
	register char ch(getchar());
	register int x(0);
	while (ch < '0' || ch > '9') ch = getchar();
	while (ch >= '0' and ch <= '9')
	{
		x = (x << 1) + (x << 3) + (ch ^ 48);
		ch = getchar();
	}
	return x;
}

vector<int> G[N];

inline void bfs()
{
	queue<int> q;
	register int cnt(0);
	for (register int i(1); i <= n; i = -~i)
	{
		if (in2[i] == 0)
		{
			q.push(i);
			++cnt;
		}
	}
	while (!q.empty())
	{
		register int u(q.front());
		q.pop();
		++in[u];
		if (G[u].size() > 1) continue;
		q.push(G[u][0]);
	}
	for (register int i(1); i <= n; i = -~i)
	{
		if (in[i] == cnt && G[i].size() && in2[i]) printf("%d\n", i);
	}
}

int main()
{
	n = read();
	for (register int i(1); i < n; i = -~i)
	{
		register int a(read()), b(read());
		G[a].push_back(b);
		//G[b].push_back(a);
		in2[b]++;
	}
	bfs();
	return 0;
}
2022/3/17 19:24
加载中...