1. 程式人生 > >google要線上筆試----bad horse 二分圖

google要線上筆試----bad horse 二分圖

一開始想得不夠全面,很傻很天真:

輸入如果兩個都存在,並且相等那麼則出錯

輸入如果一個存在那麼另一個則與其相反

輸入如果都不存在那麼一個為1一個為0,就是這個地方,01選擇的問題最終決定還是二分圖吧

想要不等他輸完就出結果,顯然不對,當然測試用例通過了百分之90 多,但是有幾個出現問題了:

例如:

N M    0 1

L D     0 1

D Q     1 0

Q N      0 0

這時應該出錯了,但是這個是沒有錯的因為如果一開始我們把N M設為1 0後面就沒錯了因此呢,這樣不對

#include<iostream>
#include<string>
#include<map>
using namespace std;
#define MAX 101
struct node
{
int color;
node *friends[MAX];
};
int main()
{

freopen("D:\\a3.in","r",stdin);
freopen("D:\\b.out","w",stdout);
/********************************************/
int T;
cin>>T;
int id=1;
while(T--)
{
map<string,int> m;
int M;
cin>>M;
int r=0;
for(int i=0;i<M;i++)
{
string a,b;
cin>>a>>b;
if(m.count(a)>0&&m.count(b)>0)//如果都存在
{
if(m[a]==m[b])
{
r=1;
}
}
else if(m.count(a)>0)
{
m[b]=(m[a]==0)?1:0;
}
else if(m.count(b)>0)
{
m[a]=(m[b]==0)?1:0;
}
else
{
m[a]=0;
m[b]=1;
}
}
if(r==0)
cout<<"Case #"<<id<<": Yes"<<endl;
else
cout<<"Case #"<<id<<": No"<<endl;
id++;
}
/********************************************/
fclose(stdin);
fclose(stdout);
return 0;
}