luogu1262 间谍网络(tarjan缩点)
来源:互联网 发布:粉红女郎 知乎 编辑:程序博客网 时间:2024/05/26 19:14
tarjan缩点,就是用来把一个有向带环图变成DAG即 Directed Acyclic Graph有向无环图的算法,复杂度为O(E+V)。dfn[i]是dfs序,low[i]是i及i的子树中最小的dfn。belong[x]是点x在新图中所属的点。
#include <bits/stdc++.h>using namespace std;#define ll long long#define inf 0x3f3f3f3f#define N 3010inline int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x*f;}struct edge{ int to,next;}data[8010];int n,m,w[N],h[N],num=0,ans=0,tot=0,sw[N];int dfn[N],low[N],num1=0,belong[N],rd[N];stack<int>q;bool vis[N],inq[N];void dfs(int x){ if(vis[x]) return; vis[x]=1;tot++; for(int i=h[x];i;i=data[i].next) dfs(data[i].to);}void tarjan(int x){ dfn[x]=low[x]=++num1; q.push(x);inq[x]=1; for(int i=h[x];i;i=data[i].next){ int y=data[i].to; if(!dfn[y]){ tarjan(y); low[x]=min(low[x],low[y]); } else if(inq[y]) low[x]=min(low[x],dfn[y]); } if(dfn[x]==low[x]){ sw[x]=inf; while(1){ int y=q.top();q.pop();inq[y]=0; belong[y]=x;sw[x]=min(sw[x],w[y]); if(y==x) break; } }}int main(){// freopen("a.in","r",stdin); n=read();m=read();memset(w,0x3f,sizeof(w)); while(m--){ int x=read(),y=read();w[x]=y; }m=read(); while(m--){ int x=read(),y=read(); data[++num].to=y;data[num].next=h[x];h[x]=num; } for(int i=1;i<=n;++i) if(w[i]!=inf) dfs(i);//是否能遍历所有点 if(tot<n) for(int i=1;i<=n;++i) if(!vis[i]){printf("NO\n%d",i);return 0;} for(int i=1;i<=n;++i) if(w[i]!=inf&&!dfn[i]) tarjan(i); for(int x=1;x<=n;++x){ for(int i=h[x];i;i=data[i].next){ int y=data[i].to; if(belong[x]!=belong[y]) rd[belong[y]]++; } } for(int i=1;i<=n;++i)//没有入度,也就是说没有别的点可以到这里,只能收买。 if(belong[i]==i&&!rd[i]) ans+=sw[i]; printf("YES\n%d",ans); return 0;}
阅读全文
0 0
- luogu1262 间谍网络(tarjan缩点)
- 洛谷1262间谍网络(tarjan缩点)
- 洛谷1262 间谍网络 tarjan缩点
- tarjan缩点以及链式前向星的基本+应用(洛谷1262 间谍网络)
- tarjan题目(3)间谍网络
- 间谍网络--tarjan 解题报告
- 【codevs4093】EZ的间谍网络 tarjan
- 洛谷 1262 间谍网络 Tarjan 图论
- tarjan——洛谷P1262 间谍网络
- tarjan强连通分量 洛谷P1262 间谍网络
- POJ2186(Tarjan缩点)
- 间谍网络
- 间谍网络
- NYOJ-120校园网络【强连通分量缩点&&tarjan】
- 【基础练习】【强连通tarjan】codevs4093 EZ的间谍网络题解
- poj2186 Popular Cows(tarjan + 缩点)
- poj Popular Cows(tarjan +缩点)
- POJ2186--Popular Cows(Tarjan+缩点)
- 项目1-C++语言中函数参数传递的三种方式
- 算法提高 ADV-135 三角形面积
- IE8、7、6动态添加样式时,CSS hack的BUG
- MySQL单表查询多表查询
- 对于loader constraint violation问题的解决方案
- luogu1262 间谍网络(tarjan缩点)
- Pandas 使用教程 3
- spring boot velocity自定义指令 java
- linux下mysql安装 主从复制和读写分离配置
- repo init报错Cannot get https://gerrit.googlesource.com/git-repo/clone.bundle
- hehheda
- 数据结构第二周项目2--程序的多文件组织
- C++下Socket编程
- 苹果2017秋季大会回顾