QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#773#511364#8029. Traveling MerchantStayAloneShunpowerFailed.2024-08-10 11:41:412024-08-10 11:41:42

Details

Extra Test:

Validator Runtime Error

input:

1
3 7
LLL
1 1
2 2
3 1
3 3
3 3
1 2
3 1

output:


result:


IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#511364#8029. Traveling MerchantShunpowerAC ✓218ms104800kbC++143.6kb2024-08-09 19:44:452024-08-09 19:44:45

answer

//Author:KIT / Shunpower
//May the force be with you and me.
#include <bits/stdc++.h>
#define ET return 0
#define fi first
#define se second
#define mp make_pair
#define pb emplace_back
#define ll long long
#define ull unsigned long long
#define inf INT_MAX
#define uinf INT_MIN
#define pii pair<int,int>
#define pll pair<ll,ll>
#define debug puts("--------Chery AK IOI--------");
#define fr1(i,a,b) for(int i=a;i<=b;i++)
#define fr2(i,a,b) for(int i=a;i>=b;i--)
#define fv(i,p) for(int i=0;i<p.size();i++)
#define ld long double
#define il inline
#define ptc putchar
//Quickly power: ll d=qpow(b,p>>1,k);
//Segment Tree: Memory Limit Excceed
//Segment Tree: Modify()->Pushdown()
//Mod: +M, %M, define int ll
//Mod: Don't use 998244353 instead of 1e9+7 and so on
//Don't solve a problem for too long time.
using namespace std;
const int N=2e5+10;
int tc;
int n,m;
char s[N];
vector <pii> spc;
vector <int> p[N];
int tot,cnt;
int rt;
vector <int> vdcc[N];
int dfn[N],low[N];
stack <int> st;
vector <int> rst[N<<1];//Round-Square Tree
bool key[N];
int bel[N];
void dfs(int x){
    tot++;
    int son=0;
    st.push(x);
    dfn[x]=low[x]=tot;
    for(auto y:p[x]){
        if(!dfn[y]){
            son++;
            dfs(y);
            low[x]=min(low[x],low[y]);
            if(low[y]>=dfn[x]){
                cnt++;
                while(st.top()!=y){
                    bel[st.top()]=cnt;
                    vdcc[cnt].pb(st.top());
                    st.pop();
                }
                bel[y]=cnt;
                vdcc[cnt].pb(y);
                st.pop();
                vdcc[cnt].pb(x);
                bel[x]=cnt;
                key[x]|=(x!=rt);
            }
        }
        else low[x]=min(low[x],dfn[y]);
    }
    if(x==rt&&son>=2) key[x]=1; 
    if(x==rt&&!son){
        cnt++;
        vdcc[cnt].pb(x);
        bel[x]=cnt;
    }
}
int f[N<<1][20];
int dep[N<<1];
void walk(int x,int fa){
    f[x][0]=fa;
    dep[x]=dep[fa]+1;
    for(auto y:rst[x]){
        if(y==fa) continue;
        walk(y,x);
    }
}
int LCA(int x,int y){
    if(dep[x]<dep[y]) swap(x,y);
    int k=dep[x]-dep[y];
    fr2(i,19,0){
        if(k>=(1<<i)) k-=(1<<i),x=f[x][i];
    }
    if(x==y) return x;
    fr2(i,19,0){
        if(f[x][i]!=f[y][i]) x=f[x][i],y=f[y][i];
    }
    return f[x][0];
}
void clear(){
    fr1(i,1,n+cnt) fr1(j,0,19) f[i][j]=0;
    fr1(i,1,n+cnt) rst[i].clear();
    while(!st.empty()) st.pop();
    fr1(i,1,n+cnt) key[i]=bel[i]=dfn[i]=low[i]=0;
    fr1(i,1,cnt) vdcc[i].clear();
    cnt=tot=0;
    spc.clear();
    fr1(i,1,n) p[i].clear();
}
void solve(){
    clear();
    cin>>n>>m;
    fr1(i,1,n) cin>>s[i];
    fr1(i,1,m){
        int u,v;
        cin>>u>>v;
        u++,v++;
        if(s[u]==s[v]) spc.pb(mp(u,v));
        else p[u].pb(v),p[v].pb(u);
    }
    rt=1;
    dfs(1);
    fr1(i,1,cnt){
        for(auto j:vdcc[i]){
            if(key[j]) rst[i+n].pb(j),rst[j].pb(i+n);
        }
    }
    if(key[1]) walk(1,1);
    else walk(bel[1]+n,bel[1]+n);
    fr1(j,1,19) fr1(i,1,n+cnt) f[i][j]=f[f[i][j-1]][j-1];
    for(auto i:spc){
        int u=i.fi,v=i.se;
        if(!dfn[u]||!dfn[v]) continue;
        if(key[u]) u=f[u][0];
        else u=bel[u]+n;
        if(key[v]) v=f[v][0];
        else v=bel[v]+n;
        int lca=LCA(u,v);
        if(lca==u||lca==v){
            cout<<"yes\n";
            return;
        }
    }
    cout<<"no\n";
}
// #define Ltp cute
int main(){
#ifdef Ltp
	freopen("travel.in","r",stdin);
	freopen("travel.out","w",stdout);
#endif
    cin>>tc;
    while(tc--) solve();
	ET;
}
//ALL FOR Zhang Junhao.