题解 | #Is It A Tree?#
Is It A Tree?
http://www.nowcoder.com/practice/1c5fd2e69e534cdcaba17083a5c56335
#include<iostream>
#include<map>
using namespace std;
int main()
{
int s, e, ss = 0, cnt;
bool ans = true;
map<int, int> mp; //s --> e
while (cin >> s >> e) {
if (s == 0 && e == 0) {
cnt = 0;
ss++;
for (auto it : mp) {
if (it.second == 0) {
cnt += 1;
if (cnt > 1) {
ans = false;
break;
}
}
if (it.second > 1) {
ans = false;
break;
}
}
if(cnt == 0 && mp.size() == 0)
ans = true;
else if(cnt == 0)
ans = false;
cout << "Case " << ss << " is " << (ans == false? "not " : "") << "a tree." << endl;
mp.clear();
ans = true;
}
else {
if (mp.find(e) == mp.end())
mp[e] = 1;
else
mp[e] += 1;
if (mp.find(s) == mp.end())
mp[s] = 0;
if (s == e)
ans = false;
}
}
}