You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
749 B
32 lines
749 B
#include <bits/stdc++.h>
|
|
using namespace std;
|
|
const int N = 110;
|
|
int n, d[N][N], ans;
|
|
int main() {
|
|
scanf("%d", &n);
|
|
|
|
int u, v;
|
|
for (int i = 1; i < n; i++) scanf("%d%d", &u, &v), d[u][v] = 1;
|
|
|
|
// Floyd求传递闭包
|
|
for (int k = 1; k <= n; k++)
|
|
for (int i = 1; i <= n; i++)
|
|
for (int j = 1; j <= n; j++)
|
|
d[i][j] |= d[i][k] & d[k][j];
|
|
|
|
for (int i = 1; i <= n; i++) {
|
|
int cnt = 0;
|
|
for (int j = 1; j <= n; j++)
|
|
if (d[j][i]) cnt++; //有多少个点可以到达i点
|
|
|
|
if (cnt == n - 1) {
|
|
ans = i;
|
|
break;
|
|
}
|
|
}
|
|
if (ans)
|
|
printf("%d", ans);
|
|
else
|
|
puts("-1");
|
|
return 0;
|
|
} |