社区讨论
啊???!!
P1407[国家集训队] 稳定婚姻参与者 3已保存回复 2
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 2 条
- 当前快照
- 1 份
- 快照标识符
- @mid5wgtg
- 此快照首次捕获于
- 2025/11/24 21:07 3 个月前
- 此快照最后确认于
- 2025/11/24 21:41 3 个月前
最暴力的代码,:
CPP#include<bits/stdc++.h>
using namespace std;
int n,m,cnt;
int v[40005],lun;
vector<int> eds[40005];
string by[40005],rl[40005];
unordered_map<string,int> p;
bool check(int x,int p){
if(x==p) return 1;
for(int i=0;i<eds[x].size();i++){
int now=eds[x][i];
if(v[now]!=lun){
v[now]=lun;
if(check(now,p)) return 1;
v[now]=0; //注意这一行
}
}
return 0;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
string x,y;
cin>>x>>y;
rl[i]=x,by[i]=y;
if(!p[x]) p[x]=++cnt;
if(!p[y]) p[y]=++cnt;
eds[p[x]].push_back(p[y]);
}
cin>>m;
for(int i=1;i<=m;i++){
string x,y;
cin>>x>>y;
if(!p[x]) p[x]=++cnt;
if(!p[y]) p[y]=++cnt;
eds[p[y]].push_back(p[x]);
}
for(int i=1;i<=n;i++){
int x=p[rl[i]],y=p[by[i]];
lun=v[y]=i;
if(check(y,x)) cout<<"Unsafe\n";
else cout<<"Safe\n";
}
return 0;
}
同学乱改了一行:
CPP#include<bits/stdc++.h>
using namespace std;
int n,m,cnt;
int v[40005],lun;
vector<int> eds[40005];
string by[40005],rl[40005];
unordered_map<string,int> p;
bool check(int x,int p){
if(x==p) return 1;
for(int i=0;i<eds[x].size();i++){
int now=eds[x][i];
if(v[now]!=lun){
v[now]=lun;
if(check(now,p)) return 1;
// v[now]=0;
}
}
return 0;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
string x,y;
cin>>x>>y;
rl[i]=x,by[i]=y;
if(!p[x]) p[x]=++cnt;
if(!p[y]) p[y]=++cnt;
eds[p[x]].push_back(p[y]);
}
cin>>m;
for(int i=1;i<=m;i++){
string x,y;
cin>>x>>y;
if(!p[x]) p[x]=++cnt;
if(!p[y]) p[y]=++cnt;
eds[p[y]].push_back(p[x]);
}
for(int i=1;i<=n;i++){
int x=p[rl[i]],y=p[by[i]];
lun=v[y]=i;
if(check(y,x)) cout<<"Unsafe\n";
else cout<<"Safe\n";
}
return 0;
}
然后就过了(???!!)
一个
v数组的操作不是 的吗,有没有dalao来看看为什么这么离谱。回复
共 2 条回复,欢迎继续交流。
正在加载回复...