标签:scan numbers was ora ast tar new -- sample
Time Limit: 15000MS | Memory Limit: 65536K | |
Total Submissions: 9460 | Accepted: 3497 | |
Case Time Limit: 2000MS |
Description
Input
Output
Sample Input
4 2 1 2 2 1 2 2 2 3 2 3 4 1 2 3 4
Sample Output
2 1 2 2 1 2 1 3 1 4
题解这里
#include<vector> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=4080; const int M=4e6+88; vector<int>sc[N]; vector<int>G[N]; int head[N],dfn[N],low[N],q[N]; bool instack[N],mp[2008][2008]; int tot,scnt,l,cnt; struct node { int to,next; } e[M]; void add(int u,int v) { e[tot].to=v; e[tot].next=head[u]; head[u]=tot++; } void Tarjan(int u) { dfn[u]=low[u]=++cnt; q[l++]=u; instack[u]=1; for(int i=head[u]; ~i; i=e[i].next) { int v=e[i].to; if(!dfn[v]) { Tarjan(v); low[u]=min(low[u],low[v]); } else if(instack[v]&&dfn[v]<low[u]) low[u]=dfn[v]; } if(low[u]==dfn[u]) { ++scnt; int t; do { t=q[--l]; sc[scnt].push_back(t); instack[t]=0; } while(t!=u); } } int main() { int n,x,y; while(scanf("%d",&n)!=EOF) { tot=scnt=cnt=l=0; memset(head,-1,sizeof(head)); memset(dfn,0,sizeof(dfn)); memset(mp,0,sizeof(mp)); for(int i=1; i<=n; ++i) { scanf("%d",&x); while(x--) { scanf("%d",&y); add(i,y+n); G[i].clear(); sc[i].clear(); mp[i][y]=1; } } for(int i=1; i<=n; ++i) { scanf("%d",&x); add(x+n,i); } for(int i=1; i<=n; ++i) if(!dfn[i]) Tarjan(i); for(int i=1; i<=scnt; ++i) sort(sc[i].begin(),sc[i].end()); for(int i=1; i<=scnt; ++i) { int tc=upper_bound(sc[i].begin(),sc[i].end(),n)-sc[i].begin(); for(int j=0; j<tc; ++j) for(int k=tc; k<(int)sc[i].size(); ++k) if(mp[sc[i][j]][sc[i][k]-n] ) G[sc[i][j]].push_back(sc[i][k]-n); } for(int i=1; i<=n; ++i) { printf("%d",(int)G[i].size()); for(int j=0; j<(int)G[i].size(); ++j) printf(" %d",G[i][j]); puts(""); } } }
标签:scan numbers was ora ast tar new -- sample
原文地址:http://www.cnblogs.com/mfys/p/7612159.html