이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define pb push_back
#define spc << " " <<
#define endl "\n"
#define all(x) x.begin(), x.end()
#define int long long
#define ii pair<int, int>
#define vi vector<int>
#define vii vector<ii>
#define st first
#define nd second
#define mid (l+r)/2
#define inf 1e15
#define MOD 998244353
#define MX 1005
using namespace std;
vi edges[MX];
bitset<MX> adj[MX];
int vis[MX];
int tim=0, last=-1;
int dfs(int node, int par){
//cerr << node+1 spc par+1 << endl;
vis[node]=++tim;
int can=-1;
for(auto i:edges[node]){
if(i==par) continue;
if(vis[i]>0){
auto dont=adj[node];
dont&=adj[par];
if(node!=0 && dont[i]==1) continue;
if(can==-1 || vis[i]>vis[can]) can=i;
}
}
if(can!=-1){
cout << node+1 << " ";
last=node;
return can;
}
for(auto i:edges[node]){
if(i==par) continue;
if(vis[i]!=0) continue;
int res=dfs(i, node);
if(res==-2) return -2;
if(res!=-1){
if(node==res){
cout << node+1 << " ";
return -2;
}
auto dont=adj[node];
dont&=adj[last];
if(dont[par]==0){
cout << node+1 << " ";
last=node;
}
return res;
}
}
vis[node]=-1;
return -1;
}
void solve(){
int n,m; cin >> n >> m;
for(int i=0; i<n; i++){
adj[MX].reset();
vis[i]=0;
}
for(int i=1; i<=m; i++){
int a,b; cin >> a >> b; a--, b--;
edges[a].pb(b);
edges[b].pb(a);
adj[a].set(b);
adj[b].set(a);
}
if(dfs(0, 0)==-1) cout << "no" << endl;
}
signed main(){
ios_base::sync_with_stdio(false);cin.tie(0);
#ifdef Local
freopen("in","r",stdin);
freopen("out","w",stdout);
/*#else
freopen("","r",stdin);
freopen("","w",stdout);*/
#endif
int t=1;
//cin >> t;
while(t--) solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |