Language: C++

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define sar(a,n) cout<<#a<<":";rep(kbrni,n)cout<<" "<<a[kbrni];cout<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define svecp(v) cout<<#v<<":";each(kbrni,v)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

vector<int> e[11][11];
vector<int> G[11];
bool visit[11];

void dfs(int u,vvi& route,vi& hoge)
{
    visit[u] = true;
    rep(i,len(G[u])){
        if(visit[G[u][i]]){
            vi ag;
            rep(j,len(hoge)){
                if(hoge[j] == G[u][i]){
                    for(;j<len(hoge);j++){
                        ag.pb(hoge[j]);
                    }
                }
            }
            ag.pb(u);
            route.pb(ag);
        }else{
            hoge.pb(u);
            dfs(G[u][i],route,hoge);
            hoge.pop_back();
        }
    }
}

int main()
{
    // cin.tie(0);
    // ios::sync_with_stdio(false);ccccc
    while(1){
        int n;
        cin >> n;
        if(n == 0){
            break;
        }
        string s;
        cin >> s;
        int kp = 0;
        bool end = false;
        rep(i,10){
            rep(j,10){
                e[i][j].clear();
            }
        }
        rep(i,10){
            G[i].clear();
            visit[i] = false;
        }
        rep(i,len(s)){
            if(s[i] == 'u'){
                kp = 0;
            }else if(kp & (1 << (int)(s[i]-'0'))){
                cout << "UNSAFE\n";
                end = true;
                break;
            }else{
                kp |= (1 << (int)(s[i]-'0'));
            }
        }
        if(end){
            continue;
        }
        kp = 0;
        rep(i,len(s)-1){
            if(s[i] != 'u'){
                if(s[i+1] != 'u'){
                    int a = (int)(s[i]-'0'),b = (int)(s[i+1]-'0');
                    e[a][b].pb(kp);
                    kp |= (1 << (int)(s[i]-'0'));
                }
            }else{
                kp = 0;
            }
        }
        rep(i,10){
            rep(j,10){
                if(len(e[i][j])){
                    G[i].pb(j);
                }
            }
        }
        rep(i,10){
            if(!visit[i]){
                vvi route;
                vi hoge;
                dfs(i,route,hoge);
                // rep(i,len(route)){
                //     svec(route[i]);
                // }
                rep(j,len(route)){
                    int loop = len(route[j]);
                    vvi vec(loop);
                    rep(k,len(route[j])){
                        int a = route[j][k],b = route[j][(k+1)%loop];
                        vec[k] = e[a][b];
                    }
                    set<int> dp[11];
                    dp[0].insert(0);
                    rep(j,loop){
                        each(k,dp[j]){
                            rep(l,len(vec[j])){
                                if(!(k & vec[j][l])){
                                    dp[j+1].insert(k|vec[j][l]);
                                }
                            }
                        }
                    }
                    if(len(dp[loop])){
                        cout << "UNSAFE\n";
                        end = true;
                        break;
                    }
                }
                if(end){
                    break;
                }
            }
        }
        if(!end){
            cout << "SAFE\n";
        }
    }
    return 0;
}



Compiler output:
a.cc: In function 'void dfs(int, vvi&, vi&)':
a.cc:46:5: error: reference to 'visit' is ambiguous
   46 |     visit[u] = true;
      |     ^~~~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:80,
                 from a.cc:1:
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:42:6: note:                 'bool visit [11]'
   42 | bool visit[11];
      |      ^~~~~
a.cc:48:12: error: reference to 'visit' is ambiguous
   48 |         if(visit[G[u][i]]){
      |            ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:42:6: note:                 'bool visit [11]'
   42 | bool visit[11];
      |      ^~~~~
a.cc: In function 'int main()':
a.cc:88:13: error: reference to 'visit' is ambiguous
   88 |             visit[i] = false;
      |             ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:42:6: note:                 'bool visit [11]'
   42 | bool visit[11];
      |      ^~~~~
a.cc:124:17: error: reference to 'visit' is ambiguous
  124 |             if(!visit[i]){
      |                 ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:42:6: note:                 'bool visit [11]'
   42 | bool visit[11];
      |      ^~~~~