poj--1470--tarjan算法
来源:互联网 发布:常州小河淘宝客服招聘 编辑:程序博客网 时间:2024/05/17 07:21
模板留着
#include<iostream>#include<cstring>#include<cstdio>#include<vector>#define maxn 10020using namespace std;vector<int> g[maxn]; //treevector<int> q[maxn]; //queryint n,m;int x,y;bool vis[maxn];int root;int f[maxn];int ant[maxn];int ans[maxn];bool init(){ char c; int nc; if(scanf("%d",&n)!=EOF) { for(int i=1;i<=n;i++) { g[i].clear(); q[i].clear(); } memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++) { scanf("%d",&x); while(c=getchar()!='('); scanf("%d",&nc); while(c=getchar()!=')'); for(int i=0;i<nc;i++) { scanf("%d",&y); g[x].push_back(y); vis[y]=1; } } scanf("%d",&m); for(int i=0;i<m;i++) { scanf(" (%d%d) ",&x,&y); q[x].push_back(y); q[y].push_back(x); } for(int i=1;i<=n;i++) { if(!vis[i]) { root=i; break; } } memset(vis,0,sizeof(vis)); return true; } return false;}int find(int u){return f[u]==u?u:f[u]=find(f[u]);}void tarjan(int u){ int v; f[u]=u; ant[u]=u; for(int i=0;i<g[u].size();i++) { v=g[u][i]; tarjan(v); f[v]=u; ant[find(u)]=u; } vis[u]=1; for(int i=0;i<q[u].size();i++) { v=q[u][i]; if(vis[v]) { ans[ant[find(v)]]++; } }}void solve(){ memset(vis,0,sizeof(vis)); memset(ans,0,sizeof(ans)); memset(f,-1,sizeof(f)); tarjan(root); for(int i=1;i<=n;i++) { if(ans[i]) { printf("%d:%d\n",i,ans[i]); } }}int main(){ freopen("1470.txt","r",stdin); while(init()) solve(); return 0;}
- poj--1470--tarjan算法
- POJ 1470 Tarjan算法
- POJ 1470 LCA tarjan 离线算法
- poj 2553 tarjan算法
- poj 2186 tarjan算法
- POJ 1236 Tarjan算法
- poj 1236(tarjan算法)
- LCA问题的Tarjan离线算法 + POJ 1470
- poj 1470 Closest Common Ancestors(离线Tarjan算法)
- POJ 1470Closest Common Ancestors tarjan算法求LCA
- POJ 1470 Closest Common Ancestors LCA Tarjan算法
- poj 1470--tarjan--LCA
- poj 1236 强连通图 tarjan算法
- poj 2186(tarjan 算法实现)
- POJ 2186 强连通分量 Tarjan算法
- poj 1330 LCA [ tarjan算法 deserve thinking ]
- POJ 1144 Network ---tarjan算法求割点
- POJ 1523 SPF tarjan算法入门
- 软件开发所需要的十三个文档
- C++/GDI+ 学习笔记(三)——基础
- STL学习(一)
- C#中的线程入门
- Android之Activity ContentProvider Service Broadcast,线程间通信,AIDL(三)
- poj--1470--tarjan算法
- 云端与终端
- 微信O2O迷局
- 红皮书5个小实例的总结
- C++/GDI+ 学习笔记(四)——实用技巧——颜色矩阵(ColorMatrix)
- 数据库索引
- 【PDF文档加密保护的两种方法】
- 如何利用技术提升微信营销效果
- iframe 的用法与注意事项