2957 {
2958 unordered_map<int, unordered_set<int>> adj;
2959 for(auto &p: pairs) {
2960 adj[p[0]].emplace(p[1]);
2961 adj[p[1]].emplace(p[0]);
2962 }
2963
2965 for(auto &[node, neighbours]: adj) {
2966 if(neighbours.size() == adj.size() - 1) {
2968 break;
2969 }
2970 }
2971 if(root == -1) {
2972 return 0;
2973 }
2974
2975 int res = 1;
2976 for(auto &[node, neighbours]: adj) {
2977 if(node == root) {
2978 continue;
2979 }
2980 const int currDegree = neighbours.size();
2981 int parent = -1;
2982 int parentDegree = INT_MAX;
2983
2984
2985 for(const auto &neighbour: neighbours) {
2986 if(adj[neighbour].size() < parentDegree && adj[neighbour].size() >= currDegree) {
2987 parent = neighbour;
2988 parentDegree = adj[neighbour].size();
2989 }
2990 }
2991 if(parent == -1) {
2992 return 0;
2993 }
2994
2995
2996 for(const auto &neighbour: neighbours) {
2997 if(neighbour == parent) {
2998 continue;
2999 }
3000 if(static_cast<unsigned int>(adj[parent].contains(neighbour)) == 0U) {
3001 return 0;
3002 }
3003 }
3004 if(parentDegree == currDegree) {
3005 res = 2;
3006 }
3007 }
3008 return res;
3009 }