Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
WA无数,谁能帮我看看或者给点数据#include<iostream> #include<stack> using namespace std; int n,m,index,tot; int first[1001],last[1001],net[600],a[1001],b[1001];//邻接表 int dfn[1001],low[1001],belong[1001];//belong(改点所在的强连通块) int instack[1001]; int lin[1001][1001],lout[1001][1001];//入度和出度 stack <int> s; void tarjan(int v)//Tarjan主体 { dfn[v]=low[v]=++index; instack[v]=true; s.push(v); for (int i=first[v];i!=0;i=net[i]) { if (!dfn[b[i]]) { tarjan(b[i]); low[v]=min(low[v],low[b[i]]); } else if (instack[b[i]]) low[v]=min(low[v],dfn[b[i]]); } if (low[v]==dfn[v]) { ++tot; int u; do { u=s.top(); s.pop(); belong[u]=tot;//记录u所在的连通块 instack[u]=false; } while (v!=u); } } bool topo(int s)//拓扑排序 { int now=s;//当前处理的结点 int sum=tot; bool b[1001]; memset(b,0,sizeof(b)); while (lout[now][0]!=0) { b[now]=true;//删点 --sum;//剩余未处理结点数 int k=lout[now][0]; for (int i=1;i<=k;++i) //删边 { --lout[now][0]; --lin[lout[now][i]][0]; } int t=0; for (int i=1;i<=tot;++i) //更新点 if (lin[i][0]==0&&!b[i]) { ++t; now=i; } if (t>1) return false;//如果发现两个入度为0的点,则说明这两个点只能由已经被删掉的点到达,也就是说这两个点互相不可达 } if (sum==1) return true; return false; } int main() { int t; cin>>t; for (int f=1;f<=t;++f) { //先初始化 tot=0;index=0; memset(first,0,sizeof(first)); memset(last,0,sizeof(last)); memset(net,0,sizeof(net)); memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); memset(belong,0,sizeof(belong)); memset(lin,0,sizeof(lin)); memset(lout,0,sizeof(lout)); memset(instack,0,sizeof(instack)); while (!s.empty()) s.pop(); cin>>n>>m; for (int i=1;i<=m;++i)//邻接表 { cin>>a[i]>>b[i]; if (first[a[i]]==0) first[a[i]]=i,last[a[i]]=i; else { net[last[a[i]]]=i; last[a[i]]=i; } } for (int i=1;i<=n;++i) if (!dfn[i]) tarjan(i); for(int i=1;i<=m;++i)//记录入度和出度 if (belong[a[i]]!=belong[b[i]]) { ++lin[belong[b[i]]][0]; lin[belong[b[i]]][lin[belong[b[i]]][0]]=belong[a[i]]; ++lout[belong[a[i]]][0]; lout[belong[a[i]]][lout[belong[a[i]]][0]]=belong[b[i]]; } for (int i=1;i<=tot;++i) if (lin[i][0]==0) { if (topo(i)) cout<<"Yes"<<endl; else cout<<"No"<<endl; break; } } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator