'commit'
This commit is contained in:
32
TangDou/AcWing/Floyd/1471.cpp
Normal file
32
TangDou/AcWing/Floyd/1471.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user