标签:
input | output |
---|---|
8 1 3 3 6 4 5 6 2 4 6 8 1 BLOOD 3 8 |
4 5 7 |
6 1 2 3 2 1 4 3 4 2 6 5 2 5 4 BLOOD 2 5 |
0 |
#include <iostream> #include <cstring> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <time.h> #include <string> #include <map> #include <stack> #include <vector> #include <set> #include <queue> #define inf 0x3f3f3f3f #define mod 10000 typedef long long ll; using namespace std; const int N=1005; const int M=100005; int n,m,k=0,a,b; int vis[N]; char str[20]; bool flag=false; vector<int>vec,parent[N],chil[N]; void dfs(int u,int f) {//printf("!!!%d %d\n",u,f); vis[u]=1; if(!f){ for(int i=0;i<parent[u].size();i++){ //printf("@@@%d\n",parent[u][i]); if(!vis[parent[u][i]])dfs(parent[u][i],f); } } else { for(int i=0;i<chil[u].size();i++){ if(!vis[chil[u][i]])dfs(chil[u][i],f); } } } void in(char *ch){ int fff=0,aa=0,bb=0; char A[20],B[20]; for(int i=0;i<strlen(ch);i++){ if(ch[i]==‘ ‘){fff=1;continue;} if(!fff)A[aa++]=ch[i]; else B[bb++]=ch[i]; } A[aa]=0;B[bb]=0; a=atoi(A); b=atoi(B); return; } int main() { scanf("%d",&n);getchar(); while(gets(str)!=NULL){k++; if(str[0]==‘B‘){flag=true;continue;} in(str); if(!flag)parent[a].push_back(b),chil[b].push_back(a); if(flag)vec.push_back(a);//if(k>=9)break; } for(int i=0;i<vec.size();i++){ int v=vec[i]; dfs(v,0); dfs(v,1); } flag=false; for(int i=1;i<=n;i++)if(!vis[i])printf("%d ",i),flag=true; if(!flag)printf("0"); printf("\n"); return 0; }
标签:
原文地址:http://www.cnblogs.com/jianrenfang/p/5875204.html