POJ 2762 Going from u to v or from v to u? 弱连通分量 -
来源:互联网 发布:大魔王女神淘宝 编辑:程序博客网 时间:2024/05/16 18:24
题目地址:http://poj.org/problem?id=2762
弱连通分量就是图中任意两点(u,v) u可以到v或者v可以到u
题目就是问该图是不是弱连通分量
缩点后,形成的树一定是一条链,这样才能有一条通路
那么就是缩点后只有n-1个出入度为1的点,1个入读为0,出度为0的点
我是通过有n-1个出度入读来判断的
AC代码:
#include<iostream>#include<cstdio>#include<vector>#include<stack> #include<cstring>#include<algorithm>using namespace std;const int maxn=1000+10;bool vis[maxn];int ID[maxn]; //点的颜色编号 int index,ncolor; vector<int> dfn(maxn),low(maxn),st;vector<vector<int> > color(maxn); //同一种颜色的点 vector<vector<int> > G(maxn); vector<vector<int> > GT(maxn); void Tarjan(int u){dfn[u]=low[u]=++index;vis[u]=true;st.push_back(u);for(int i=0;i<G[u].size();i++){int v=G[u][i];if(!vis[v]){Tarjan(v);low[u]=min(low[u],low[v]);}else if(find(st.begin(),st.end(),v)!=st.end()) //in stacklow[u]=min(low[u],dfn[v]);}if(dfn[u]==low[u]){int v; ncolor++;do{v=st.back(); st.pop_back();color[ncolor].push_back(v);ID[v]=ncolor;}while(v!=u);}}bool solve(int n){index=ncolor=0; st.clear(); memset(vis,false,sizeof(vis));memset(ID,false,sizeof(ID));for(int i=1;i<=n;i++)if(!vis[i]) Tarjan(i);if(ncolor==1) return true;int out=0; //m标记出度为1的点有几个 vector<int> points;for(int i=1;i<=ncolor;i++) {points.clear();for(int j=0;j<color[i].size()&&points.size()<=1;j++){int u=color[i][j];for(int k=0;k<G[u].size()&&points.size()<=1;k++){int v=G[u][k];if(ID[u]!=ID[v]&&find(points.begin(),points.end(),v)==points.end()) //有出度 points.push_back(v);}}if(points.size()==1) out++;}int in=0; //入读为1的点有几个 for(int i=1;i<=ncolor;i++) {points.clear();for(int j=0;j<color[i].size()&&points.size()<=1;j++){int u=color[i][j];for(int k=0;k<GT[u].size()&&points.size()<=1;k++){int v=GT[u][k];if(ID[u]!=ID[v]&&find(points.begin(),points.end(),v)==points.end()) //有出度 points.push_back(v);}}if(points.size()==1) in++;}if(in==out&&in==ncolor-1) return true; return false;}int main(){int n,m,u,v,T;cin>>T;while(T--){cin>>n>>m;for(int i=1;i<=n;i++) G[i].clear(),GT[i].clear(),color[i].clear();while(m--){scanf("%d%d",&u,&v);G[u].push_back(v);GT[v].push_back(u);}cout<<(solve(n)?"Yes":"No")<<endl;}return 0;}
0 0
- POJ 2762 Going from u to v or from v to u(弱连通分量)
- POJ 2762 Going from u to v or from v to u? 弱连通分量 -
- POJ 2762 Going from u to v or from v to u? / 强连通分量&&拓扑
- poj 2762 Going from u to v or from v to u?(强连通分量)
- Poj 2762 Going from u to v or from v to u? (判断图弱连通)
- POJ 2762 Going from u to v or from v to u?(强连通分量+缩点)
- [POJ 2762]Going from u to v or from v to u? (强连通分量+拓扑排序)
- POJ 2762 Going from u to v or from v to u?(强连通分量+拓扑排序)
- POJ - 2762 Going from u to v or from v to u?(拓扑排序+强连通分量)
- POJ 2762 Going from u to v or from v to u? 强连通分量+DAG最长路
- 【连通图|单连通】POJ-2762 Going from u to v or from v to u?
- poj 2762 Going from u to v or from v to u? 强连通最长链
- POJ 2762 Going from u to v or from v to u? 强连通+判断链
- 【POJ】2762 Going from u to v or from v to u? 强连通+最长路
- POJ 2762 Going from u to v or from v to u?(强连通+拓扑)
- POJ 2762 — Going from u to v or from v to u? 强连通+拓扑
- poj 2762 Going from u to v or from v to u? 单向连通图判定
- poj2762 Going from u to v or from v to u? 强连通分量 + 拓扑排序
- 为什么我要用 Node.js? 案例逐一介绍
- 二叉树的简单实现
- JAVAWeb之JSP学习笔记一
- Light—oj 1045 - Digits of Factorial
- oracle 12c 创建表空间,用户并指定表空间,以及授权
- POJ 2762 Going from u to v or from v to u? 弱连通分量 -
- 使用NFC打开网页
- c++智能指针学习
- OpenGL应用开发----基础数学
- java环境变量设置
- Leetcode-345-Reverse Vowels of a String
- PilotEdit v9.7
- 三角形锥形(1)
- 带你读开源—ASP.NET_MVC(一)