QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#738649 | #8029. Traveling Merchant | SimonLJK | WA | 22ms | 22996kb | C++17 | 1.9kb | 2024-11-12 19:37:49 | 2024-11-12 19:37:54 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=4e5+999,L=19;
int n,m,cnt;
string str;
vector<int> to[N];
struct edge{
int v,nxt;
}edg[N*2];
int cntedge,hd[N];
void add(int u,int v){
edg[++cntedge]=(edge){v,hd[u]};
hd[u]=cntedge;
}
vector< pair<int,int> > e;
stack<int> sta;
int dfn[N],low[N],st[N],ed[N],timer,f[N][L];
void dfs(int u){
int vv;
dfn[u]=low[u]=++timer;
sta.push(u);
for(int v:to[u]){
if(dfn[v]) low[u]=min(low[u],dfn[v]);
else{
dfs(v); low[u]=min(low[u],low[v]);
if(low[v]>=dfn[u]){
cnt++;
do{
vv=sta.top(); sta.pop();
add(vv,cnt); add(cnt,vv);
}while(vv!=u);
sta.push(u);
}
}
}
return;
}
void dfs(int u,int fa){
st[u]=++timer;
f[u][0]=fa;
for(int i=1;i<L;i++)
f[u][i]=f[f[u][i-1]][i-1];
int v;
for(int i=hd[u];i;i=edg[i].nxt){
v=edg[i].v;
if(v==fa) continue;
dfs(v,u);
}
ed[u]=++timer;
return;
}
bool judge(int u,int v){
if(!u) return true;
return st[u]<=st[v]&&ed[v]<=ed[u];
}
int lca(int u,int v){
if(judge(u,v)) return u;
if(judge(v,u)) return v;
for(int i=L-1;i>=0;i--)
if(!judge(f[u][i],v))
u=f[u][i];
return f[u][0];
}
void clr(){
e.clear();
for(int i=1;i<=n;i++){
dfn[i]=low[i]=0;
to[i].clear();
}
for(int i=1;i<=cnt;i++)
st[i]=ed[i]=hd[i]=0;
cntedge=timer=0;
while(!sta.empty()) sta.pop();
}
void solve(){
cin>>n>>m>>str;
str=" "+str;
int u,v,lc;
for(int i=1;i<=m;i++){
cin>>u>>v; u++; v++;
if(str[u]==str[v]) e.push_back(make_pair(u,v));
else to[u].push_back(v),to[v].push_back(u);
}
cnt=n;
dfs(1);
timer=0;
dfs(1,0);
bool ans=0;
for(pair<int,int> now:e){
u=now.first; v=now.second;
lc=lca(u,v);
if(lc==u||lc==v||lc>n) ans=1;
}
clr();
if(ans) cout<<"yes"<<'\n';
else cout<<"no"<<'\n';
return;
}
int main(){
std::ios::sync_with_stdio(false);
cin.tie(0);
int T; cin>>T;
while(T--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 22996kb
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: 22ms
memory: 19628kb
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 yes yes no yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes no yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes yes ye...
result:
wrong answer 3rd lines differ - expected: 'no', found: 'yes'