您的位置:首页 > 其它

【例题 6-16 UVa 10129】Play on Words

2017-11-17 06:43 429 查看

【链接】 我是链接,点我呀:)
【题意】


在这里输入题意

【题解】


入度减去出度个数为1的点和为-1的点各有1个,然后其他点入度减去出度为0
或者全都是入度为0的点即可。

【代码】

#include <bits/stdc++.h>
using namespace std;

const int N = 300;

int n,du
;
map <char,int> mmap;
int f[N+10];
bool bo[N+10];

int ff(int x){
if (f[x]==x) return x;
else return f[x] =  ff(f[x]);
}

int main(){
#ifdef LOCAL_DEFINE
freopen("F:\\c++source\\rush_in.txt", "r", stdin);
#endif
ios::sync_with_stdio(0),cin.tie(0);
int T;
cin >> T;
while (T--){
memset(du,0,sizeof du);
memset(bo,0,sizeof bo);
for (int i = 'a';i <= 'z';i++)
f[i] = i;
cin >> n;
string s;
for (int i = 1;i <=n;i++){
cin >> s;
int x = s[0],y = s[(int) s.size()-1];
du[(int) s[0]]++;
du[(int) s[(int) s.size()-1]]--;
bo[(int) s[0]] = true;
bo[(int) s[(int) s.size()-1]] = true;
if (ff(x)!=ff(y)){
f[ff(x)] = ff(y);
}

}

bool ok = true;
int idx = 'a';
for (int i = 'a';i <= 'z';i++)
if (bo[i]){
idx = ff(i);
}
for (int i = 'a';i <= 'z';i++)
if (bo[i]&& ff(i)!=idx)
ok = false;
if (!ok){
cout <<"The door cannot be opened."<<endl;
continue;
}

int fi1 = 0,fi2 = 0;
for (int i = 'a';i <= 'z';i++)
if (du[i]==-1)
fi1++;
else if (du[i]==1){
fi2++;
}else if (du[i]!=0) fi1 = 2;

if ((fi1==1 && fi2==1) || (fi1==0 && fi2==0)){
cout << "Ordering is possible." << endl;
}else cout <<"The door cannot be opened."<<endl;
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: