不知道那一步出错了
查看原帖
不知道那一步出错了
446185
恨天剑仙楼主2020/12/10 06:40
#include<cstdio>
#include<iostream>
#include<string.h>
#include<set>
using namespace std;
const int maxn = 50000;
int fa[maxn];
set<int>s;
int main() {

		int a, b;
		bool A = true;
		int i=0;
		while(scanf("%d%d",&a,&b)!=EOF)
		{
			if (a == 0 && b == 0)
				{
					if (A == true&&s.size()==1)
			            cout << "Case" << " " << ++i << " "<<"is a tree." <<'\n';
		            else {			
			             cout << "Case" << " " << ++i << " " << "is not a tree." <<'\n';
		           }
		                s.clear();
		                memset(fa, 0, sizeof(fa));
		                continue;
				}								
				//打破循环的条件最好放在最前面,或者用while
		else if (a!=-1&&b!=-1)		
		{
				if (fa[b] != 0 && fa[b]!= a&&fa[b]!=b)
			//要注意他除了前面两个条件之外还有可能是根的情况
			{
				A = false;
			}
			
			if (s.count(b))
				s.erase(b);
				
			fa[b] = a;
			
			if (fa[a]== 0)
			{
                s.insert(a);
     			fa[a] = a;				
			}	
			}
			else if(a==-1&&b==-1)
			break;					
		}	
	return 0;
}
2020/12/10 06:40
加载中...