资深光能
谁知道为啥WA了
#include <iostream>
#include <map>
#include <cstring>
using namespace std;
int n, m;
map<string, int> mp;
map<int, string> mp2;
bool f[25][25];
int cnt;
int main() {
int tmp = 0;
while(cin >> n >> m) {
cnt ++;
tmp = 0;
if(n == 0 && m == 0) continue;
mp.clear();
mp2.clear();
memset(f, false, sizeof(f));
for(int i=1; i<=m; i++) {
string s1, s2;
cin >> s1 >> s2;
if(!mp[s1]) mp[s1] = ++tmp, mp2[tmp] = s1;
if(!mp[s2]) mp[s2] = ++tmp, mp2[tmp] = s2;
f[mp[s1]][mp[s2]] = true;
}
for(int i=1; i<=n; i++) f[i][i] = true;
for(int k=1; k<=n; k++) {
for(int i=1; i<=n; i++) {
if(f[i][k]) {
for(int j=1; j<=n; j++) {
f[i][j] |= f[i][k] && f[k][j];
}
}
}
}
cout << "Calling circles for data set " << cnt << ":" << endl;
int vis[25] = {0};
for(int i=1; i<=n; i++) {
if(!vis[i]) {
vis[i] = true;
cout << mp2[i];
for(int j=1; j<=n; j++) {
if(f[i][j] && f[j][i] && !vis[j]) {
vis[j] = true;
cout << ", " << mp2[j];
}
}
cout << endl;
}
}
}
return 0;
}
周琪岳在2021-06-19 20:01:40追加了内容
@黄子阳 @侯平仄 @曹灿阳
周琪岳在2021-06-29 19:11:59追加了内容
顶(附:祝贺dmy巨佬AKIOI)
周琪岳在2021-06-30 17:00:58追加了内容
顶
资深天翼
?
int n,m,cnt,s=0;
string x,y;
while(输入n,m)){
if(n==0&&m==0) break;
memset(b,0,sizeof(b));
memset(g,0,sizeof(g));
H.clear();//注意map清空方法
cnt=0;
循环(int i=1;i<=m;i++){
cin>>x>>y;
if(H[x]==0) cnt++,H[x]=cnt,ans[cnt]=x;//map映射
if(H[y]==0) cnt++,H[y]=cnt,ans[cnt]=y;
g[H[x]][H[y]]=1;
}
循环(int k=1;k<=n;k++)
循环(int i=1;i<=n;i++)
循环(int j=1;j<=n;j++)
g[i][j]|=g[i][k]&&g[k][j];//Floyd主代码
输出("Calling circles 循环 data set %d:\n",++s);
queue<string> q;
循环(int i=1;i<=n;i++){
if(b[i]) continue;//避免重复输出
q.push(ans[i]);
循环(int j=i+1;j<=n;j++)//看是否在同一连通块
if(g[i][j]&&g[j][i])
q.push(ans[j]),b[j]=1;
while(q.size()!=1){//愚蠢的输出方法
cout<<q.front()<<", ";
q.pop();
}
输出q.front;
q.pop();
}
}
结束;