sth. about tarjan 第二弹~~考前复习版
2016-11-16 17:32
211 查看
唔 RP++
Part 1
最基本的强联通分量
bzoj 1654
注意 编号+的位置
还有 low[x] 的更新!!!
恩恩别的记住统计的条件就好了。
Part2
割点
还是 low[t]>=dfn[x]
搜索树的根节点的节点数 的 数目
还有就是 加加减减的 答案
ps被m=0 坑到了输入
Part3
边双连通分量
恩 没什么 和割点比较像 就是 平行边
Part 1
最基本的强联通分量
bzoj 1654
注意 编号+的位置
还有 low[x] 的更新!!!
low[x]=min(low[x],low[v]) (!dfn[v]) low[x]=min(low[x],dfn[v]) (else)
恩恩别的记住统计的条件就好了。
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; //by mars_ch int n,m; int dfn[10005],low[10005],stack[10005],instack[10005],top; int id,sz[10005]; int tim,tot,first[10005]; struct data { int f,t,nxt; }e[50005*2]; void add(int a,int b) { e[tot].f=a,e[tot].t=b; e[tot].nxt=first[a]; first[a]=tot++; } void tarjan(int x) { dfn[x]=low[x]=++tim; stack[++top]=x; instack[x]=1; for(int i=first[x];i!=-1;i=e[i].nxt) { int t=e[i].t; if(!dfn[t]) { tarjan(t); low[x]=min(low[t],low[x]); } else if(instack[t]) { low[x]=min(low[x],dfn[t]); } } if(low[x] == dfn[x]) { ++id; while(1) { int t=stack[top--]; instack[t]=0; sz[id]++; if(t == x) break; } } } int main() { scanf("%d%d",&n,&m); memset(first,-1,sizeof(first)); for(int i=1;i<=m;i++) { int a,b; scanf("%d%d",&a,&b); add(b,a); } for(int i=1;i<=n;i++) { if(!dfn[i]) { tarjan(i); } } int ans=0; for(int i=1;i<=id;i++) { if(sz[i]>1) { ans++; } } printf("%d\n",ans); return 0; }
Part2
割点
还是 low[t]>=dfn[x]
搜索树的根节点的节点数 的 数目
还有就是 加加减减的 答案
ps被m=0 坑到了输入
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; //by mars_ch int n,m; struct data { int f,t,nxt; }e[50005*2]; int first[10005],tot; int stack[10005],instack[10005]; int dfn[100005],low[100005]; int ans=0; int tim,top,son,maxx; void add(int a,int b) { e[tot].f=a,e[tot].t=b; e[tot].nxt=first[a],first[a]=tot++; } void tarjan(int x,int root) { dfn[x]=low[x]=++tim; int child=0; for(int i=first[x];i!=-1;i=e[i].nxt) { int t=e[i].t; if(!dfn[t]) { tarjan(t,root); low[x]=min(low[x],low[t]); if(x == root) son++; else if(low[t]>=dfn[x]) { child++; } } else { low[x]=min(low[x],dfn[t]); } } ans=max(ans,child+1); } int main() { while(scanf("%d%d",&n,&m) && (n!=0 || m!=0)) { memset(first,-1,sizeof(first)); memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); ans=0,maxx=0,tot=0; int cnt=0; if(m == 0) printf("%d\n",n-1); else { for(int i=1;i<=m;i++) { int a,b; scanf("%d%d",&a,&b); add(a,b); add(b,a); } for(int i=0;i<n;i++) { if(!dfn[i]) { cnt++; son=0; tarjan(i,i); //printf("the number of son is %d\n",son); if(son>1) ans=max(ans,son); maxx=max(maxx,ans); } } // printf("%d %d\n",cnt,maxx); printf("%d\n",cnt-1+maxx); } } return 0; }
Part3
边双连通分量
恩 没什么 和割点比较像 就是 平行边
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; //by mars_ch int n,m; struct data { int f,t,nxt; }e[100005*2]; int first[1005],tot; void add(int a,int b) { e[tot].f=a,e[tot].t=b; e[tot].nxt=first[a],first[a]=tot++; } int stack[1005],instack[1005],top,idx,tim; int low[1005],dfn[1005],belong[1005],deg[1005]; void tarjan(int x,int fa) { dfn[x]=low[x]=++tim; stack[++top]=x,instack[x]=1; for(int i=first[x];i!=-1;i=e[i].nxt) { int t=e[i].t; if(t == fa) continue; if(!dfn[t]) { tarjan(t,x); low[x]=min(low[x],low[t]); } else if(instack[t]) { low[x]=min(low[x],dfn[t]); } } if(low[x] == dfn[x]) { ++idx; while(1) { int t=stack[top--]; belong[t]=idx; if(t == x) break; } } } int main() { scanf("%d%d",&n,&m); memset(first,-1,sizeof(first)); for(int i=1;i<=m;i++) { int a,b; scanf("%d%d",&a,&b); add(a,b); add(b,a); } for(int i=1;i<=n;i++) { if(!dfn[i]) { tarjan(i,-1); } } for(int i=1;i<=n;i++) { for(int j=first[i];j!=-1;j=e[j].nxt) { int t=e[j].t; if(belong[i]!=belong[t]) { deg[belong[t]]++; } } } int ans=0; for(int i=1;i<=idx;i++) { if(deg[i] == 1) { ans++; } } printf("%d\n",(ans+1)/2); return 0; }
相关文章推荐
- sth. about tarjan
- Sth about Java
- E文积累_20080310_not know beans about sth.
- Sth about Gospel||Soul Rock||Punk||RnB||Hip-Hop
- sth about "this"
- Sth about 函数式编程(Functional Programming)
- sth about struts2
- sth. about SSL (secure socket layer), JSSE (java secure socket extension)
- 英语作业(general version an narrow version about sth)
- Sth about java security
- Sth. about Static Library
- Sth about network scanning
- sth about Hadoop
- sth about Inheritance
- sth about intrusion
- keep dark about sth
- sth. about "批处理" "应用"
- Sth. about Static Library
- Sth. about WinCE
- sth about "批处理文件"(2)