标签:style blog http color os java io ar for
4 2 1 2 2 1 2 2 2 3 2 3 4 1 2 3 4
2 1 2 2 1 2 1 3 1 4
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 2000000; 18 struct arc{ 19 int to,next; 20 }; 21 arc e[maxn]; 22 int head[maxn],dfn[maxn],low[maxn],tot; 23 bool instack[maxn]; 24 int belong[maxn],cnt,scc,n; 25 stack<int>stk; 26 void add(int u,int v){ 27 e[tot].to = v; 28 e[tot].next = head[u]; 29 head[u] = tot++; 30 } 31 void tarjan(int u){ 32 dfn[u] = low[u] = ++cnt; 33 stk.push(u); 34 instack[u] = true; 35 for(int i = head[u]; i != -1; i = e[i].next){ 36 if(!dfn[e[i].to]){ 37 tarjan(e[i].to); 38 low[u] = min(low[u],low[e[i].to]); 39 }else if(instack[e[i].to]) 40 low[u] = min(low[u],dfn[e[i].to]); 41 } 42 if(dfn[u] == low[u]){ 43 int v; 44 scc++; 45 do{ 46 v = stk.top(); 47 belong[v] = scc; 48 instack[v] = false; 49 stk.pop(); 50 }while(v != u); 51 } 52 } 53 vector<int>ans; 54 int main() { 55 int i,j,k,u,v; 56 scanf("%d",&n); 57 for(i = 1; i <= n<<1; i++){ 58 dfn[i] = low[i] = belong[i] = 0; 59 instack[i] = false; 60 head[i] = -1; 61 } 62 cnt = scc = tot = 0; 63 for(i = 1; i <= n; i++){ 64 scanf("%d",&k); 65 while(k--){ 66 scanf("%d",&j); 67 add(i,j+n); 68 } 69 } 70 for(i = 1; i <= n; i++){ 71 scanf("%d",&j); 72 add(j+n,i); 73 } 74 for(i = 1; i <= n; i++) 75 if(!dfn[i]) tarjan(i); 76 for(i = 1; i <= n; i++){ 77 ans.clear(); 78 for(j = head[i]; j != -1; j = e[j].next){ 79 if(e[j].to > n && belong[i] == belong[e[j].to]){ 80 ans.push_back(e[j].to-n); 81 } 82 } 83 sort(ans.begin(),ans.end());//要求递增 84 printf("%d",ans.size()); 85 for(j = 0; j < ans.size(); j++) 86 printf(" %d",ans[j]); 87 puts(""); 88 } 89 return 0; 90 }
标签:style blog http color os java io ar for
原文地址:http://www.cnblogs.com/crackpotisback/p/3944770.html