http://acm.hdu.edu.cn/showproblem.php?pid=2094
思路蘸嘶,用map建立節(jié)點(diǎn)記錄名字與id信息脑豹,統(tǒng)計(jì)所有入度為1的節(jié)點(diǎn)申屹,有且只有一個(gè)入度為1的節(jié)點(diǎn)的情況就是yes丛晌,否則no
#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <map>
using namespace std;
int inDegree[10005]={0};
int main()
{
int n, t, temp, cnt;
string p,q;
map<string,int> name;
while(scanf("%d", &n) && n!=0)
{
for(int i=0;i<=n;i++)
inDegree[i] = 0;
name.clear();
t = 1;
while(n--)
{
cin>>p>>q;
if(!name.count(p))
{
name.insert(make_pair(p,t));
t++;
}
if(!name.count(q))
{
name.insert(make_pair(q,t));
t++;
}
temp = name[q];
inDegree[temp]++;
}
cnt = 0;
for(int i = 1; i < t; i++)
{
if(inDegree[i]==0)
cnt++;
}
if(cnt==1)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
出現(xiàn)了很多意外的情況镜廉,map是邊百度邊寫(xiě)的,完全不會(huì)用寓涨,也很神奇自己能想起來(lái)盯串,但是后面由于把大小寫(xiě)寫(xiě)錯(cuò)了以為是這邊的問(wèn)題,其實(shí)是大小寫(xiě)和初始化的問(wèn)題戒良。從今往后做這種有多組輸入數(shù)據(jù)使用STL模板和數(shù)組建立狀態(tài)信息的題目或者工程的時(shí)候一定要記得初始化体捏,老友!E雌椤<哥浴!