Untitled

 avatar
unknown
c_cpp
6 months ago
1.7 kB
7
Indexable
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define ff first
#define ss second
#define vll vector<ll>
#define vvll vector<vll>

void solve(){
    ll n;cin>>n;
    // in and out
    vector<ll> in(n+1),out(n+1);
    for(int i=1;i<=2*n;i++){
        char c;
        int x;
        cin>>c>>x;
        if(c=='+') in[x]=i;
        else out[x]=i;
    }
    // people -> people 
    vector<vector<ll>> g(n+1);

    auto f=[&](ll x,ll y){
        if(in[x]<in[y] && in[y]<out[x] && out[x]<out[y]) return true;
        if(in[y]<in[x] && in[x]<out[y] && out[y]<out[x]) return true;
        return false;
    };

    for(int i=1;i<=n;i++){
        for(int j=i+1;j<=n;j++){
            if(f(i,j)){
                g[i].push_back(j);
                g[j].push_back(i);
            }
        }
    }
    // we formed the graph -> now we have to check is it bipartite?
    vector<bool> vis(n+1);
    vector<ll> color(n+1);
    // 1,2  [1->2] and [2->1]
    function<bool(ll,ll)> dfs=[&](ll node,ll col){
        vis[node]=true;
        color[node]=col;
        for(auto &it:g[node]){
            if(!vis[it]){
                bool poss=dfs(it,3-col);
                if(!poss) return false;
            }
            else if(color[it]==col) return false;
        }
        return true;
    };

    for(int i=1;i<=n;i++){
        if(vis[i]) continue;
        bool ret=dfs(i,1);
        if(!ret){
            cout<<"*\n";
            return;
        }
    }
    for(int i=1;i<=n;i++){
        color[i]==1 ? cout<<"G":cout<<"S";
    }
    cout<<'\n';
    

}

int32_t main()
{
    int t = 1;
    //cin>>t;
    while (t--)
    {
        solve();
    }
    return 0;
}
Editor is loading...
Leave a Comment