UVALive
来源:互联网 发布:i see软件 编辑:程序博客网 时间:2024/06/07 08:01
原题链接:https://cn.vjudge.net/problem/UVALive-4287
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2288
题意:
给出N个命题,要求你证明这N个命题的等价性
比如有4个命题a,b,c,d,我们证明a<->b, b<->c,c<->d,每次证明都是双向的,因此一共用了6次推导
如果换成证明a->b,b->c,c->d,d->a,每次证明都是单向的,而只需4次就可以证明所有命题的等价性
现在给出M个命题证明,问还需要证明几个,才可以保证N个命题等价。
分析:
缩点后求DAG中入度为0和出度为0的联通块的较大值。
此题在刘汝佳《算法竞赛入门经典训练指南》P322
此题和POJ1236差不多,那题题解:http://blog.csdn.net/hurmishine/article/details/75451953
由于粗心找了好久的错误,吧POJ 1236的代码改了数据范围和输入输出,竟然过了!,过了。
遂用在线文本比较工具才找出差异,竟然少了一个等号,等号。。。。
AC代码:
/** * 行有余力,则来刷题! * 博客链接:http://blog.csdn.net/hurmishine * 个人博客网站:http://wuyunfeng.cn/*/#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <stack>using namespace std;const int maxn=50000+5;int n,m;int indexx;int cnt;int low[maxn],dfn[maxn];bool vis[maxn];int in[maxn],out[maxn];int belong[maxn];vector<int>G[maxn];stack<int>s;void Init(){ indexx=cnt=0; //少了个等号............... for(int i=0; i<=n; i++) { G[i].clear(); vis[i]=false; low[i]=dfn[i]=0; }}void Tarjan(int u){ dfn[u]=low[u]=++indexx; vis[u]=true; s.push(u); 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 x; do { x=s.top(); s.pop(); vis[x]=false; belong[x]=cnt; } while(x!=u); }}int main(){ //freopen("C:\\Documents and Settings\\Administrator\\桌面\\data.txt","r",stdin); int T; cin>>T; while(T--) { cin>>n>>m; Init(); int u,v; for(int i=0; i<m; i++) { scanf("%d%d",&u,&v); G[u].push_back(v); } for(int i=1; i<=n; i++) { if(!dfn[i]) Tarjan(i); } for(int i=1; i<=cnt; i++) { in[i]=out[i]=0; } for(int u=1; u<=n; u++) { for(int j=0; j<G[u].size(); j++) { int v=G[u][j]; if(belong[u]!=belong[v]) { in[belong[v]]++; out[belong[u]]++; } } } int t1=0,t2=0; for(int i=1; i<=cnt; i++) { if(!in[i]) t1++; if(!out[i]) t2++; } int ans=max(t1,t2); if(cnt==1) ans=0; cout<<ans<<endl; } return 0;}
AC代码:
阅读全文
0 0
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- 基于PHP+MySQL实现简单留言板
- 06_归并排序
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。
- MySQl 命令大全
- 推荐一款好用解压RAR、ZIP文件Mac软件,可以输入密码Dr. Unarchiver
- UVALive
- 布尔矩阵—C语言
- Java Object的clone方法浅复制和深复制
- Dijkstra算法详解
- oracle11g一分钟学会触发器,建序列
- pop显示以及动画
- 【转】漫谈Moq(四)
- 小程序资源包加载失败
- 基于R的高级数据管理