QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#508215#8029. Traveling MerchantShunpowerWA 24ms13836kbC++143.9kb2024-08-07 09:47:072024-08-07 09:47:07

Judging History

你现在查看的是最新测评结果

  • [2024-08-07 20:25:21]
  • hack成功,自动添加数据
  • (/hack/771)
  • [2024-08-07 09:47:07]
  • 评测
  • 测评结果:WA
  • 用时:24ms
  • 内存:13836kb
  • [2024-08-07 09:47:07]
  • 提交

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;
/*
首先 容易得出充分必要条件:
我们把LH看成两种颜色
那就是找一个奇环,考虑奇环上的同色边(u,v),你需要先1->u,再u->v即可

那么你考虑去掉同色边做一个导出子图
那么你枚举所有同色边 考虑是否存在一条简单路径1->u->v
我们考虑做一个DFS树
你考虑你先沿着树边1->u抵达u
那么你想回到v但是不能走LCA->u的树边了
那么只需要u到LCA没有割边即可
同理或者v到LCA没有割边就行
可以感受一下另一个点是不是在路径上 发现这样是对的
*/
const int N=2e5+10;
int tc;
int n,m;
char s[N];
int head[N],to[N],nxt[N];
int _;
void add(int u,int v){
    _++;
    nxt[_]=head[u];
    head[u]=_;
    to[_]=v;
    // cout<<"!"<<u<<" "<<v<<endl;
    // cout<<nxt[_]<<endl;
}
vector <pii> spc;
vector <int> t[N];
int tot,tpt;
bool vis[N];
int dfn[N],low[N];
int bel[N];
bool bridge[N];
int dep[N],f[N][19];
void dfs(int x,int e){
    // cout<<x<<" "<<e<<endl;
    tot++;
    dfn[x]=low[x]=tot;
    for(int i=head[x];~i;i=nxt[i]){
        if((i^1)==e) continue;
        int y=to[i];
        // cout<<i<<" "<<y<<"?"<<endl;
        if(!dfn[y]){
            t[x].pb(y);
            dfs(y,i);
            low[x]=min(low[x],low[y]);
            if(low[x]==dfn[x]) bridge[i]=bridge[i^1]=1;
        }
        else low[x]=min(low[x],low[y]);
    }
}
void cut(int x,int c){
    bel[x]=c;
    vis[x]=1;
    for(int i=head[x];~i;i=nxt[i]){
        int y=to[i];
        if(vis[y]||bridge[i]) continue;
        cut(y,c);
    }
}
void init(int x,int fa){
    f[x][0]=fa;
    dep[x]=dep[fa]+1;
    for(auto y:t[x]) init(y,x);
}
int LCA(int x,int y){
    if(dep[x]<dep[y]) swap(x,y);
    int k=dep[x]-dep[y];
    fr2(i,18,0){
        if(k>=(1<<i)) k-=(1<<i),x=f[x][i];
    }
    if(x==y) return x;
    fr2(i,18,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) dfn[i]=0;
    fr1(i,1,n) vis[i]=0;
    fr1(i,1,_) bridge[i]=nxt[i]=0;
    fr1(i,1,n) head[i]=-1;
    fr1(i,1,n) t[i].clear();
    _=1;
    tot=tpt=0;
    spc.clear();
}
void solve(){
    cin>>n>>m;
    clear();
    fr1(i,1,n) cin>>s[i];
    fr1(i,1,m){
        int u,v;
        cin>>u>>v;
        u++,v++;
        if(s[u]!=s[v]) add(u,v),add(v,u);
        else spc.pb(mp(u,v));
    }
    dfs(1,0);
    fr1(i,1,n){
        if(dfn[i]&&!vis[i]){
            tpt++;
            cut(i,tpt);
        }
    }
    init(1,1);
    fr1(j,1,18){
        fr1(i,1,n) 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;
        int lca=LCA(u,v);
        // cout<<u<<" "<<v<<" "<<lca<<endl;
        // cout<<tpt<<endl;
        if(bel[lca]==bel[u]||bel[lca]==bel[v]) return cout<<"yes\n",void();
    }
    cout<<"no\n";
}
int main(){
#ifdef Ltp
	freopen("hack.txt","r",stdin);
	freopen("out.txt","w",stdout);
#endif
    ios::sync_with_stdio(false);
    cin>>tc;
    while(tc--) solve();
	ET;
}
//ALL FOR Zhang Junhao.

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 13836kb

input:

2
4 4
LHLH
0 1
1 2
1 3
2 3
3 3
LHH
0 1
0 2
1 2

output:

yes
no

result:

ok 2 lines

Test #2:

score: -100
Wrong Answer
time: 24ms
memory: 13836kb

input:

12385
9 29
LHLLHLHLH
0 7
1 4
4 6
2 0
4 2
6 5
8 4
7 1
2 6
7 3
7 2
8 7
1 3
5 3
0 8
4 0
0 5
8 1
8 6
3 2
0 1
1 2
6 3
2 5
6 0
3 0
5 4
4 3
7 5
7 15
LLLLLHL
5 2
6 3
3 0
0 6
4 5
1 4
6 1
0 5
3 4
5 6
1 0
2 6
0 2
4 2
0 4
6 6
LHHHHH
5 0
0 4
2 5
1 4
1 3
3 4
9 8
LHLHLLHLL
5 7
5 4
7 4
7 8
1 5
0 1
1 8
1 2
5 4
LHHHH...

output:

yes
yes
no
no
no
yes
yes
yes
no
yes
yes
no
yes
yes
yes
yes
yes
yes
yes
yes
no
no
no
yes
no
no
yes
yes
no
no
yes
no
no
yes
yes
yes
yes
yes
yes
no
no
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
yes
no
yes
yes
yes
yes
yes
yes
no
yes
yes
no
yes
yes
yes
yes
yes
yes
no
no
no
yes
yes
yes
no
yes
yes...

result:

wrong answer 280th lines differ - expected: 'no', found: 'yes'