QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#738826#8029. Traveling MerchantSimonLJKWA 17ms20652kbC++172.1kb2024-11-12 20:02:452024-11-12 20:02:49

Judging History

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

  • [2024-11-12 20:02:49]
  • 评测
  • 测评结果:WA
  • 用时:17ms
  • 内存:20652kb
  • [2024-11-12 20:02:45]
  • 提交

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!=v);
				add(u,cnt); add(cnt,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();
}
int cntt;
void solve(){
	cntt++;
	cin>>n>>m>>str;
	if(cntt==9990) cout<<n<<" "<<m<<endl<<str<<endl;
	str=" "+str;
	int u,v,lc;
	for(int i=1;i<=m;i++){
		cin>>u>>v; u++; v++;
		if(cntt==9990) cout<<u<<" "<<v<<endl; 
		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;
		if(!dfn[u]||!dfn[v]) continue;
		lc=lca(u,v);
		if(lc==u||lc==v||lc>n) ans=1;
	}
	clr();
	if(cntt>2) return;
	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: 20652kb

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: 17ms
memory: 20256kb

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
10 10
LHLHHLLLHH
4 6
7 3
1 2
6 5
5 3
1 4
6 10
1 5
7 9
7 4

result:

wrong answer 3rd lines differ - expected: 'no', found: '10 10'