JasonZRY @ 2019-12-06 21:00:40
2-SAT问题模板,请问各位大佬哪错了
#include<bits/stdc++.h>
using namespace std;
vector<int>g[2000005];
stack<int>s;
int t,n,m,ind,cnt,dfn[2000005],low[2000005],scc[2000005];
char x[2],y[2];
bool f,vis[2000005];
void clean(){
for(int i=1;i<=n;i++)g[i].clear();
while(!s.empty())s.pop();
ind=cnt=f=0;
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(scc,0,sizeof(scc));
memset(vis,0,sizeof(vis));
}
void tarjan(int u){
dfn[u]=low[u]=++ind;
s.push(u);
vis[u]=1;
for(int i=0;i<g[u].size();i++){
int v=g[u][i];
if(!dfn[v])tarjan(v),low[u]=min(low[u],low[v]);
else if(vis[v])low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u]){
cnt++;
int v;
do{
v=s.top();
s.pop();
scc[v]=cnt;
vis[v]=0;
}while(u!=v);
}
}
int main(){
scanf("%d",&t);
while(t--){
clean();
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
scanf("%s %s",x,y);
if(x[0]=='m'&&y[0]=='m')g[x[1]-'0'+n].push_back(y[1]-'0'),g[y[1]-'0'+n].push_back(x[1]-'0');
if(x[0]=='m'&&y[0]=='h')g[x[1]-'0'+n].push_back(y[1]-'0'+n),g[y[1]-'0'].push_back(x[1]-'0');
if(x[0]=='h'&&y[0]=='m')g[x[1]-'0'].push_back(y[1]-'0'),g[y[1]-'0'+n].push_back(x[1]-'0'+n);
if(x[0]=='h'&&y[0]=='h')g[x[1]-'0'].push_back(y[1]-'0'+n),g[y[1]-'0'].push_back(x[1]-'0'+n);
}
for(int i=1;i<=2*n;i++)if(!dfn[i])tarjan(i);
for(int i=1;i<=n;i++){
if(scc[i]==scc[i+n]){
f=1;
break;
}
}
if(!f)printf("GOOD\n");
else printf("BAD\n");
}
}
by 2017gdgzoi999 @ 2019-12-07 08:21:48
@JasonZRY 材料最多有100种,不一定是一位数,但是程序只处理了一位,导致出错,如m14
这样的会被当成m1
。
by JasonZRY @ 2019-12-07 08:37:18
@2017gdgzoi999
我现在改成前向星过了
by JasonZRY @ 2019-12-07 08:40:17
@2017gdgzoi999
刚刚发现清vector的时候只清到n,改成n*2也过了
by JasonZRY @ 2019-12-07 08:40:41
@2017gdgzoi999
我已经把读入改了