#include<bits/stdc++.h>
using namespace std;
int bin[1005];
int a[1005];
int b[1005];
int tp[1005];
int find(int x){
if(bin[x] == x)return x;
return bin[x] = find(bin[x]);
}
void merge(int x, int y){
x = find(x);
y = find(y);
if(x != y)bin[y] = x;
}
int main(){
ios::sync_with_stdio(false);
int n;
while(cin >> n){
for(int i = 1; i <= n ; i ++)
bin[i] = i;
for(int i = 1; i <= n ; i ++){
cin >> a[i] >> b[i];
if(a[i] == 1){
merge(i, b[i]);
}
}
memset(tp,0,sizeof(tp));
for(int i = 1; i <= n ; i ++){
if(a[i] == 0){
int k = 0;
for(int j = 1; j <= n ; j ++){
if(find(j) == find(i))
k ++;
}
tp[b[i]] += k;
}
}
int _max = 0, k;
for(int i = 1; i <= n; i ++){
if(tp[i] > _max){
k = i;
_max = tp[i];
}
}
for(int i = 1; i <= n ; i ++){
if(_max == tp[i] && k != i){
k = 0;
break;
}
}
cout << k << endl;
}
return 0;
}
CYB狼人杀(归票)(并查集)
最新推荐文章于 2023-05-24 19:25:01 发布