求调!
查看原帖
求调!
908416
zhuletian楼主2024/5/28 16:52
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;

vector<int> g[110];
bool f1[110], f2[110];

void DFS(int u)
{
	for (int v : g[u])
	{
		if (!f1[v])
		{
			f1[v] = f2[v] = true;
			DFS(v);
			f1[v] = false;
		}
	}
}

int main()
{
	int n;
	cin >> n;
	for (int i = 1, x, y; i < n; i++)
	{
		cin >> x >> y;
		g[y].push_back(x);
	}
	for (int i = 1; i <= n; i++)
	{
		memset(f1, false, sizeof f1);
		memset(f2, false, sizeof f2);
		DFS(i);
		bool flag = true;
		for (int j = 1; j <= n; j++)
		{
			if (!f2[j])  flag = false;
		}
		if (flag)
		{
			cout << i;
			return 0;
		}
	}
	cout << -1;
	return 0;
}
2024/5/28 16:52
加载中...