LightOj 1251 Forming the Council(2-sat)
来源:互联网 发布:手机文字编辑软件 编辑:程序博客网 时间:2024/05/18 13:31
原题地址:点击打开链接
#include<stdio.h>#include<vector>#include<queue>#include<string.h>#include<stack>using namespace std;int m;int time;int low[20000];int dfn[20000];int cmp[20000]; int in[20000];int color[20000];int fp[20000];bool instack[20000];int count;int k;vector<int>edge[20000];vector<int>g[20000];void add_edge(int u,int v){int x=u,y=v;if(u<0) x=-u+m;if(v<0)y=-v+m;if(x>m) edge[x-m].push_back(y);else edge[x+m].push_back(y);if(y>m) edge[y-m].push_back(x);else edge[y+m].push_back(x);}stack<int>S;void tarjan(int u) {dfn[u]=low[u]=++time;S.push(u);instack[u]=true;for(int i=0;i<edge[u].size();i++){int v=edge[u][i];if(dfn[v]==-1){tarjan(v);low[u]=min(low[u],low[v]);}else if(instack[v]){low[u]=min(dfn[v],low[u]);}}if(dfn[u]==low[u]){count++;int a;do{a=S.top();S.pop();cmp[a]=count;instack[a]=false;}while(a!=u);}}void suodian() //建新图 {memset(in,0,sizeof(in));for(int i=1;i<=count;i++)g[i].clear();for(int i=1;i<=2*m;i++){for(int j=0;j<edge[i].size();j++){int u=edge[i][j];if(cmp[i]!=cmp[u]) //反向建图 {g[cmp[u]].push_back(cmp[i]);in[cmp[i]]++;}}}} void tpsort() { memset(color,-1,sizeof(color)); int i,u; queue<int>que; for(i=1;i<=count;i++) { if(in[i]==0) que.push(i); //将入度为0的顶点加入队列 } while(!que.empty()) { u=que.front(); que.pop() ; if(color[u]==-1) { color[u]=1; color[fp[u]]=0;} for(i=0;i<g[u].size();i++) { int v=g[u][i]; if(--in[v]==0) que.push(v); } } } void solve(){int res=0,i;count=0;time=0;memset(dfn,-1,sizeof(dfn));memset(cmp,-1,sizeof(cmp));memset(instack,false,sizeof(instack));for(i=1;i<=2*m;i++){if(dfn[i]==-1)tarjan(i);}printf("Case %d: ",++k);for(i=1;i<=m;i++){if(cmp[i]==cmp[i+m]){printf("No\n");return ;}else{fp[cmp[i]]=cmp[i+m];fp[cmp[i+m]]=cmp[i];}}suodian();tpsort();for(i=1;i<=m;i++){if(color[cmp[i]]==1)res++;}printf("Yes\n%d",res);for(i=1;i<=m;i++){if(color[cmp[i]]==1)printf(" %d",i);}printf("\n");}int main() {int i,t,n,u,v;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);for(i=1;i<=2*m;i++)edge[i].clear();for(i=0;i<n;i++){scanf("%d%d",&u,&v);add_edge(u,v);}solve();}return 0;}
0 0
- lightOJ 1251 - Forming the Council(2-sat)
- LightOj 1251 Forming the Council(2-sat)
- LightOJ - 1251 Forming the Council(2-SAT)
- 【Lightoj 1251 Forming the Council】& Tarjan & 2
- LightOJ 1251 - Forming the Council 2-sat输出任意一组解
- LightOJ 1251 Forming the Council【2-Sat+逆向拓扑排序输出可行解】好题!
- Light OJ 1251 Forming the Council 2-SAT输出任意一组解
- Light OJ 1251 - Forming the Council (2-SAT模板题)
- light oj 1251 - Forming the Council (2-SAT + 输出任意解)
- Light oj 1251 - Forming the Council 【2-sat】【判断是否存在可行解 + 反向拓扑输出可行解】
- Pairs Forming LCM LightOJ
- Pairs Forming LCM LightOJ
- lightOJ 1236 Pairs Forming LCM
- LightOJ 1236 Pairs Forming LCM
- LightOJ 1236Pairs Forming LCM
- lightoj 1236 - Pairs Forming LCM
- LightOJ 1236 Pairs Forming LCM
- LightOJ 1407 - Explosion(2sat+枚举)
- SteamVR插件详解:SteamVR_Controller脚本分析+Vive控制器功能开发
- Javascript入门学习
- MicroService 微服务架构模式简述
- 细说Spring
- 几个linux下的命令
- LightOj 1251 Forming the Council(2-sat)
- SSH框架详解
- Javascript函数的简单学习
- 构造方法定义
- linux基础命令
- Spring提供的后容器处理器
- 【类反射】(模拟Java内省的功能)
- 初探nginx架构(100%)
- Struts2常量的具体用法实例