D37 2-SAT P3007 [USACO11JAN] The Continental Cowngress G
视频链接:D37 2-SAT P3007 [USACO11JAN] The Continental Cowngress G_哔哩哔哩_bilibili
P3007 [USACO11JAN] The Continental Cowngress G - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
// O(n*n) #include <iostream> #include <cstring> #include <algorithm> using namespace std; const int N=2005; int n,m; char s1[10],s2[10]; int dfn[N],low[N],stk[N],scc[N],tim,top,cnt; int head[N],idx,vis[N]; struct Edge{int to,ne;}e[8005]; void add(int x,int y){ e[++idx].to=y;e[idx].ne=head[x];head[x]=idx; } void tarjan(int x){ dfn[x]=low[x]=++tim; stk[++top]=x; for(int i=head[x];i;i=e[i].ne){ int y=e[i].to; if(!dfn[y]){ //若y尚未访问 tarjan(y); low[x]=min(low[x],low[y]); } else if(!scc[y]) //若y已访问且未处理 low[x]=min(low[x],dfn[y]); } if(low[x]==dfn[x]){ //若x是SCC的根 ++cnt; for(int y=-1;y!=x;) scc[y=stk[top--]]=cnt; } } void dfs(int x){ vis[x]=1; for(int i=head[x];i;i=e[i].ne) if(!vis[e[i].to]) dfs(e[i].to); } int check(int x){ memset(vis,0,sizeof(vis)); dfs(x); //找出以x为起点的链上的点 // 如果能从x走到x+n或x-n,则返回 true if(vis[x]&&vis[x<=n?x+n:x-n]) return 1; return 0; } int main(){ scanf("%d %d",&n,&m); for(int i,j,a,b;m--;){ scanf("%d %s %d %s",&i,s1,&j,s2); a=(s1[0]=='Y'); //N:0,i和Y:1,i+n b=(s2[0]=='Y'); add(i+!a*n,j+b*n), add(j+!b*n,i+a*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]){ puts("IMPOSSIBLE"); return 0; } for(int i=1,x,y;i<=n;++i){ x=check(i); //若i→i+n,则取Y y=check(i+n); //若i+n→i,则取N if(x&&!y) putchar('Y'); else if(!x&&y) putchar('N'); else if(!x&&!y) putchar('?'); } }
分类:
D 图论
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2022-08-04 C08【模板】可持久化线段树(主席树)